Hello everyone, new to this forum. Done a few Arduino projects before. I am wondering if anyone can help me out. I'm trying to control an RGB LED, I want it to cycle through all possible colors for 20 seconds and then pick a random color to hold after this point. Ideally, this would also be activated using a button. I've gotten all these separate things working, and some somewhat working together...but not perfectly.
Main problem right now, button isn't functioning and after it cycles through the colors, it just executes multiple random colors and doesn't pick one to remain.
Take a look at my code and let me know what you think is going wrong.
// Output
int redPin = 10; // Red LED, connected to digital pin 9
int greenPin = 9; // Green LED, connected to digital thatpin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int inPin = 0; // choose the input pin (for a pushbutton)
// Program variables
int redVal = 1; // Variables to store the values to send to the pins
int greenVal = 255; // Initial values are Red full, Green and Blue off
int blueVal = 1;
int pinVal = 0;
int hold = 2000;
int i = 0; // Loop counter
int wait = 3; // 50ms (.05 second) delay; shorten for faster fades
int timer = 0;
int touch = 0;
int redNew = 0;
int blueNew = 0;
int greenNew = 0;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(inPin, INPUT);
timer = 0;
i = 0;
}
// Main program
void loop(){main:
timer += 1;
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 764) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else {
if (timer < 3){
redVal = redNew;
greenVal = greenNew;
blueVal = blueNew;
redNew = random(255);
greenNew = random(255);
blueNew = random(255);
delay(5000);
touch = analogRead(inPin);
if(touch > 10){
i = 0;
}
}
else{
i = 0;
}
}
analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);
delay(wait);
analogRead(inPin);
}