I think I'm missing something

Hello everyone
I think I'm a bit rusty but the following code works except for one part:
I cannot return to the original value of count, it is highlighted in the code section.
Basically my circuit is a test circuit for a 7 segment display being controlled by a push-button switch. Here is the code:

int e =  13;
int g =  12;
int c =  11;
int d =  10;
int b  =   9;
int a  =   8;
int f  =   7;
int s;
int x;
int count;
int btt = 2;
int ledState = HIGH;         
int lastButtonState = LOW;
long lastDebounceTime = 0;  
long debounceDelay = 50;


void setup()   
{                
  
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(btt,INPUT);
   
}



void loop()                     
{
   
  long reading=LOW;
  int s = digitalRead(btt);
  
  if (s != lastButtonState)
  {

    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    s = reading;
  }
  
  lastButtonState = reading;
  
  
  if (s==HIGH)
  {
      count++;
      }
      
     
   if (count==0)   // this is where I want to return after the last push of the button
    {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
      return;
    }
    
    else if (count==1)
    {
      digitalWrite(a, LOW);
      digitalWrite(b, LOW);
      digitalWrite(c, LOW);
      digitalWrite(d, LOW);
      digitalWrite(e, LOW);
      digitalWrite(f, LOW);
      
      
      if (x!=5)
      {
          digitalWrite(b, HIGH);    
          delay(50);
          digitalWrite(b, LOW);    
          delay(50);
          x++;  
          digitalWrite(c, HIGH);
          delay(50);
          digitalWrite(c, LOW);    
          delay(50);
          x++;  
          digitalWrite(d, HIGH);   
          delay(50);
          digitalWrite(d, LOW);    
          delay(50);
          x++;
          digitalWrite(e, HIGH);   
          delay(50);
          digitalWrite(e, LOW);    
          delay(50);
          x++;  
          digitalWrite(f, HIGH);   
          delay(50);
          digitalWrite(f, LOW);    
          delay(50);
          x++;
          }
          
            else if (x==5)
            {
                digitalWrite(f, HIGH);   
                delay(50);            
                digitalWrite(f, LOW);    
                delay(50);
                x--;  
                digitalWrite(e, HIGH);    
                delay(50);
                digitalWrite(e, LOW);    
                delay(50);
                x--;  
                digitalWrite(d, HIGH);
                delay(50);
                digitalWrite(d, LOW);    
                delay(50);
                x--;  
                digitalWrite(c, HIGH);   
                delay(50);
                digitalWrite(c, LOW);    
                delay(50);
                x--;
                digitalWrite(b, HIGH);   
                delay(50);
                digitalWrite(b, LOW);    
                delay(50);
                x--; 
              } 
             return; 
   
    }
   
        else if (count==2)
        {        
        digitalWrite(f,HIGH);
        digitalWrite(c,HIGH);
        delay(100);
        digitalWrite(b,HIGH);
        digitalWrite(e,HIGH);
        delay(100);
        digitalWrite(f,LOW);
        digitalWrite(c,LOW);
        delay(50);
        digitalWrite(b,LOW);
        digitalWrite(e,LOW);
        delay(50);
        return;
        }       
              
        else if (count==3)
        {
          
          digitalWrite(a,HIGH);
          delay(50);
          digitalWrite(a,LOW);
          delay(50);
          digitalWrite(g,HIGH);
          delay(50);
          digitalWrite(g,LOW);
          delay(50);
          digitalWrite(d,HIGH);
          delay(50);
          digitalWrite(d,LOW);
          delay(50);
          return;
        }
      
          else if (count==4)
        { 
          digitalWrite(a, HIGH);
          digitalWrite(b, HIGH);
          digitalWrite(c, HIGH);
          digitalWrite(d, HIGH);
          digitalWrite(e, HIGH);
          digitalWrite(f, HIGH);
          return;
        } 
        
        else if (count==5)
        {
          digitalWrite(f,HIGH);
          delay(20);
          digitalWrite(f,LOW);
          delay(20);
          digitalWrite(g,HIGH);
          delay(20);
          digitalWrite(g,LOW);
          delay(20);
          digitalWrite(c,HIGH);
          delay(20);
          digitalWrite(c,LOW);
          delay(20);
          digitalWrite(d,HIGH);
          delay(20);
          digitalWrite(d,LOW);
          delay(20);
          digitalWrite(e,HIGH);
          delay(20);
          digitalWrite(e,LOW);
          delay(20);
          digitalWrite(g,HIGH);
          delay(20);
          digitalWrite(g,LOW);
          delay(20);
          digitalWrite(b,HIGH);
          delay(20);
          digitalWrite(b,LOW);
          delay(20);
          digitalWrite(a,HIGH);
          delay(20);
          digitalWrite(a,LOW);
          delay(20);
          return;
        }
          else 
        {
          count=0;
          return;
        }
          
      
}

I tried inserting int count=0; both outside the if statement and inside but its just becomes unstable

Any help is greatly appreciated, I have prior experience in programming however I haven't touched any coding for the last 3 years so I'm a bit rusty :-[

Thank you in advance

Before digging into your code a quick question, have you wired a pull-down resistor for the switch input? If not that would cause unstable reading of the push button switch.

Lefty

yes switch is connected to pin 2 then to a 10k resistor to GND.

x==0; //equal to 0

deSilva: Normally you see the opposite (assignment in an if-then); I think this is the first time I have seen an unused comparison outside an if-then as a bug in code.

I wonder if this is a coding mistake caused by exposure to BASIC (where assignment and comparison both use the equality operator =)? I can't think of another language off the top of my head that uses the same symbol for both; every language I can recall seems to use two different operators...

:slight_smile:

i understand what you are saying however if i assign it "correctly" i.e x=0; , that part of code doesn't function as intended. What this means that instead of going back and forward the segments just "chase" each other. However if I declare it using a comparison operator == it will function like I want it to. Im probably not being very efficient with my code as I would suspect a FOR function would be better. That said if you look at my IF ELSE IF functions all the section of code work flawlessly except for the last part where it just doesn't seem to want to go back to COUNT=0 and just skips that whole section, i.e Segments a-f dont light up. That section is only active once at reset.

The equivalent of doing what you are doing is simply not doing it - remove the line, it should function the same (as all the comparison is doing is the check, then throwing away the result - the variable isn't changed - or at least it shouldn't be!); if it doesn't function the same, then something very strange indeed is going on!

:slight_smile:

ok...your are correct , i took the line out and it functions properly. Thank you for making my code less bloated :wink:

now back to the original problem, any ideas on how to return to the initial state i.e count==0? like i said if i put it outside the IF it becomes unstable

if i put it outside the IF it becomes unstable

You need to explain where you put "it", what "it" is, and what "it becomes unstable" means.

sorry my bad:

by it I mean the count=0 statement and by outside i mean inside the void loop() but before the sequence of IF ELSE IF. Basically before if (s==HIGH). When I put it there, my sequence doesn't advance.

Hope this helps

Your code is difficult to read...

Is it correct that "reading" is always FALSE? That looks funny...

Hope this helps

No, not really. You modified your code. It still doesn't work. You still need help.

I bet you can figure out what you need to do.

If not, here's a hint.

POST YOUR CODE!

Look at the debounce example under Examples-->digital-->debounce

They put the long reading = 0 at the beginning before setup.

@ Paul , the code is posted in the first post......

Thanks BigOil I'll try that, i actually grafted the Debounce code from examples to my own code.

I tried inserting int count=0; both outside the if statement and inside but its just becomes unstable

@ Paul , the code is posted in the first post......

Precisely where in that code is the line to reset count to 0?

sorry I thought I posted the updated code. Here it is:

int e =  13;
int g =  12;
int c =  11;
int d =  10;
int b  =   9;
int a  =   8;
int f  =   7;
int s;
int x;
int count;
int btt = 2;
int ledState = HIGH;         
int lastButtonState = LOW;
long lastDebounceTime = 0;  
long debounceDelay = 50;
long reading=LOW; 
// LED connected to digital pin 13


// The setup() method runs once, when the sketch starts

void setup()   
{                
  // initialize the digital pin as an output:
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(btt,INPUT);
  
  
  
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                     
{
 
  
  int s = digitalRead(btt);
  
  if (s != lastButtonState)
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    s = reading;
  }
  
  lastButtonState = reading;
  
  if (s==HIGH)
  {
      count++;
  }
      
    
    
    
    if (count==0)
    {
      digitalWrite(a, HIGH);
      digitalWrite(b, HIGH);
      digitalWrite(c, HIGH);
      digitalWrite(d, HIGH);
      digitalWrite(e, HIGH);
      digitalWrite(f, HIGH);
    }
    
    else if (count==1)
    {
    
      
      if (x!=5)
      {
          digitalWrite(a,LOW);
          digitalWrite(b, HIGH);    
          delay(50);
          digitalWrite(b, LOW);    
          delay(5);
          x++;  
          digitalWrite(c, HIGH);
          delay(50);
          digitalWrite(c, LOW);    
          delay(5);
          x++;  
          digitalWrite(d, HIGH);   
          delay(50);
          digitalWrite(d, LOW);    
          delay(5);
          x++;
          digitalWrite(e, HIGH);   
          delay(50);
          digitalWrite(e, LOW);    
          delay(5);
          x++;  
          digitalWrite(f, HIGH);   
          delay(50);
          digitalWrite(f, LOW);    
          delay(5);
          x++;
          }
          
            else if (x==5)
            {
                digitalWrite(f, HIGH);   
                delay(50);            
                digitalWrite(f, LOW);    
                delay(5);
                x--;  
                digitalWrite(e, HIGH);    
                delay(50);
                digitalWrite(e, LOW);    
                delay(5);
                x--;  
                digitalWrite(d, HIGH);
                delay(50);
                digitalWrite(d, LOW);    
                delay(5);
                x--;  
                digitalWrite(c, HIGH);   
                delay(50);
                digitalWrite(c, LOW);    
                delay(5);
                x--;
                digitalWrite(b, HIGH);   
                delay(50);
                digitalWrite(b, LOW);    
                delay(5);
                x--;  
            }  
   
    }
   
        else if (count==2)
        {        
        digitalWrite(f,HIGH);
        digitalWrite(c,HIGH);
        delay(100);
        digitalWrite(b,HIGH);
        digitalWrite(e,HIGH);
        delay(100);
        digitalWrite(f,LOW);
        digitalWrite(c,LOW);
        delay(50);
        digitalWrite(b,LOW);
        digitalWrite(e,LOW);
        delay(50);
        }       
              
        else if (count==3)
        {
          
          digitalWrite(a,HIGH);
          delay(50);
          digitalWrite(a,LOW);
          delay(50);
          digitalWrite(g,HIGH);
          delay(50);
          digitalWrite(g,LOW);
          delay(50);
          digitalWrite(d,HIGH);
          delay(50);
          digitalWrite(d,LOW);
          delay(50);
        }
      
       
        
        else if (count==4)
        {
          digitalWrite(f,HIGH);
          delay(20);
          digitalWrite(f,LOW);
          delay(20);
          digitalWrite(g,HIGH);
          delay(20);
          digitalWrite(g,LOW);
          delay(20);
          digitalWrite(c,HIGH);
          delay(20);
          digitalWrite(c,LOW);
          delay(20);
          digitalWrite(d,HIGH);
          delay(20);
          digitalWrite(d,LOW);
          delay(20);
          digitalWrite(e,HIGH);
          delay(20);
          digitalWrite(e,LOW);
          delay(20);
          digitalWrite(g,HIGH);
          delay(20);
          digitalWrite(g,LOW);
          delay(20);
          digitalWrite(b,HIGH);
          delay(20);
          digitalWrite(b,LOW);
          delay(20);
          digitalWrite(a,HIGH);
          delay(20);
          digitalWrite(a,LOW);
          delay(20);
        }
          else
        {
          count=0;
          return;
        }
  
      
}

The thing you are missing is the proper de-bouncing of the button.

long reading=LOW;

The type for reading is wrong. The digitalRead function returns an int. There is no reason to waste space making reading a long. It should be an int.

  int s = digitalRead(btt);

  if (s != lastButtonState)
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

This part is good. The lastDebounce time is updated when the state changes.

  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    s = reading;
  }

This, on the other hand is garbage. If the button is not bouncing, set it to LOW.

  if (s==HIGH)
  {
      count++;
  }

Increment count while the button is bouncing. OK. If that's what you want to do...

ok so I took out what you told me to = smaller code, thank you.
Now for

 if (s==HIGH)
           { 
                 count++;
            }

I want to switch display sequence with each button press, and I was under the impression this was the way to go, I'm open to any suggestion you may have to make it better.

However the core problem still exists , the code doesn't return to count 0 just skips that whole section. Might be something really stupid but I just cant seem to find it.

There is a button class. It handles debouncing for you. You should use it.

the button is working fine ,the code responds to the button push but it doesnt return to the first sequence IF.