So I am trying to write a program when the potentiometer is anywhere from 0-341, the blue light of the RGB LED comes on. from 342-681 the green light and 681-1023 the red LED lights up. The problem is all green, red and blue all work at the same time. They all start from 0 to 255 for every section but what I want is when red is on, the blue and green be off. I dont know what I am doing wrong. Here is the program I wrote below. Please help. Thanks.
int input = 0;
int inputValue = 0;
int output1 = 3;
int output2 = 5;
int output3 = 6;
int output1Value = 0;
int output2Value = 0;
int output3Value = 0;
void setup() {
Serial.begin (9600);
pinMode (output1, OUTPUT);
pinMode (output2, OUTPUT);
pinMode (output3, OUTPUT);
}
void loop() {
inputValue = analogRead(input);
if (output1Value = map(inputValue, 0, 341, 0, 255))
{
analogWrite ( output1, outputValue);}
if (output2Value = map(inputValue, 342,681, 0, 255))
{
analogWrite ( output2, output2Value);}
if (output3Value = map(inputValue, 682,1023, 0, 255))
{
analogWrite ( output3, output3Value);}
Serial.println(input1Value);
delay(5);
}
I see two problems: you need "==" in the if statements, not "=", and I don't see any code that shuts off any of these that are already lit when you transition to the range for the next.
Thanks, would you suggest using else for shutting them off?
There were lots of problems with the code.
Your comparisons were all wrong.
Why not use meaningful variable and constant names?
Here's what I think you were trying to do.
// Values which don't change are constants.
// Give constants and variables meaningful
// names.
const int POT_PIN = A0;
int potValue;
const int RED_PIN = 3;
const int GREEN_PIN = 5;
const int BLUE_PIN = 6;
int outputRed;
int outputGreen;
int outputBlue;
void setup() {
Serial.begin (9600);
pinMode (RED_PIN, OUTPUT);
pinMode (GREEN_PIN, OUTPUT);
pinMode (BLUE_PIN, OUTPUT);
}
void loop()
{
// Start each loop with all the color values zeroed.
outputRed = 0;
outputGreen = 0;
outputBlue = 0;
potValue = analogRead(POT_PIN);
// Check analog value to see which of three ranges
// it falls in.
// Map the pot value to LED brightness for only the
// the single color.
if (potValue < 342)
{
outputRed = map(potValue, 0, 341, 0, 255);
}
else if (potValue < 682)
{
// since the pot must have value between
// 342 and 681, we'll use those values
// as the extremes.
outputGreen = map(potValue, 342, 681, 0, 255);
}
else // potValue must be 682 or larger
{
outputBlue = map(potValue, 682, 1023, 0, 255);
}
// One LED will be set according to the pot position
// and the others will be set to zero brightness.
analogWrite(RED_PIN, outputRed);
analogWrite(GREEN_PIN, outputGreen);
analogWrite(BLUE_PIN, outputBlue);
Serial.println(potValue);
delay(5);
}
It looks like you were using pin zero as the analog input? Did you intend to use "A0"?
Make sure you read the "How to use this forum" thread to learn to use code tags.
newguy1000:
Thanks, would you suggest using else for shutting them off?
Start the loop with all the output values zero. Then you can set only one output value based on the pot setting. By using analogWrite with all the pins, you can both turn on the one LED and turn off the other LEDs.
Thanks a lot. it seems to work. I am really new and trying to learn by myself as much as I can. really appreciate the help.