Hello,
I am trying to make a game that checks a reaction speed. The point is that if a random LED lights up, the person needs to press corresponding button in a specified time frame. I have tried to use Random function, the code works, but the problem is that this Random value does not correlate with the LED that lights up; for example, there are three LED lights, if the second one lights up, a Random Value should be 1, but in my case, it is equal to arbitrary number, and I wasn't able to notice any pattern in there.
I am a newbie in Arduino coding, so the code could be a bit messy.
#include "pitches.h"
int led = 9;
int minPin = 9;
int maxPin = 11;
int speaker = 7;
int contButton[] = {3, 5, 6};
int mills = 0;
int score = 0;
int ledNumber = 0;
int melody[] = {NOTE_D5, NOTE_D5, NOTE_G4, NOTE_A4, NOTE_D5, NOTE_B4, NOTE_B3, NOTE_A4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
for (int i = minPin; i <= maxPin; i++) {
pinMode(i, OUTPUT);
}
pinMode(contButton[0], INPUT);
pinMode(contButton[1], INPUT);
pinMode(contButton[2], INPUT);
int noteDuration = 1000 / 16;
tone(speaker, NOTE_A7, noteDuration);
delay(noteDuration * 1.3);
noTone(speaker);
ledNumber = random(3);
digitalWrite(led + ledNumber, HIGH);
}
void loop() {
int whileCheck = 0;
mills = millis();
while (millis() < mills + 5000) {
Serial.println(ledNumber);
if (digitalRead(contButton[ledNumber]) == HIGH) {
cont();
digitalWrite(led + ledNumber, HIGH);
whileCheck = 1;
break;
}
}
if (whileCheck != 1) {
lossV2();
digitalWrite(led + ledNumber, HIGH);
whileCheck = 0;
}
delay(10);
ledNumber = random(3);
led = 9;
}
void cont() {
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, HIGH);
}
delay(700);
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, LOW);
}
led = 9;
}
void lossV2() {
led = minPin - 1;
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, LOW);
}
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(led, LOW);
led++;
digitalWrite(led, HIGH);
delay(50);
}
led = maxPin + 1;
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(led, LOW);
led = led - 1;
digitalWrite(led, HIGH);
delay(50);
}
delay(100);
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, LOW);
}
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, HIGH);
}
delay(750);
for (int i = minPin; i <= maxPin; i++) {
digitalWrite(i, LOW);
}
led = 9;
}
void music() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(speaker, melody[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speaker);
}
}
I would really appreciate your help!