Program for electronical dice doesn't work

this is my first self written code and it doesn't work, can anyone help me.
Another code it does work on the same hardware, so my hardware works.

int Pin31 = 0;
int Pin32 = 1;
int Pin33 = 2;
int Pin34 = 3;
int Button1 = 6;
int Button2 = 7;
int statk1;
int statk2;
int r;



void setup() {
  pinMode(Pin31, OUTPUT);
  pinMode(Pin32, OUTPUT);
  pinMode(Pin33, OUTPUT);
  pinMode(Pin34, OUTPUT);
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
}

void loop() {
  statk1 = digitalRead(Button1);
  statk2 = digitalRead(Button2);
  if(statk2 == HIGH){
    off();
  }
  if(statk1 == HIGH){
    Shuffle();
    r = random (1,7);
    statk1 = digitalRead(Button1);
    if (statk1 == HIGH){
      six();
    }
   else{
    if(r==1){
      one();
    }else if(r==2){
      two();
    }else if(r==3){
      three();
    }else if(r==4){
      four();
    }else if(r==5){
      fife();
    }else if(r==6){
      six();
    }
   }
  }
}

void Shuffle() {
  one();
  delay(100);
  off();
  delay(100);
  two();
  delay(100);
  off();
  delay(100);
  three();
  delay(100);
  off();
  delay(100);
  four();
  delay(100);
  off();
  delay(100);
  fife();
  delay(100);
  off();
  delay(100); 
  six();
  delay(100);
  off();
  delay(100);
}

void off(){
  pinMode(Pin31, LOW);
  pinMode(Pin32, LOW);
  pinMode(Pin33, LOW);
  pinMode(Pin34, LOW);
}

void one(){
  digitalWrite (Pin31, HIGH);
}
void two(){
  digitalWrite (Pin32, HIGH);
}
void three(){
  digitalWrite (Pin31, HIGH);
  digitalWrite (Pin32, HIGH);
}
void four(){
  digitalWrite (Pin32, HIGH);
  digitalWrite (Pin33, HIGH);
}
void fife(){
  digitalWrite (Pin32, HIGH);
  digitalWrite (Pin33, HIGH);
  digitalWrite (Pin31, HIGH);
}
void six(){
  digitalWrite (Pin32, HIGH);
  digitalWrite (Pin31, HIGH);
  digitalWrite (Pin33, HIGH);
}

Welcome to the forum

What does that mean ?

What should it do ?
What does it do instead ?

1 Like

instead of pinMode(), shouldn't these be digitalWrite()?

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW and may need to be debounced (e.g. delay (20):wink:

these should be INPUT_PULLUP

it does exactly nothing

i don't realy understand what you mean. i have my button that it is conected to the pin olso conected to ground with a 1k Ω resistor if that is what you mean

don't know how you connected the resistor. if the switch is connected between the pin and ground the resistor should be between the pin and 5V

if the button pulls the pin the ground, it makes the pin LOW when pressed. looks like your logic expects it to he HIGN

if you enable the internal pullup resistor, INPUT_PULLUP, there's no need for an external resistor

connecting momentary switch to Arduino pin. From my tutorial >> State change detection for active LOW inputs

Are your LEDs connected correctly?

Your code appears to expect the LEDs to light when the pin they are connected to is HIGH. So the anode should be connected to the pin and the cathode to ground. Plus a series resistor, of course. The resistor can be between the pin and the anode or between the cathode and ground.

my leds are conected corectly because with another program that i haven't witten myself it works

Ok. Have you also used another program to test your buttons are connected correctly?

Are you Scottish, by any chance?

the problem is

LOW is 0 and configure the pin as INPUT

1 Like

@gcjr got it.

off() is called every time, undoing the effect of

Yes

No from the Netherlands, but can't write English

Hi @daniel1 ,

Welcome to the forum..

I simmed your project..
Seems to work??
I did move the led pins up 2 pins, got off the rx and tx pins..
fixed the pin modes in the off function too..

UnoBinDice Simmed..

good luck.. ~q

yes it works

thank you all so much for the help with my project

1 Like

I made different LEDs light up:

void one(){
  digitalWrite (Pin31, HIGH);
}
void two(){
  digitalWrite (Pin32, HIGH);
}
void three(){
  digitalWrite (Pin32, HIGH);
  digitalWrite (Pin31, HIGH);
}
void four(){
  digitalWrite (Pin33, HIGH);
}
void fife(){
  digitalWrite (Pin33, HIGH);
  digitalWrite (Pin31, HIGH);
}
void six(){
  digitalWrite (Pin33, HIGH);
  digitalWrite (Pin32, HIGH);
}
void zeven(){
  digitalWrite (Pin33, HIGH);
  digitalWrite (Pin32, HIGH);
  digitalWrite (Pin31, HIGH);
}

a7

Ok. Fife is a place in Scotland which is spelled similar to five. Thought it might be a little joke.

Maybe it was a little Jock :grinning:

2 Likes