The following circuit and code shows an example of a decision maker made with LEDs. Pushing the button will make the LEDs flash in a random order for 1 second. Then displaying the random decision where one LED lights up for 3 seconds and then reseting.
-
int timeShowRandom = 1000;
-
int timeShowDecision = 3000;
-
int timeBlink = 50;
-
int buttonPin = 3;
-
-
int buttonPress = false;
-
int randomNumber;
-
int previousNo = 0;
-
int timePassed = 0;
-
-
void setup() {
-
// Set button pin
-
pinMode(buttonPin, INPUT);
-
// Set output pins
-
pinMode(12, OUTPUT);
-
pinMode(11, OUTPUT);
-
pinMode(10, OUTPUT);
-
-
}
-
-
void getRandomNo() {
-
int rand = random(10,13);
-
if(rand == previousNo) {
-
getRandomNo();
-
} else {
-
randomNumber = rand;
-
previousNo = randomNumber;
-
}
-
}
-
-
void loop() {
-
// Check if button is pressed
-
if(digitalRead(buttonPin) == HIGH && buttonPress == false) {
-
buttonPress = true;
-
} if(buttonPress == true && timePassed <= timeShowRandom) {
-
getRandomNo(); // Get random pin number
-
digitalWrite(randomNumber, HIGH);
-
delay(timeBlink);
-
digitalWrite(randomNumber, LOW);
-
delay(timeBlink);
-
timePassed = timePassed + (timeBlink * 2);
-
} else if(buttonPress == true) {
-
digitalWrite(random(10,13), HIGH); // Set random pin on
-
delay(timeShowDecision); // For x seconds
-
buttonPress = false; // Set button to be enabled again
-
timePassed = 0;
-
} else {
-
// Reset all output pins
-
digitalWrite(10, LOW);
-
digitalWrite(11, LOW);
-
digitalWrite(12, LOW);
-
}
-
}
I try to build the dicision maker but it doest work. The switch is not working. When i take the black wire from the resitor loose the in works. What could be the problem?
@jorg What switch are you using? You can you supply me with a sketch on your circuit?
Nice documented example
.
One thought on the design:
I would not use pin 13 because pin 13 already has a led and resistor. This is why the led you connected on pin 13 is less bright than the others.
Best regards
Jantje
@Jantje Thanks for the tip! I have changed the design to not use pin 13 now!
Hi Ellen.
This can’t work.
You need to change your Fritzing sketch, as A0 will always be 0.
It is fixed to GND.
That has nothing to do with the type of switch that has been used.
So connect A0 to the opposite side of the resistor (where that connects to the button), then ik will work (but still too complicated).
Or add an extra resistor between the existing one and the switch and connect A0 there.
Our attention (jantje and mine) was drawn here because we got questions about this in the Arduino forum.
Hi MAS3! I have updated the sketch and code now so it will work.
its fixed now. It works like a charm.