Hi i found on this forum a program to select random outputs that i want to use to create a reaction game.
The program works very well but when i instal a relay board some of the relays don't work. I have tested the relay board with a simple program and it works perfect. It looks like the outputs aren't declared as output.
//https://forum.arduino.cc/t/random-output-selection-only-once/394681/9//
const int stickCount = 8;
boolean selected[stickCount]; // true => stick has been selected
boolean inSequence = false; // true => button has been pressed and sequences are running
int pushButton = 3; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int catch_a_stick = 0;
unsigned long a = 4000UL; //tthis sets the time before the next stick will fall
unsigned long b = 800UL;
int stick1 = 5; //this makes stick 1 fall
int stick2 = 6; //this makes stick 2 fall
int stick3 = 7; //this makes stick 3 fall
int stick4 = 8; //this makes stick 4 fall
int stick5 = 9; //this makes stick 5 fall
int stick6 = 10; //this makes stick 6 fall
int stick7 = 11; //this makes stick 7 fall
int stick8 = 12; //this makes stick 8 fall
void setup() { //this sets the output pins
Serial.begin(9600);
randomSeed(analogRead(0));
Serial.println("Ready ...");
// set all selected to false
clearSelected();
pinMode(pushButton, INPUT_PULLUP); // declare pushbutton as input
pinMode(stick1,OUTPUT); // declare to be an output
pinMode(stick2,OUTPUT); // declare to be an output
pinMode(stick3,OUTPUT); // declare to be an output
pinMode(stick4,OUTPUT); // declare to be an output
pinMode(stick5,OUTPUT); // declare to be an output
pinMode(stick6,OUTPUT); // declare to be an output
pinMode(stick7,OUTPUT); // declare to be an output
pinMode(stick8,OUTPUT); // declare to be an output
}
// call before slecting random sequence
void clearSelected()
{
for (int i = 0; i < stickCount; i++)
{
selected[i] = false;
}
Serial.println("Sticks cleared.");
}
void loop() {
int catch_a_stick;
int rnd;
// if button is pressed and not running the sequence
if ( (inSequence == false) && (digitalRead(pushButton) == LOW) )
{
inSequence = true; // set flag
clearSelected(); // mark all sticks as available
}
// if we are running the sequence
if ( inSequence == true )
{
// if there are sticks that are not chosen
if ( sticksChosen() < stickCount )
{
while ( true )
{
// choose a possible stick
rnd = random(stickCount);
// if it has not been selected
if ( selected[rnd] == false )
{
catch_a_stick = rnd; // set as chosen
selected[rnd] = true; // mark that it has been selected
Serial.print("Stick: "); Serial.println(catch_a_stick);
break; // done looking
} // if
} // while
// run selected sequence
Serial.println(" Begin Delay ");
delay(b);
Serial.println(" End Delay ");
switch (catch_a_stick) {
case 0: //if catch_a_stick equals 0 then the stick1 will be released, stick 1 will fall
analogWrite(stick1, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
analogWrite(stick1, LOW);
break;
case 1: //if catch_a_stick equals 1 then the stick2 will be released, stick 2 will fall
digitalWrite(stick2, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
digitalWrite(stick2, LOW);
break;
case 2: //if catch_a_stick equals 2 then the stick3 will be released, stick 3 will fall
digitalWrite(stick3, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
digitalWrite(stick3, LOW);
break;
case 3: //if catch_a_stick equals 3 then the stick4 will be released, stick 4 will fall
digitalWrite(stick4, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
digitalWrite(stick4, LOW);
break;
case 4: //if catch_a_stick equals 4 then the stick5 will be released, stick 5 will fall
analogWrite(stick5, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
analogWrite(stick5, LOW);
break;
case 5: //if catch_a_stick equals 5 then the stick6 will be released, stick 6 will fall
digitalWrite(stick6, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
digitalWrite(stick6, LOW);
break;
case 6: //if catch_a_stick equals 6 then the stick7 will be released, stick 7 will fall
digitalWrite(stick7, HIGH);
delay(a);
digitalWrite(stick7, LOW);
break;
case 7: //if catch_a_stick equals 7 then the stick8 will be released, stick 8 will fall
digitalWrite(stick8, HIGH);
Serial.println(" Begin Delay 2");
delay(a);
Serial.println(" End Delay 2");
digitalWrite(stick8, LOW);
break;
default:
break;
}
} else {
// all completed, clear flag
inSequence = false;
} // else
} // if
}
// return # of sticks that have been chosen
//
int sticksChosen()
{
int retVal = 0;
// look at all sticks
for (int i = 0; i < stickCount; i++)
{
// increment count if this one has been selected
if ( selected[i] == true ) retVal++;
} // for
return retVal;
}