I am receiving a four number string from an Android app, 1-255 over serial from bluetooth in the form of (### ### ### ###),(R,G,B,A) Throwing out the alpha for now. I can read them on the serial monitor so I know they are being received. The wiring is very simple for the RGB LED, pins 9,10,11, to a 220 ohm resistor in series with the LED legs, and the cathode to ground. The LED works if power applied to resistor, so its not fried.
Not sure if I broke something, or where to go from here.
Any and all help appreciated.
Thanks in advance
//Thanks to Author: Trevor Shannon,et al For Arduino code, snippets tweaked by Mike
//see http://trevorshp.com/creations/android_led.htm
//pin definitions. must be PWM-capable pins!
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
//maximum duty cycle to be used on each led for color balancing.
//if "white" (R=255, G=255, B=255) doesn't look white, reduce the red, green, or blue max value.
const int max_red = 255;
const int max_green = 90;
const int max_blue = 100;
byte colors[4] = {0, 0, 0, 0}; //array to store led brightness values
byte lineEnding = 0x29; // ")" //originally 0A; //10 in decimal, ASCII newline character
void setup()
{
Serial.begin(9600); //note this may need to be changed to match your module
Serial.println("OK then, you first, say something.....");
Serial.println("Go on, type something in the space above and hit Send,");
Serial.println("or just hit the Enter key");
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
//this is just to test the serial connection
{
while(Serial.available()==0)
{}
delay(500);
Serial.println("");
Serial.println("I heard you say:");
while(Serial.available()>0)
{
Serial.write(Serial.read()); // note it is Serial.WRITE
}
Serial.println("");
//this is to take received RGB & set PWM outputs
//check that at least 3 bytes are available on the Serial port
if (Serial.available() > 2){
//store data up until lineEnding (0x29) in the bytesRead array
int bytesRead = Serial.readBytesUntil(lineEnding, (char*)colors, 4);//changed 3 to 4
}
//set the three PWM pins according to the data read from the Serial port
//we also scale the values with map() so that the R, G, and B brightnesses are balanced.
analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
}