Need to have 1 digit Led display count increase with mechanical switch

Hello Everyone,

This is my first post here and I am definitely not very experienced with arduino.

Right now I am playing with a 1 digit led display and I have been able to make it

count 0 - 9 with the code written below…

Basically instead of having the numbers increase automatically, I would like to have the numbers increase every time I toggle a switch. Can anyone please help?? I am stumped!!!

#define segA 10//connecting segment A to PIN1
#define segB 2// connecting segment B to PIN3
#define segC 3// connecting segment C to PIN2
#define segD 4// connecting segment D to PIN9
#define segE 5// connecting segment E to PIN5
#define segF 6// connecting segment F to PIN7
#define segG 7// connecting segment G to PIN6
#define segH 9// connecting segment G to PIN4

int COUNT = 0; // Count Integer for 0-9 increment

void setup()

{
    for(int i=2; i<11; i++)
    {
      pinMode(i, OUTPUT); //taking all pins from 2-11 as outputs
    }
}
void loop()
{
  switch(COUNT)
  {
    case 10: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 9: //when count value is 0 a zero is shown
    digitalWrite(segA, LOW);
    digitalWrite(segB, LOW);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;

    case 8: //when count value is 0 a zero is shown
    digitalWrite(segA, LOW);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, LOW);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 7: //when count value is 0 a zero is shown
    digitalWrite(segA, LOW);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, HIGH);
    break;

    case 6: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, LOW);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    digitalWrite(segH, HIGH);
    break;

    case 5: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, LOW);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, HIGH); 
    break;

    case 4: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, LOW);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 3: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, LOW);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;

    case 2: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, HIGH);
    digitalWrite(segG, HIGH);
    break;

    case 1: //when count value is 0 a zero is shown
    digitalWrite(segA, HIGH);
    digitalWrite(segB, HIGH);
    digitalWrite(segC, HIGH);
    digitalWrite(segD, HIGH);
    digitalWrite(segE, HIGH);
    digitalWrite(segF, LOW);
    digitalWrite(segG, LOW);
    break;
    
  }
   if (COUNT==0)
       {
            COUNT++;
                delay(10000);///increment count integer for every second
       }
                if (COUNT==10)
                {
                   COUNT=0;// if count integer value is equal to 10, reset it to zero.
                   delay(10000);
                }

}

First read the "How to use this forum" post found at the top of any of the message boards. Pay particular attention to the parts about how to post code correctly.

Next go look at the "State Change Example" that comes with the IDE. It does just what you want, count button presses. All you need to do is display that number on your display.

Sorry about that! Will make sure to post correctly in the future. Thank you for pointing me in the right direction!

While the StateChangeDetect example is indeed what you want, take note that it requires a pull down resistor on the pin, switch between pin and 5V, and the switch being closed gives a "high"

Easier, is to pinMode the pin as INPUT_PULLUP, thus saving messing around with an external resistor. But then the switch must go from pin to ground, and the pullup takes it to 5V. So then, closing the switch makes the pin "low", thus reversing the logic. It may seem counter-intuitive for an "on" switch to give a "low", but it's very common.

gruscak:
Sorry about that! Will make sure to post correctly in the future. Thank you for pointing me in the right direction!

No, not the future, now!

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output) from the IDE and click the icon. In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window.

When posting code, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. I think you have at least done that. If you do not post it as “code”, it can be quite garbled and is always more difficult to read.

The problem with a switch is “contact bounce”. To you, the switch simply switches. To the microcontroller, it may switch dozens of times as the contacts bounce as you switch it in either direction.

And “delay()” prevents you from doing anything useful. You need to study the “Demonstration code for several things at the same time” examples in order to write any serious code. So yes, you have some work to do.