Hello! I am a newbie in programming and arduino in general, and I have a question for you more experienced folks!
I have made the right "connections" on the breadboard and arduino, but my problem is the coding part.
I want to make following things happen:
1. Have a working ON and OFF switch witch makes the LED light turn completly ON and OFF
2. Get the LED light when turned ON to flash 3 different random lights (RED, GREEN, BLUE)
3. Have a delay of 5000milliseconds (5 sec) while on a color before switching to another random color
My code is messy, and I do probably need to use millis instead of delay.
However I have little to no idea how to do this and make the 3. things work.
Extremly happy if anybody could help me!
The code:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
int buttonstatus = 0;
int times = 0;
int a = 5000; //this sets how long the stays one color for
int red = 11; //this sets the red led pin
int green = 12; //this sets the green led pin
int blue = 13; //this sets the blue led pin
int ledcolor = random(2); //this randomly selects a number between 0 and 6
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (buttonState == HIGH) {
if(buttonstatus == 1) {
digitalWrite(ledPin, LOW);
buttonstatus = 0;
delay(200);
} else {
digitalWrite(ledPin, HIGH);
buttonstatus = 1;
delay(200);
}
}
}
if(buttonstatus == 1) {
switch (ledcolor) {
case 0: //if ledcolor equals 0 then the led will turn red
analogWrite(red, 51);
delay(a);
analogWrite(red, 255);
break;
case 1: //if ledcolor equals 1 then the led will turn green
digitalWrite(green, LOW);
delay(a);
digitalWrite(green, HIGH);
break;
case 2: //if ledcolor equals 2 then the led will turn blue
digitalWrite(blue, LOW);
delay(a);
digitalWrite(blue, HIGH);
break;
}
Serial.println("Buttonswitch is ON");
} else if(buttonstatus == 0) {
digitalWrite(blue, LOW);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
Serial.println("Buttonswitch is OFF");
}
}