So, I'm new to programming (but having fun). I have written the following code but when I run it and send the ASCII characters to toggle an output (which is used to turn a relay on or off), it switches the output on for approx. one second and then it always clicks back to off. I want the relay, when the ASCII is received, to switch states and hold it's position there until the same ASCII message is received. I don't know that it matters but, I'm using a Fundamental Logic MaxSerial board (Diecimila).
int Pin0 = 0; // Relay Pins and LED test on 13 int Pin1 = 1; int Pin2 = 2; int Pin3 = 3; int Pin4 = 4; int Pin5 = 5; int Pin6 = 6; int Pin7 = 7; int Pin8 = 8; int Pin9 = 9; int Pin10 = 10; int Pin11 = 11; int Pin12 = 12; int ledPin = 13; int Pin14 = 14; int Pin15 = 15; int Pin16 = 16; int incomingCommand = 0; //holding variable for strings
void setup() // run once, when the sketch starts { Serial.begin(9600); pinMode(Pin0, OUTPUT); // sets the digital pins as outputs pinMode(Pin1, OUTPUT); pinMode(Pin2, OUTPUT); pinMode(Pin3, OUTPUT); pinMode(Pin4, OUTPUT); pinMode(Pin5, OUTPUT); pinMode(Pin6, OUTPUT); pinMode(Pin7, OUTPUT); pinMode(Pin8, OUTPUT); pinMode(Pin9, OUTPUT); pinMode(Pin10, OUTPUT); pinMode(Pin11, OUTPUT); pinMode(Pin12, OUTPUT); pinMode(ledPin, OUTPUT); pinMode(Pin14, OUTPUT); pinMode(Pin15, OUTPUT); pinMode(Pin16, OUTPUT); }
void loop(){ if (Serial.available() > 0) { digitalWrite (ledPin, HIGH); //turn LED on when serial incoming incomingCommand = Serial.read(); } else { digitalWrite (ledPin, LOW); //turn it off when no serial available }
//Serial.flush();
if (incomingCommand == 00) while(Pin0 == true){ digitalWrite(Pin0, LOW); } else{ digitalWrite(Pin0, HIGH); }
//Serial.flush();
if (incomingCommand == 01) while(Pin1 = true){ digitalWrite(Pin1, LOW); } else{ digitalWrite(Pin1, HIGH); }
//Serial.flush();
if (incomingCommand == 02) while(Pin2 = true){ digitalWrite(Pin2, LOW); } else{ digitalWrite(Pin2, HIGH); }
//Serial.flush();
if (incomingCommand == 03) while(Pin3 = true){ digitalWrite(Pin3, LOW); } else{ digitalWrite(Pin3, HIGH); }
//Serial.flush();
if (incomingCommand == 04) while(Pin4 = true){ digitalWrite(Pin4, LOW); } else{ digitalWrite(Pin4, HIGH); }
//Serial.flush();
if (incomingCommand == 05) while(Pin5 = true){ digitalWrite(Pin5, LOW); } else{ digitalWrite(Pin5, HIGH); }
//Serial.flush();
if (incomingCommand == 06) while(Pin6 = true){ digitalWrite(Pin6, LOW); } else{ digitalWrite(Pin6, HIGH); }
//Serial.flush();
if (incomingCommand == 07) while(Pin7 = true){ digitalWrite(Pin7, LOW); } else{ digitalWrite(Pin7, HIGH); }
//Serial.flush();
if (incomingCommand == 18) while(Pin8 = true){ digitalWrite(Pin8, LOW); } else{ digitalWrite(Pin8, HIGH); }
//Serial.flush();
if (incomingCommand == 19) while(Pin9 = true){ digitalWrite(Pin9, LOW); } else{ digitalWrite(Pin9, HIGH); }
//Serial.flush();
if (incomingCommand == 10) while(Pin10 = true){ digitalWrite(Pin10, LOW); } else{ digitalWrite(Pin10, HIGH); }
//Serial.flush();
if (incomingCommand == 11) while(Pin11 = true){ digitalWrite(Pin11, LOW); } else{ digitalWrite(Pin11, HIGH); }
//Serial.flush();
if (incomingCommand == 12) while(Pin12 = true){ digitalWrite(Pin12, LOW); } else{ digitalWrite(Pin12, HIGH); }
//Serial.flush();
if (incomingCommand = 14) while(Pin14 = true){ digitalWrite(Pin14, LOW); } else{ digitalWrite(Pin14, HIGH); }
//Serial.flush();
if (incomingCommand == 15) while(Pin15 = true){ digitalWrite(Pin15, LOW); } else{ digitalWrite(Pin15, HIGH); }
//Serial.flush();
if (incomingCommand == 16) while(Pin16 = true){ digitalWrite(Pin16, LOW); } else{ digitalWrite(Pin16, HIGH); }
//Serial.flush();
}