Hello, what Im trying to make is a simple game that uses 6 multi-color LEDs and 6 buttons. BUT before I connect 5 LEDs and 5 buttons, I
m doing some experiment with using only 1 LED and 1 button. The game is...
- when LED turns on the RED color, the player needs to push the RED button and the lights turns off.
- Then LED turns on the BLUE color, the player needs to push the BLUE button and the lights turns off.
- (Actually, the color of LED) turns on randomly.
I need some help. Below is the code that I wrote and I`m stuck in the 'BUTTON' part. How can I solve?
Thanks.
int redLed = 9;
int greenLed = 10;
int blueLed = 11;
int BUTTON1 = 13;
int buttonstate1 = 0;
long randNumb;
void setup()
{
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(BUTTON1, INPUT);
}
void red()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
void orange()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 35);
analogWrite(blueLed, 255);
}
void yellow()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
}
void green()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 255);
analogWrite(blueLed, 0);
}
void blue()
{
analogWrite(redLed, 0);
analogWrite(greenLed, 0);
analogWrite(blueLed, 255);
}
void pink()
{
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 162);
}
void loop()
{
buttonstate1 = digitalRead(BUTTON1);
randNumb = random(1, 6); // Generate a random number between 1 and 6
if(randNumb == 1){
red();
}
if(randNumb == 2){
orange();
}
if(randNumb == 3){
yellow();
}
if(randNumb == 4){
green();
}
if(randNumb == 5){
blue();
}
if(randNumb == 6){
pink();
}
//I think I need to do something with below
if(buttonstate1 == LOW) {
analogWrite(redLed, LOW);
analogWrite(greenLed, LOW);
analogWrite(blueLed, LOW);
} else {
(buttonstate1 == HIGH);
}
}