Leds not turning on :(

I have a problem. I recently made a sketch meant to show tiny animations on 4 LEDs by pressing different buttons, one of the buttons changes the speed that the animations go.
The LEDs are then, if the ''animation-button'' is pressed, supposed to light up one after one, depending on ho high the speed is. The problem is that the buttons dont light up.
The pins 2-5 are output-pins (LEDs) and the pins 6-9 are input-pins (pushbuttons)

I know that the code can get better, even in the parts that work, but here is the code:


int buttn1 = 0;
int buttn2 = 0;
int buttn3 = 0;
int buttn4 = 0;
int blinkspeed = 80;
int mode = 0;
void setup() {
  pinMode(2, OUTPUT);  
  Serial.begin(9600);
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT);  
  pinMode(5, OUTPUT); 
  pinMode(6, INPUT);  
  pinMode(7, INPUT); 
  pinMode(8, INPUT);  
  pinMode(9, INPUT); 
    digitalWrite(2, HIGH); //LEDs blink so user know that the sketch has started
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    delay(500);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    delay(500);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
}

void loop(){  //
buttn1 = digitalRead(6);
buttn2 = digitalRead(7);
buttn3 = digitalRead(8);
buttn4 = digitalRead(9);
Serial.print(" mode: ");// Values printed to the serial monitor to know that values has changed
Serial.print(mode);
Serial.print("  blinkspeed ");
Serial.println(blinkspeed);
if (buttn1 == HIGH){ //Mode is set to the button that is pressed down
(mode = 1);
}else{
if (buttn2 == HIGH){
(mode = 2);
}else{
if (buttn3 == HIGH){
(mode = 3);
}else{
if (buttn4 == HIGH){
(mode = 4);
}else{
  (mode = 0); //Mode 0 is added to know that no buttons are pressed
         }
        }
      }
    }
switch(mode){
  case 0:
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  break;
  case 1:
  digitalWrite(2, HIGH);//animation one (is there a way to animate without taking up so much space?)
  delay(blinkspeed);
  digitalWrite(2,LOW);
  digitalWrite(3,HIGH);
   delay(blinkspeed);
  digitalWrite(3,LOW);
  digitalWrite(4,HIGH);
   delay(blinkspeed);
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  delay(blinkspeed);
    digitalWrite(5,LOW);
  
case 2://animations not made yet
break;
case 3:
break;
case 4:
if (buttn4 == HIGH){//changing speed 
(blinkspeed = blinkspeed + 40);
delay(1000);
if (blinkspeed == 240){//when limit reached, speed set lo least
  (blinkspeed = 40);
  delay(1000);

//code problem here 
if (blinkspeed == 40){ //LEDs light up in order to show animation-speed
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);

`1. * Listobjekt`

}else{//
if (blinkspeed == 80){//
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}else{//
if (blinkspeed == 120){//
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}else{//
if (blinkspeed == 160){//
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}else{
if (blinkspeed == 200){//
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
break;
         }
        }
      }
    }
  }
}
}
}
}

The first thing I can see is that your buttons are connected between UNOs pins and GND. That means D6, D7, D8 and D9 should be pinMode set to INPUT_PULLUP and all the button readings detect LOW instead of HIGH.

You should make it a habit to press ctrl-T once every three minutes
or after writing five lines of code

Are you sure that your code-logic is working correctly by nesting all the if and else if -conditions

After pressing ctrl-T for autoformatting your code this can be seen

int buttn1 = 0;
int buttn2 = 0;
int buttn3 = 0;
int buttn4 = 0;
int blinkspeed = 80;
int mode = 0;
void setup() {
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  digitalWrite(2, HIGH); //LEDs blink so user know that the sketch has started
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  delay(500);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
}

void loop() { //
  buttn1 = digitalRead(6);
  buttn2 = digitalRead(7);
  buttn3 = digitalRead(8);
  buttn4 = digitalRead(9);
  Serial.print(" mode: ");// Values printed to the serial monitor to know that values has changed
  Serial.print(mode);
  Serial.print("  blinkspeed ");
  Serial.println(blinkspeed);
  if (buttn1 == HIGH) { //Mode is set to the button that is pressed down
    (mode = 1);
  } else {
    if (buttn2 == HIGH) {
      (mode = 2);
    } else {
      if (buttn3 == HIGH) {
        (mode = 3);
      } else {
        if (buttn4 == HIGH) {
          (mode = 4);
        } else {
          (mode = 0); //Mode 0 is added to know that no buttons are pressed
        }
      }
    }
  }
  
  switch (mode) {
    case 0:
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      break;
    case 1:
      digitalWrite(2, HIGH);//animation one (is there a way to animate without taking up so much space?)
      delay(blinkspeed);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      delay(blinkspeed);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      delay(blinkspeed);
      digitalWrite(4, LOW);
      digitalWrite(5, HIGH);
      delay(blinkspeed);
      digitalWrite(5, LOW);

    case 2://animations not made yet
      break;
    case 3:
      break;
    case 4:
      if (buttn4 == HIGH) { //changing speed
        (blinkspeed = blinkspeed + 40);
        delay(1000);
        if (blinkspeed == 240) { //when limit reached, speed set lo least
          (blinkspeed = 40);
          delay(1000);

          //code problem here
          if (blinkspeed == 40) { //LEDs light up in order to show animation-speed
            digitalWrite(2, LOW);
            digitalWrite(3, LOW);
            digitalWrite(4, LOW);
            digitalWrite(5, LOW);

            //1. * Listobjekt`

          } else { //
            if (blinkspeed == 80) { //
              digitalWrite(2, HIGH);
              digitalWrite(3, LOW);
              digitalWrite(4, LOW);
              digitalWrite(5, LOW);
            } else { //
              if (blinkspeed == 120) { //
                digitalWrite(2, HIGH);
                digitalWrite(3, HIGH);
                digitalWrite(4, LOW);
                digitalWrite(5, LOW);
              } else { //
                if (blinkspeed == 160) { //
                  digitalWrite(2, HIGH);
                  digitalWrite(3, HIGH);
                  digitalWrite(4, HIGH);
                  digitalWrite(5, LOW);
                } else {
                  if (blinkspeed == 200) { //
                    digitalWrite(2, HIGH);
                    digitalWrite(3, HIGH);
                    digitalWrite(4, HIGH);
                    digitalWrite(5, HIGH);
                    break;
                  }
                }
              }
            }
          }
        }
      }
  }
}

with comments

  if (buttn1 == HIGH) { //Mode is set to the button that is pressed down
    (mode = 1);
  } else { // only in case if (buttn1 == HIGH) is false 
    if (buttn2 == HIGH) { // <= check this
      (mode = 2);
    } else { // only in case if (buttn2 == HIGH) is false 
      if (buttn3 == HIGH) { // <== check this
        (mode = 3);
      } else { // only in case if (buttn3 == HIGH) is false 
        if (buttn4 == HIGH) { //<== check this
          (mode = 4);
        } else {
          (mode = 0); // Mode 0 is added to know that no buttons are pressed
        }
      }
    }
  }

*My bad, I saw that the schematic that i got as a gift is incorrect,the buttons power source is the 5 volt pin and not the digital pins.

with free hand pen and paper draw the wiring as it REALLY is
take a picture of the paper and post this picture

There is no such thing as a "power-source" for buttons.

Buttons like that need no electric "power"

The buttons open/close a mechanical contact
In case you code your code uses input-mode "INPUT_PULLUP" instead of inputmode "INPUT"

  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

opening/closing the contact connects the IO-pin to
button unpressed = contact opened IO-pin is connected to +5V through the internal pullup-resistor reading "HIGH"
or
button pressed = contact close IO-pin is connected to GND reading "LOW"

  • We suggest wiring switches as S3 is wired in the image below.

Why do not I wire the button from the 5 volt pin to a digital pin? (I have wired the buttons from 5 volt pin to ground with a 10kΩ resistor and to digital pin 6-9) I use a solution from the arduino starter kit projects book,project 02, spaceship interface.

If that translates that you have buttons wired between 5V and digital pin with 10k pulldowns, that's ok. But if you don't mention that, it's becoming guess work for others... That's why you should demonstrate your Real wiring.

  • It is best and easiest to wire switches from an input pin to GND.
    In setup ( ), we turn on the internal pull-up resistor using INPUT_PULLUP in the pinMode line(s) of code, no external resistor to GND is needed.
    If we see a LOW when we read the switch, that switch is closed, if we see a HIGH that switch is opened.

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