Hello, im making an arduino dice as my 2nd project. It works except for 1 thing: ´the random function changes 2 times when i press the button. I can't figure it out. Thanks in advance!
#include <ezButton.h>
#define SWITCH_OFF 0
#define SWITCH_ON 1
ezButton button(2);
int switch_state = SWITCH_OFF;
const int VAL1 = 13;
const int VAL2 = 12;
const int VAL3 = 11;
const int VAL4 = 10;
const int VAL5 = 9;
const int VAL6 = 8;
int randomOutput;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50);
pinMode(VAL1, OUTPUT);
pinMode(VAL2, OUTPUT);
pinMode(VAL3, OUTPUT);
pinMode(VAL4, OUTPUT);
pinMode(VAL5, OUTPUT);
pinMode(VAL6, OUTPUT);
}
void loop() {
button.loop();
if (button.isPressed()) switch_state = !switch_state;
if(switch_state == SWITCH_ON){
randomOutput = random(1, 7);
Serial.println(randomOutput);
switch (randomOutput) {
case 1:; {
digitalWrite(VAL1, HIGH);
}
break;
case 2:;
digitalWrite(VAL2, HIGH);
break;
case 3:;
digitalWrite(VAL3, HIGH);
break;
case 4:;
digitalWrite(VAL4, HIGH);
break;
case 5:;
digitalWrite(VAL5, HIGH);
break;
case 6:;
digitalWrite(VAL6, HIGH);
break;
}
delay(1000);
digitalWrite(VAL1, LOW);
digitalWrite(VAL2, LOW);
digitalWrite(VAL3, LOW);
digitalWrite(VAL4, LOW);
digitalWrite(VAL5, LOW);
digitalWrite(VAL6, LOW);
switch_state = SWITCH_OFF;
} else {
switch_state = SWITCH_OFF;
}
}
This means as long as you hold the button down you will keep on creating random numbers every second it is held down.
Look at the IDE example "State change detection" example code that is in the IDE.
As @Grumpy_Mike suggests, you need to see when the button changes, not when it is.
edit: ezButton appears to do this for you, isPressed() functions like "got pressed", one report true for one button press event, no matter you hold it down.
A simple way to test that is to make your press of the button short relative to the time it takes before it gets checked again.
So... either make the delay(1000) quite a bit bigger if you can't keep the press to under 1 second.
Or press quicker.
Tell us what happens when you either briefly stab the button, or give yourself time for what it seems must be very leisurely pressing on your part.
Your code, unmodified, runs fine in the wokwi, see it here:
Even better, it seems that ezButton lives up to its name. It does not continue to report isPressed as true but once per button got pressed... despite what is claimed earlier.
which prints 0s and if you press and hold the button, a single 1, then back to 0s.
Or this
void loop()
{
button.loop();
if (button.isPressed()) Serial.println("button went down!");
if (button.isReleased()) Serial.println("button went up!");
}
Allright today i didn't manage to get rid of ezButton library, but it works! Im so happy and thakful for your help. Here's the code:
Once again thaks a7
#include <ezButton.h>
#define SWITCH_OFF 0
#define SWITCH_ON 1
ezButton button(2);
int switch_state = SWITCH_OFF;
const int VAL1 = 13;
const int VAL2 = 12;
const int VAL3 = 11;
const int VAL4 = 10;
const int VAL5 = 9;
const int VAL6 = 8;
int randomOutput;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50);
pinMode(VAL1, OUTPUT);
pinMode(VAL2, OUTPUT);
pinMode(VAL3, OUTPUT);
pinMode(VAL4, OUTPUT);
pinMode(VAL5, OUTPUT);
pinMode(VAL6, OUTPUT);
}
void loop() {
button.loop();
switch_state = digitalRead(2);
if(switch_state == 0){
randomOutput = random(1, 7);
Serial.println(randomOutput);
switch (randomOutput) {
case 1: {
digitalWrite(VAL1, HIGH);
}
break;
case 2:
digitalWrite(VAL2, HIGH);
break;
case 3:
digitalWrite(VAL3, HIGH);
break;
case 4:
digitalWrite(VAL4, HIGH);
break;
case 5:
digitalWrite(VAL5, HIGH);
break;
case 6:
digitalWrite(VAL6, HIGH);
break;
}
delay(1000);
digitalWrite(VAL1, LOW);
digitalWrite(VAL2, LOW);
digitalWrite(VAL3, LOW);
digitalWrite(VAL4, LOW);
digitalWrite(VAL5, LOW);
digitalWrite(VAL6, LOW);
switch_state = SWITCH_OFF;
} else {
switch_state = SWITCH_OFF;
}
}
@iwanttolearnarduino you kinda sorta did, as you are not using ezButton's functionality in your code…
But it works, so… yay!
Try holding the button down for 5 or ten seconds, and you'll see why you got the kind of advices you did, and it will inform your further study and learning.
You can lose these lines… and be rid of the library: