Hey, school holidays are on and i'm pretty bored so I decided to play around with my arduino. I'm pretty new to serial communication between the arduino and the computer. So I need some help. I have set up 10 leds connected to pin 2 - pin 11. I want to turn on the leds by typing the number 1 and so on. I managed to write a code for it. I know decided to write a code whereby when I press the number 1 and the led is already turned on I want it to turn in off. I managed to figure out how to do this using 'boolean'. The problem is that once it turns on, it turns off automatically. How do I stop this. Don't worry I posted the code underneath and it's not really complete yet and i'm working on one led at a time. If you don't mind. Could someone tell me what the while(serial.avalaible() == 0) mean in a simple way. Thanks
int ledPin = 2;
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int ledPin5 = 7;
int ledPin6 = 8;
int ledPin7 = 9;
int ledPin8 = 10;
int ledPin9 = 11;
boolean ledstate = LOW;
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
while(Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 1)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED 1 On");
ledstate = !ledstate;
delay(2000);
}
if (val == 1 && ledstate == HIGH)
{
digitalWrite(ledPin, LOW);
Serial.println("LED 1 Off");
ledstate = !ledstate;
}
else if (val == 2)
{
digitalWrite(ledPin1, HIGH);
}
else if (val == 3)
{
digitalWrite(ledPin2, HIGH);
}
}