Hey all, I am trying to get python to dictate the color of the multi-color LED through Arduino. I'm having some problems getting it to work. My connection is fine and I got python to dictate the amount of blinks of an LED. So I know they are communicating. What I'd like to do in python is enter '0' and the LED will turn green, enter '1' and it will turn blue, etc. However, it seems that the only output I'm getting is the value of '0' and in this code, that makes the LED green. If i change the value of '0' it will display that color as the response every time. See if you smart cookies can help me out!
Arduino CODE:
// Output
int redPin = 10; // Red LED
int greenPin = 9; // Green LED
int bluePin = 11; // Blue LED
// Program variables
int serVal = 0; // variable to store the data from the serial port
int redVal = 0; // Variables to store the values to send to the pins
int greenVal = 0;
int blueVal = 0;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
// Main program
void loop() {
serVal = Serial.read(); // read the serial port
if (serVal > '0' && serVal <= '4' ) {
serVal = serVal - '0'; // convert from character to number
for(int i=0; i < serVal; i++) {
{
if (serVal = '0')
{
redVal = 10;
greenVal = 255;
blueVal = 0;
}
if (serVal = '1')
{
redVal = 15;
greenVal = 165;
blueVal = 170;
}
else if (serVal = '2')
{
redVal = 255;
greenVal = 230;
blueVal = 0;
}
else if (serVal = '3')
{
redVal = 220;
greenVal = 90;
blueVal = 0;
}
else if (serVal = '4')
{
redVal = 255;
greenVal = 0;
blueVal = 0;
}
else
{
redVal = 0;
greenVal = 0;
blueVal = 0;
}
}
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
}
}
}