Can't get the code to work properly.

I have been working on my Arduino Lab for the last few days and I have virtually everything that I need done but I'm running into an issue. I am supposed to set this program up so that when I press one button any number of times if it is 15 or less it will display as a binary code on 4 leds when I press a second button. If it is over 15 clicks when I press the second button it does a ping pong effect to represent overflow. The issue Im running into is that after I press the second button the program should essentially reset so that the next number of clicks on button 1 is what will be displayed in binary. However the way it is, the second number of clicks on button 1 is added to the first resulting in the two numbers being added and displayed that way. I have started from scratch on this program over and over and over and tried it several different ways. At this point I have got it pretty much where I need it but no matter where I try to reset the value for the number of times the first button has been pressed it stops all of the different leds from lighting up.

sketch_oct31a.ino (2.64 KB)

I have been looking up all sorts of youtube tutorials and that was from one of them. And thats the issue Im running into. No matter where i put presses = 0, the second it goes in no led will light up.

Compiles, not tested. If it works, compare to your code.

// Variables
const byte button1 = 2;
const byte button2 = 3;
int presses = 0;
const byte numPins = 4;
const byte ledPins[] = {5, 6, 7, 8};

byte
    lastB1,
    lastB2;

// Void Setup()
void setup() 
{
    // Inputs
    pinMode( button1, INPUT_PULLUP );
    pinMode( button2, INPUT_PULLUP );
    lastB1 = digitalRead( button1 );
    lastB2 = digitalRead( button2 );

    // Outputs
    for( int i=0; i<numPins; i++ )
        pinMode(ledPins[i], OUTPUT);
    
    presses = 0;
    
}//setup

void loop() 
{
    static unsigned long
        tRead=0;
    unsigned long
        tNow;
    byte
        nB;

    //read the buttons every 50mS
    tNow = millis();
    if( tNow - tRead < 50ul )
        return;

    tRead = tNow;
    nB = digitalRead( button1 );
    if( nB != lastB1 )
    {
        lastB1 = nB;
        if( nB == LOW )
            presses++;
    }//if

    nB = digitalRead( button2 );
    if( nB != lastB2 )
    {
        lastB2 = nB;
        if( nB == LOW )            
        {
            ShowResult();
            presses = 0;
            
        }//if
        
    }//if

}//loop

void ShowResult( void )
{
    int
        index;
        
    if( presses <= 15 )
    {
        for( int i=0; i<numPins; i++ )
            digitalWrite( ledPins[i], (presses & (1<<i))?HIGH:LOW );
            
    }//if
    else
    {
        //basically a copy of your original code
        for(index = 0; index<numPins; index++ )
        {
            digitalWrite(ledPins[index], HIGH);             // turn LED on
            delay(100);                                     // pause to slow down
            digitalWrite(ledPins[index], LOW);              // turn LED off
            delay(100);                                     // pause to slow down
            
        }//for
        
        for(index=0; index<numPins; index++ )
        {
            digitalWrite(ledPins[numPins-index-1], HIGH);   // turn LED on
            delay(100);                                     // pause to slow down
            digitalWrite(ledPins[numPins-index-1], LOW);    // turn LED off
            delay(100);                                     // pause to slow down
            
        }//for
        
    }//else
    
}//ShowResult

Hi,
How have you got your buttons wire?
Between digital input and 5V, with a 10K resistor from digital input to gnd?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: