Hi guys, recently I bought arduino uno for my school project. I am trying to make the light go ON and OFF using ArduDroid app. This is fourth day I am working on it and I managed to make the light go on and off, I watched a lot of tutorial videos and I have basic programing knowledge so it wasnt so hard. Project is based on code I found on the internet, it was meant to control LEDs via bluetooth, I just edited it for relay. But problem came when I tried to edit it even more, actually I tried to implement commands which made arduino invert previous input value ( for example when it would obtain same value '1' for turning relay ON twice in the row, it would go back off), but I never managed to make it work. I tried multiple variations but I think there must be some common mistake I was unable to see, so I´d like to ask you for help please. The code:
int relay = 7;
int prev; //previous value
int val; //actual value
void setup()
{
pinMode(relay, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) //send data when recieved
{
val = Serial.read(); //read value
}
if (val == '1')
{
if (prev == '1')
{
digitalWrite(relay, LOW);
prev=val;
}
else
{
digitalWrite(relay, HIGH);
prev=val;
}
}
else if (val == '0');
{
if (prev == '0')
{
digitalWrite(relay, HIGH);
prev=val;
}
else
{
digitalWrite(relay, LOW);
prev=val;
}
}
}