Arduino Dice problems

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;
  }
}

Your topic was MOVED to its current forum category as it is more suitable than the original

case 1:; 

That semicolon should not be there

Thank you, but the 2 random outputs form one input still remains :frowning:

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.

It didn't change the behaviour. Although unusual, it is harmless.

ezbutton used the way you have written it sets the pinMode to INPUT_PULLUP, so neither is that your problem.

a7

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.

a7

Well i made the delay 10000ms and it still gives out 2 outputs

Please supply a schematic of your project.

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.

Test it yourself with this loop:


void loop()
{
  button.loop();
  
  Serial.println(button.isPressed());
  delay(300);
}

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!");
  
}

So yet a mystery.

HTH

a7

Here is the wokwi schematic, thats how i have it at home noProblem.ino - Wokwi ESP32, STM32, Arduino Simulator

Ummm, I drew that, you know.

I am interested in your wiring, if you mean you have the same exact circuit the mystery deepens.

What kind of button are you using? It might be an incredibly bad pushbutton, don't thinks so, clutching for straw here.

a7

The circuit is 100% the same. And on the loop you sent me it works just fine...

Impossible! <- said with French accent.

I can't wait to see WTF is going on for you over there.

a7

OK nap time.

I am seeing the undesirable behaviour!

It is a fault of ezButton - it really does need to be called more often than your block code will allow.

So you gotta figure out how to let that happen. There are easy hackish ways, and there are proper techniques.

I think "arduino blink without delay" and "arduino two things at once" might be good google food.

CU

a7

1 Like

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 :smiley:

#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:

#include <ezButton.h>

ezButton button(2);

  button.setDebounceTime(50);
  
  button.loop();

The pinMode is INPUT at reset, you may want to consider adding

pinMode(2, INPUT_PULLUP)

to your code depending on how your button is wired.

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.