Arduino binary timer

hello, everyone i'm new to using a arduino and c++ i have followed a tutorial where 4 LED's count from 0 too 15 and that starts and stops when you click a button but the button doesn't work. can someone explain what i am doing wrong

// C++ code
//

int start=0;

int buttonState = 0;
int count = 0;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(12,INPUT);
}


void loop()
{
  digitalWrite (2,LOW); digitalWrite (4,LOW); digitalWrite (6,LOW); digitalWrite (8,LOW);
  buttonState = digitalRead(2);
  if(buttonState == LOW) {}
  else{
 if(start==0)
 { 
  digitalWrite (2,LOW); digitalWrite (4,LOW); digitalWrite (6,LOW); digitalWrite (8,LOW);
   delay(1000);
 }
  else if(start==1)
 { 
  digitalWrite (2,LOW); digitalWrite (4,LOW); digitalWrite (6,LOW); digitalWrite (8,HIGH);
    delay(1000);
 }
 else if(start==2)
 { 
   digitalWrite (2,LOW); digitalWrite (4,LOW); digitalWrite (6,HIGH); digitalWrite (8,LOW);
   delay(1000);
 }
  else if(start==3)
 { 
  digitalWrite (2,LOW); digitalWrite (4,LOW); digitalWrite (6,HIGH); digitalWrite (8,HIGH);
    delay(1000);
 }
 else if(start==4)
 { 
   digitalWrite (2,LOW); digitalWrite (4,HIGH); digitalWrite (6,LOW); digitalWrite (8,LOW);
   delay(1000);
 } 
   else if(start==5)
 { 
 digitalWrite (2,LOW); digitalWrite (4,HIGH); digitalWrite (6,LOW); digitalWrite (8,HIGH);
     delay(1000);
 } 
   else if(start==6)
 { 
 digitalWrite (2,LOW); digitalWrite (4,HIGH); digitalWrite (6,HIGH); digitalWrite (8,LOW);
     delay(1000);
 } 
    else if(start==7)
 { 
 digitalWrite (2,LOW); digitalWrite (4,HIGH); digitalWrite (6,HIGH); digitalWrite (8,HIGH);
      delay(1000);
 } 
    else if(start==8)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,LOW); digitalWrite (6,LOW); digitalWrite (8,LOW);
      delay(1000);
 } 
    else if(start==9)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,LOW); digitalWrite (6,LOW); digitalWrite (8,HIGH);
      delay(1000);
 } 
    else if(start==10)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,LOW); digitalWrite (6,HIGH); digitalWrite (8,LOW);
      delay(1000);
 } 
    else if(start==11)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,LOW); digitalWrite (6,HIGH); digitalWrite (8,HIGH);
      delay(1000);
 } 
  else if(start==12)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,HIGH); digitalWrite (6,LOW); digitalWrite (8,LOW);
    delay(1000);
 }   
   else if(start==13)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,HIGH); digitalWrite (6,LOW); digitalWrite (8,HIGH);
     delay(1000);
 }  
    else if(start==14)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,HIGH); digitalWrite (6,HIGH); digitalWrite (8,LOW);
      delay(1000);
 } 
    else if(start==15)
 { 
 digitalWrite (2,HIGH); digitalWrite (4,HIGH); digitalWrite (6,HIGH); digitalWrite (8,HIGH);
      delay(1000);
 } 
    start=start+1;
  }

And how do you have the button wired?

so lets say start is a 0. The code runs very fast so the user has only a short time to press the button before the program puts the CPU to sleep for 1 second. During the sleep time the button pressing is not being registered. The user/operator has some microseconds to press the button before the thing goes to sleep.

And is the button on pin2 or pin 12?

 buttonState = digitalRead(2); ,<<<<<<<<<<reads pin 2

which has been defined as an output, pinMode(2, OUTPUT);.

Your frizzy picture explains it all, you do not have the button hooked up. You should read or watch a tutorial on microprocessor input and outputs. The pins are referenced from the processor so inputs is where data comes in, outputs is where data goes out. Some can be either then it is the software that defines the inputs and outputs. Defining a pin as an output and connecting a switch to it potentially could destroy the port.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

Maybe it is a quantum button

need to recognize a button press, that it changed state
consider

// simple button processing

const byte butPin = A1;
byte       ledPin = 13;

byte butState;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            digitalWrite (ledPin, ! digitalRead (ledPin));
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, Off);
    pinMode      (ledPin, OUTPUT);

    pinMode     (butPin, INPUT_PULLUP);
    butState  = digitalRead (butPin);
}

consider
avoid redundant code, let computer work for you
use millis() so button presses is detected immediately, not after delay() completes

#undef MyHW
#ifdef MyHW
const byte ButPin  = A1;
const byte LedPins [] = { 10, 11, 12, 13 };

enum { Off = HIGH, On = LOW };

#else
const byte ButPin  = 12;
const byte LedPins [] = { 2, 4, 6, 8 };

enum { Off = LOW, On = HIGH };

#endif

#define Nled  sizeof(LedPins)

byte butState;
byte mode;
byte cnt;

unsigned long Period = 1000;
unsigned long msecLst;
unsigned long msec;

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (ButPin);

    if (butState != but)  {     // check that button state changed
        butState = but;
        delay (10);         // debounce

        if (LOW == but)     // button pressed
            mode = !mode;
    }

    if (mode)  {
        msec = millis ();
        if ((msec - msecLst) > Period)  {
            msecLst = msec;

            cnt++;
            if (16 <= cnt)
                cnt = 0;

            byte mask = cnt;
            for (int n = 0; n < 4; n++)  {
                if (mask % 2)
                    digitalWrite (LedPins [n], On);
                else
                    digitalWrite (LedPins [n], Off);
                mask /= 2;
            }
        }
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < Nled; n++)  {
        digitalWrite (LedPins [n], Off);
        pinMode      (LedPins [n], OUTPUT);
    }

    pinMode     (ButPin, INPUT_PULLUP);
    butState  = digitalRead (ButPin);
}

Hello usename2002

Take a view to this small Q&D example:

constexpr byte Leds[] {2, 3, 4, 5}; // port pins for LEDs 
constexpr byte Pow2[] {1, 2, 4, 8}; // power of two 
constexpr byte Knop {A0};           // port pin for knop 

void setup() {
  for (auto led : Leds) pinMode(led, OUTPUT);
  pinMode (Knop, INPUT_PULLUP);
}
void loop() {
  static byte counter = 0;
  static int stateOld = false;
  delay(20); // delay for lazy debouncing :) 
  int stateNew = !digitalRead(Knop);
  if (stateOld != stateNew)
  {
    stateOld = stateNew;
    if (stateNew)
    {
      counter++;
      for (int pow2=0;pow2<=3;pow2++) digitalWrite(Leds[pow2], counter & Pow2[pow2]);
    }
  }
}

Or, the LED on pin 2.

Does OP's program really put the CPU/MCU into sleep mode?

No.