Go Down

Topic: RGB LED control over serial and fading (Read 1 time) previous topic - next topic

sk8er_square

hey there - new to coding and arduino here :)

I was designing an RGB LED which I can control using values from 0-100 from the serial monitor.

1.Unfortunately it seems to be just throwing up weird values in the serial monitor like 48,52.. and ignoring what i'm inputting.

2. I was hoping on adding a fader to fade from one color to the next color with a delay once its been inputted. - ideas?

3. Is this the most effective way of doing this, any coding help would be appreciated.

Thanks for your time!


Code: [Select]
//setup our used ports (where we have the LEDs), put them in the right order!!! Which is RGB!
//            R  G  B
int leds[] = {11,10,9};

//the values for our LEDs, in RGB order
//              R  G  B
int values[] ={255,0,0};

int inByte;

//set here the delay (in milliseconds)
int delaySec = 100;

//setup function, runs only once when the board is initialized
void setup(){
  // initialize serial communication:
  Serial.begin(9600);
  //set our ports for output (so we have power to light up the LEDs)
  for (int i=0;i<3;i++){
    pinMode(leds[i], OUTPUT);
  }
}

void loop(){

  // Check to see if serial connection is open and if it is continue
  if (Serial.available() > 0) {
 
   // Read the serial for an input between 0-100 and change to the appropriate LED colour.
    inByte = Serial.read(); 
   
    if (inByte == 0){
    //White
    values[0] = 255;
    values[1] = 250;
    values[2] = 250;
    }
   
    if ((inByte > 0) && (inByte < 11)){
    //Red
    values[0] = 255;
    values[1] = 0;
    values[2] = 0;
    }   
   
    if ((inByte > 10) && (inByte < 21)){
    //Pink
    values[0] = 255;
    values[1] = 192;
    values[2] = 203;
    }
   
    if ((inByte > 20) && (inByte < 31)){
    //Orange
    values[0] = 255;
    values[1] = 165;
    values[2] = 0;
    }
   
    if ((inByte > 30) && (inByte < 41)){
    //Goldenrod
    values[0] = 218;
    values[1] = 165;
    values[2] = 32;
    }
   
    if ((inByte > 40) && (inByte < 51)){
    //Yellow
    values[0] = 250;
    values[1] = 250;
    values[2] = 0;
    }
   
    if ((inByte > 50) && (inByte < 61)){
    //Green Yellow
    values[0] = 173;
    values[1] = 255;
    values[2] = 47;
    }
   
    if ((inByte > 60) && (inByte < 71)){
    //Green
    values[0] = 0;
    values[1] = 255;
    values[2] = 0;
    }
   
    if ((inByte > 70) && (inByte < 81)){
    //Aqua
    values[0] = 127;
    values[1] = 255;
    values[2] = 212;
    }
   
    if ((inByte > 80) && (inByte < 91)){
    //Blue
    values[0] = 0;
    values[1] = 0;
    values[2] = 255; 
    }
   
    if ((inByte > 91) && (inByte < 101)){
    //Violet
    values[0] = 138;
    values[1] = 43;
    values[2] = 226;
    }

  Serial.println(inByte); 
 
    //change colors
  for (int i=0;i<3;i++){
    analogWrite(leds[i], values[i]);
    delay(delaySec);
  }
}
}

   

Aeturnalus

The Serial Monitor sends ASCII text - the Serial.read() method returns the integer value of that ASCII text.  You will need to convert the text into a number for it to set correctly.  Also, you should probably use a while-loop to iterate through all of the data in the serial buffer, not just the first value.

Example (not tested):

Code: [Select]


void loop() {
 uint8_t serRead[Serial.available()]; // make an array with a size equal to number of bytes in USART buffer
 uint8_t i = 0; // a pointer variable
 while (Serial.available()) {
   serRead[i] = Serial.read(); // read in the byte
   i++; // increment the pointer
 }

 uint8_t value = atoi(serRead); // atoi converts a char pointer into an int

 // do wtv with the value

}


sk8er_square

Thanks so much for your fast reply!

I tried using that code which you posted and received this error:

Code: [Select]
uint8_t value = atoi(serRead); // atoi converts a char pointer into an int
Invalid conversion from 'uint8_t*' to 'const char*'

any ideas?

Aeturnalus

Oh - right.  Replace

Code: [Select]

  uint8_t serRead[Serial.available()];


with this:

Code: [Select]

  char serRead[Serial.available()];


They're the same type, technically, but atoi is picky.

MikMo

#4
Mar 13, 2011, 12:32 pm Last Edit: Mar 13, 2011, 12:35 pm by MikMo Reason: 1
Try to check this:

http://todbot.com/blog/bionicarduino/   ( the serialRGB link a bit down the page)

And this (not serial)

http://genericnerd.blogspot.com/2009/05/arduino-mood-light-controller.html

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy