/*
Vygeneruje sa náhodna farba na RGB diódu.
Napr. R - 200, G - 10, B 75.
Úlohou užívateľa je uhádnuť,
ktoré spektrum RGB svieti najviac a kliknút
na príslušné tlačidlo.
*/
int rgbRed = 11;
int rgbGreen = 10;
int rgbBlue = 9;
int rgbRedRand = 0;
int rgbGreenRand = 0;
int rgbBlueRand = 0;
int btnRed = 7;
int btnGreen = 6;
int btnBlue = 5;
bool btnRedState = LOW;
bool btnGreenState = LOW;
bool btnBlueState = LOW;
int buzzer = 3;
bool active = HIGH;
String correct = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(rgbRed, OUTPUT);
pinMode(rgbGreen, OUTPUT);
pinMode(rgbBlue, OUTPUT);
pinMode(btnRed, INPUT_PULLUP);
pinMode(btnGreen, INPUT_PULLUP);
pinMode(btnBlue, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
// Red
rgbRedRand = random(0, 256);
analogWrite(rgbRed, rgbRedRand);
// Green
rgbGreenRand = random(0, 256);
analogWrite(rgbGreen, rgbGreenRand);
// Blue
rgbBlueRand = random(0, 256);
analogWrite(rgbBlue, rgbBlueRand);
if(rgbRedRand > rgbGreenRand && rgbRedRand > rgbBlueRand){
correct = "red";
}else if(rgbGreenRand > rgbRedRand && rgbGreenRand > rgbBlueRand){
correct = "green";
}else{
correct = "blue";
}
while(true){
if(digitalRead(btnRed) == LOW ||
digitalRead(btnGreen) == LOW ||
digitalRead(btnBlue) == LOW){
Serial.println("n");
if(correct == "red" && digitalRead(btnRed) == LOW){
Serial.println("Correct. Red!");
buzzIt(HIGH);
}else if(correct == "green" && digitalRead(btnGreen) == LOW){
Serial.println("Correct. Green!");
buzzIt(HIGH);
}else if(correct == "blue" && digitalRead(btnBlue) == LOW){
Serial.println("Correct. Blue!");
buzzIt(HIGH);
}else{
Serial.println("Incorrect.");
buzzIt(LOW);
}
}
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void buzzIt(bool success){
if(success == HIGH){
tone(buzzer, 200, 400);
delay(200);
tone(buzzer, 400, 400);
delay(400);
tone(buzzer, 600, 600);
delay(600);
}else{
tone(buzzer, 200, 400);
delay(400);
}
}