This is the piece of code that your compiler is complaining about, and there are quite a few things wrong with it.
I’m assuming that it is meant to be a function, and in that case it needs to be given a name, so that the compiler recognises it as a function, and it can be called from within the rest of your code. As it’s meant to control you LEDs, lets call it control_LEDs
, like this…
void control_LEDs()
{
// your code in here...
}
Your piece of function code has a mis-matched number of opening and closing curly brackets, which the compiler won’t like.
In addition, one of your 'ifstatements isn't written correctly as it has a semicolon after the
ifcomparison, and no curly brackets around the action to be taken if the
if` statement evaluates as true.
Also, to find the current state of a GPIO, it’s necessary to do a digitalRead
on the pin, so this:
won’t do what you are wanting it to.
I’m also not sure that the logic of your if / else
statements will actually do what you are expecting.
It’s much easier to see at a glance what your code is meant to be doing if you are sensible about where you position your opening and closing curly brackets, and uses indents appropriately.
In addition to this, you also have some curly brackets scattered around your void setup which shouldn’t be there.
This is what I think your code should look like, with the addition of a timer to call your new control_LEDs
function every 100ms…
#define relay 16
#define openPositionGPIO 17
#define closedPositionGPIO 18
#define vRelayBtn V1
#define v_openLED V2
#define v_closedLED V3
BlynkTimer timer;
void control_LEDs()
{
if (digitalRead(openPositionGPIO) == HIGH)
{
Blynk.virtualWrite(v_openLED,255);
}
else
{
Blynk.virtualWrite(v_openLED,0);
}
if (digitalRead(closedPositionGPIO) == HIGH)
{
Blynk.virtualWrite(v_closedLED,255);
}
else
{
Blynk.virtualWrite(v_closedLED,0);
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay,OUTPUT);
pinMode(openPositionGPIO,INPUT_PULLUP);
pinMode(closedPositionGPIO,INPUT_PULLUP);
timer.setInterval(100,control_LEDs);
Blynk.begin(auth, ssid, pass);
}
BLYNK_WRITE(vRelayBtn)
{
if (param.asInt())
{
digitalWrite(relay, HIGH);
}
else
{
digitalWrite(relay, LOW);
}
}
void loop()
{
timer.run();
Blynk.run();
}
If you’re having trouble sleeping and fancy a little light bedtime reading, then this will tell you all you ever wanted to know (and much, much more) about timers…
Pete.