Hello everyone,
I'm trying to make a program that, after a button starts the program, randomley activates an output foor a few seconds and then goes to a next one. But when an outpur has been activated it should not be selected again.
i've made a code that randomly selects an output, but the same output can be selected several times.
I als can't get the button to work
I would really appreciate any help with this
//void setup() {
int pushButton = 13; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int catch_a_stick = 0;
int a = 40000; //tthis sets the time before the next stick will fall
int b = 8000
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
pinMode(pushButton, INPUT); // 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
}
void loop() {
if (pushButton == HIGH) // Check for the opposite of what your button will normally be
int catch_a_stick = random(8); //this randomly selects a number between 0 and 7
delay(b);
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);
delay(a);
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);
delay(a);
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);
delay(a);
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);
delay(a);
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);
delay(a);
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);
delay(a);
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);
delay(a);
digitalWrite(stick8, LOW);
break;
}
}
