Help using If statements (ithink)

Hi all!

New here and i'm starting my first "real world" project. I have setup a counter using a 7 seg LCD. This can count up to 9 and back down to 0 if buttons, or in this case a relay is fired (don't worry about this bit its just a button for the moment)

What i need is for another Pin (11, as this is the only one i have left) to go HIGH for 3 seconds if the counter reads 0-9. If the counter is over 9 for the pin not to go HIGH.

Just to clarify. The Pin will go High on each count unless the counter reaches 9, this also needs to work backwards was well for the down count. The main code i am using is from a tutorial i followed, which i need to make changes to. its below.

//Code wtiren by YAHAV HADAD
// 2016
//All Rights Reserved ©

//ENJOY!!

//Initialize the 7 segment pins
int A = 3;
int B = 2;
int C = 4;
int D = 5;
int DP = 7;
int E = 6;
int F = 8;
int G = 9;

//Initialize the push buttons pins, push buttons states, and the counter
int switchUpPin = 13;
int switchDownPin = 12;
int counter = 0;
int buttonUpState = 0;
int lastButtonUpState = 0;
int buttonDownState = 0;
int lastButtonDownState = 0;

void setup() 
{
 Serial.begin(9600);
 
 pinMode(A, OUTPUT);
 pinMode(B, OUTPUT);
 pinMode(C, OUTPUT);
 pinMode(D, OUTPUT);
 pinMode(E, OUTPUT);
 pinMode(F, OUTPUT);
 pinMode(G, OUTPUT);
 pinMode(DP, OUTPUT);

 //Start with the deceimal point off
 digitalWrite(DP ,HIGH);
}

void loop() 
{
 //Getting the reads from the buttons
 buttonUpState = digitalRead(switchUpPin);
 buttonDownState = digitalRead(switchDownPin);

 //Detecting button press and getting the button status
 //Do this for the button up
 if (buttonUpState != lastButtonUpState) 
 {
   if (buttonUpState == HIGH) 
   {
     //Reset the counter to -1
     if(counter == 9)
     {
       counter = -1;
     }
     //Increase the counter by 1
     counter++;
     //Print the counter to the console and calling the function
     Serial.println(counter);
     changeNumber(counter);
     //Delaying by 250 ms
     delay(250);
   }
   else
   {
       Serial.println("OFF");
   }
   //Delay to avoid button bouncing
  delay(1000);
 }

 //Do this for the button down
 if (buttonDownState != lastButtonDownState) 
 {
   if (buttonDownState == HIGH) 
   {
     //Set the counter to 10
     if(counter == 0)
     {
       counter = 10;
     }
     //Decreases the counter by 1
     counter--;
     ////Print the counter to the console and calling the function
     Serial.println(counter);
     changeNumber(counter);
     //Delaying by 250 ms
     delay(250);
   }
   else
   {
       Serial.println("OFF");
   }
   //Delay to avoid button bouncing
  delay(1000);
 }
 //Calling the function changeNumber with the arg counter
 changeNumber(counter);
}

//The function to display the numbers
void changeNumber(int buttonPress)
{
 switch (buttonPress)
 {
   //number 0
   case 0:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, LOW);
     digitalWrite(E, LOW);
     digitalWrite(F, LOW);
     digitalWrite(G, HIGH);
     break;
   //number 1
   case 1:
     digitalWrite(A, HIGH);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, HIGH);
     digitalWrite(E, HIGH);
     digitalWrite(F, HIGH);
     digitalWrite(G, HIGH);
     break;
   //number 2
   case 2:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, HIGH);
     digitalWrite(D, LOW);
     digitalWrite(E, LOW);
     digitalWrite(F, HIGH);
     digitalWrite(G, LOW);
     break;
   //number 3
   case 3:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, LOW);
     digitalWrite(E, HIGH);
     digitalWrite(F, HIGH);
     digitalWrite(G, LOW);
     break;
   //number 4
   case 4:
     digitalWrite(A, HIGH);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, HIGH);
     digitalWrite(E, HIGH);
     digitalWrite(F, LOW);
     digitalWrite(G, LOW);
     break;
   //number 5
   case 5:
     digitalWrite(A, LOW);
     digitalWrite(B, HIGH);
     digitalWrite(C, LOW);
     digitalWrite(D, LOW);
     digitalWrite(E, HIGH);
     digitalWrite(F, LOW);
     digitalWrite(G, LOW);
     break;
   //number 6
   case 6:
     digitalWrite(A, LOW);
     digitalWrite(B, HIGH);
     digitalWrite(C, LOW);
     digitalWrite(D, LOW);
     digitalWrite(E, LOW);
     digitalWrite(F, LOW);
     digitalWrite(G, LOW);
     break;
   //number 7
   case 7:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, HIGH);
     digitalWrite(E, HIGH);
     digitalWrite(F, HIGH);
     digitalWrite(G, HIGH);
     break;
   //number 8
   case 8:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, LOW);
     digitalWrite(E, LOW);
     digitalWrite(F, LOW);
     digitalWrite(G, LOW);
     break;
   //number 9
   case 9:
     digitalWrite(A, LOW);
     digitalWrite(B, LOW);
     digitalWrite(C, LOW);
     digitalWrite(D, HIGH);
     digitalWrite(E, HIGH);
     digitalWrite(F, LOW);
     digitalWrite(G, LOW);
     break;
 }
}

Your first task is to get rid of the delay()s in your program because they will cause the program to be blocked from running whilst they occur.

Have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Using millis() for timing needs a different approach than simply using the delay() function but allows the program to run freely without stalling. You cannot, however, just pop millis() into the existing program. It will need restructuring.

OK thanks, However i might be right in says the debounce one is needed otherwise the counter will continue to count if my relay (button) is held down. I have set it for 1000ms as this is how long the relay fires for.

Detect when the button becomes pressed, not when it is pressed

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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:

Whoops, Sorry! Attached is the diagram. Thanks everyone for their help

Hi,
Thanks. OPs circuit;


Tom... :slight_smile:

Had a play with the Mills. Demo project was insightful. Just need to figure out the changes.

Since you are using only two buttons, you could utilize pin-change interrupts to detect when a button was pressed.
Usually this is the best way to deal with button presses that could occur at any time.

Don't forget to "debounce" your buttons, either in hardware or in your software.
Do this in your ISR:

Additionally you might look into for-loops and the map function:

Might not be very useful in your specific scenario, more for a (battery) graph style LED-arrangement.

Looking back on this thread I am not clear what the requirement is.

I have setup a counter using a 7 seg LCD. This can count up to 9 and back down to 0 if buttons, or in this case a relay is fired (don't worry about this bit its just a button for the moment)

If the count can only be between 0 and 9 as you say

What i need is for another Pin (11, as this is the only one i have left) to go HIGH for 3 seconds if the counter reads 0-9. If the counter is over 9 for the pin not to go HIGH.

then how can it ever not read 0-9 ?

What values can the count hold ?

Sorry I will try and be more clear.

The project is a counter for a door. 1 button counts in, the other out. If there are less the 9 people in the building then the relay will go high and open the door. if the counter is at 9 (the limit), the relay doesn't go high and doesn't open the door.

I hope this make sense. Do I need to store the number state within a INT and then run an IF/Else from that?