Go Down

Topic: Serial Communication Between Arduino (LEDs) and Computer (Read 651 times) previous topic - next topic

Navin2012

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  :)

Code: [Select]
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);
  }
}


AWOL

Concentrate on getting it to work for one LED.
After that, the rest is simple (especially if you learn about arrays)

The "while" simply waits for a character to appear in the receive buffer.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Navin2012

Thanks for the reply. I will try to work on one led from now on but do you know how to make it stay on for the first led
Code: [Select]
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;
  }


AWOL

Quote
but do you know how to make it stay on for the first led

Yes.
The trick is not to turn it off after two seconds.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Navin2012

Ohh. Sorry, the reason the delay was there was because I thought if I added a delay the arduino wouldn't turn off the led the moment it turns on. Even with the delay, it turns if off after two seconds. It's like using a button to turn on and off an led in the arduino. Instead of a button i'm using serial communication. Sorry.

AWOL

why not simply:
Code: [Select]
if (val == 1)
  {
    digitalWrite(ledPin, ledstate);
    ledstate = !ledstate;
  }
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Go Up