Hello,
This is all very new to me and I am learning a lot. I am trying to make a reaction game, the game will drop items for the player to catch, the items need to be dropped in a random order at random intervals.
Via lots of reading, getting things wrong, pinching bits from other example programs from here and there, I have managed to make a program that gets pretty close.
To the best of my understanding, the program seems to have the functionality to randomly select the outputs and incorporates a random delays between each. (sometimes, as it is random it could select an output that's already dropped - so nothing happens - ideally it would be good for it to not repeat, but currently its not an issue and just adds to the randomness.)
BUT when I run the program in the simulator the LED's that visualise the release signal always light up in the same order with the same timing, there is nothing random about it.
8>2>3>1small pause >4>5>6 long pause >7. its this order and timing every time.
I haven't physically built anything yet so don't know if its just a quirk of the simulator and it would work in real life? (didn't want to spend money and time building the game till I knew the code worked)
There are probably many improvements that can be made, like how I flash the green LED after the start button is pressed but my main priority is to get the randomness working.
Can anyone provide any assistance as to why my random selections and timings just aren't random.
Thank you in advance for any help/advise/pointers you can give.
// put your setup code here, to run once:
// constants won't change. They're used here to set pin numbers:
const int StartPin = 2; // the number of the pushbutton pin
const int CrazyPin = 4; // Crazy mode - drop all
const int ReadyLED = 3; // Get ready LED
const int GatePin1 = 5; // drop gate 1
const int GatePin2 = 6; // drop gate 2
const int GatePin3 = 7; // drop gate 3
const int GatePin4 = 8; // drop gate 4
const int GatePin5 = 9; // drop gate 5
const int GatePin6 = 10; // drop gate 6
const int GatePin7 = 11; // drop gate 7
const int GatePin8 = 12; // drop gate 8
// variables will change:
int buttonStateStart = 0;
int buttonStateCrazy = 0;
void setup() {
// initialize the gate pins as outputs:
pinMode(GatePin1, OUTPUT);
pinMode(GatePin2, OUTPUT);
pinMode(GatePin3, OUTPUT);
pinMode(GatePin4, OUTPUT);
pinMode(GatePin5, OUTPUT);
pinMode(GatePin6, OUTPUT);
pinMode(GatePin7, OUTPUT);
pinMode(GatePin8, OUTPUT);
pinMode(ReadyLED, OUTPUT);
// initialize the pushbutton and reset pin as an input:
pinMode(StartPin, INPUT);
pinMode(CrazyPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonStateStart = digitalRead(StartPin);
buttonStateCrazy = digitalRead(CrazyPin);
// checks for the Start button being pressed when in Crazy Mode
if (buttonStateCrazy == HIGH and buttonStateStart == HIGH) {
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
digitalWrite(GatePin1, HIGH);
digitalWrite(GatePin2, HIGH);
digitalWrite(GatePin3, HIGH);
digitalWrite(GatePin4, HIGH);
digitalWrite(GatePin5, HIGH);
digitalWrite(GatePin6, HIGH);
digitalWrite(GatePin7, HIGH);
digitalWrite(GatePin8, HIGH);
}
// check if the pushbutton is pressed. If it is, start the program:
if (buttonStateCrazy == LOW and buttonStateStart == HIGH) {
digitalWrite(ReadyLED, HIGH); //flashing the LED to prep player and forms a delay after pressing start button
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
digitalWrite(ReadyLED, HIGH);
delay(500);
digitalWrite(ReadyLED, LOW);
delay(500);
start:
int const ran = random(1, 9);
int const ranDel = random(50, 200);
if (ran == 1) {
digitalWrite(GatePin1, HIGH);
delay(ranDel);
goto start;
}
if (ran == 2) {
digitalWrite(GatePin2, HIGH);
delay(ranDel);
goto start;
}
if (ran == 3) {
digitalWrite(GatePin3, HIGH);
delay(ranDel);
goto start;
}
if (ran == 4) {
digitalWrite(GatePin4, HIGH);
delay(ranDel);
goto start;
}
if (ran == 5) {
digitalWrite(GatePin5, HIGH);
delay(ranDel);
goto start;
}
if (ran == 6) {
digitalWrite(GatePin6, HIGH);
delay(ranDel);
goto start;
}
if (ran == 7) {
digitalWrite(GatePin7, HIGH);
delay(ranDel);
goto start;
}
if (ran == 8) {
digitalWrite(GatePin8, HIGH);
delay(ranDel);
goto start;
}
}
}
PS - There is also a "Crazy mode" that will be a hidden switch that can drop all the items at the same time, just for laughs
PPS - There are 8 gates for dropping in the code/simulator but this might not be the final physical design I plan to decide this later when physically building it and will just adjust the code as appropriate