Pages: [1]   Go Down
Author Topic: Controlling RGB LED over serial  (Read 658 times)
0 Members and 1 Guest are viewing this topic.
Aalborg, Denmark
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I'm trying to control an common anode RGB led using this code:
Code:
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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Aalborg, Denmark
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes it's selected.
Logged

Aalborg, Denmark
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Try it this way instead:
Code:
      digitalWrite(RED, HIGH);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Aalborg, Denmark
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 16, 2012, 12:50:30 am by Mungoo » Logged

Pages: [1]   Go Up
Jump to: