Go Down

Topic: Controlling RGB LED over serial (Read 764 times) previous topic - next topic

lokefar

Hi

I'm trying to control an common anode RGB led using this code:
Code: [Select]
const int RED = 6;
const int GREEN = 5;
const int BLUE = 3;

int REDvalue = 0;

void setup()
{
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
 
  Serial.begin(9600);
}

void loop(){
  if (Serial.available() > 0) {
    int REDvalue = Serial.read();
   
    switch (REDvalue) {
      case 'a':
        digitalWrite(RED, LOW);
        break;
   
    default:
    for (int RED = 6; ;) {
      digitalWrite(RED, HIGH);
    }
   }
}
}


But when I write a in the serial monitor and press send nothing happens.

johnwasser

Did you select "No line ending" in Serial Monitor?  If not, your 'a' will turn off the red LED and the line ending will immediately turn it back on.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


lokefar

For some reason I can turn it off now... Which as I see it should be the opposed of what should happen. But I can't turn it back on.

johnwasser

This 'for' loop does nothing at all which might explain why you can't turn the light back on.

Code: [Select]

    for (int RED = 6; ;) {
      digitalWrite(RED, HIGH);
    }


Try it this way instead:
Code: [Select]

      digitalWrite(RED, HIGH);
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

lokefar

I've asked for help for my entire project in this post:
http://arduino.cc/forum/index.php/topic,96268.0.html

Any help would be appreciated.

Grumpy_Mike

Quote
I've asked for help for my entire project in this post:

So how does the last comment:-
Quote
Ignore the question. Figured it all out a different way around.

Square with this?

Mungoo

#7
Mar 16, 2012, 06:41 am Last Edit: Mar 16, 2012, 06:50 am by Mungoo Reason: 1
Thank you. I had not come across that site and it helped a lot. You were right that I Led Light Accessories really needed to see how diodes worked and when I saw that understanding the LED was easy.

Go Up