Go Down

Topic: Touchy Program - How do I fix? (Read 1 time) previous topic - next topic

mrmm314

I'm pretty new to programming in C, so I tried to write a program to use with a 7 segment display, and the following is what I came up with.  the only problem is that it is pretty touchy, and doesn't quite do what the button presses should be telling it to do every time, although sometimes it works with 1 to 3 button presses.  Any suggestions would be greatly appreciated.

Code: [Select]
/*While button2 is pressed (HIGH), the program counts
the presses (HIGH) of button, then displays the count
number with a 7 Segment Display*/

//Button Count Variables
int state = LOW;
int lastState = LOW;
int count = 10;

//7 Segment Display Pins
int segF = 4;
int segG = 5;
int segE = 6;
int segD = 7;
int segA = 8;
int segB = 9;
int segC = 10;

//Button Pins
int button = 11;
int button2 = 12;

void setup()
 {
   Serial.begin(9600);
   pinMode(button, INPUT);
   pinMode(button2, INPUT);
   state = digitalRead(button);
   pinMode(segA, OUTPUT);
   pinMode(segB, OUTPUT);
   pinMode(segC, OUTPUT);
   pinMode(segD, OUTPUT);
   pinMode(segE, OUTPUT);
   pinMode(segF, OUTPUT);
   pinMode(segG, OUTPUT);
   
 }

void loop()
 {
   while(digitalRead(button2) == HIGH)
   {
         
   if (state == HIGH && lastState == LOW)
   {
     count++;
   }
   
   lastState = state;
   state = digitalRead(button);
   }
   
   switch (count)
   {
 case 1:
   //Turns on and off the LEDs on the 7seg to display "1"
   digitalWrite(segA, LOW);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, LOW);
   digitalWrite(segE, LOW);
   digitalWrite(segF, LOW);
   digitalWrite(segG, LOW);
   break;
 case 2:
   /*Same as 1, but this displays "2", and so on for the
   rest of these*/
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, LOW);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, HIGH);
   digitalWrite(segF, LOW);
   digitalWrite(segG, HIGH);
   break;
 case 3:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, LOW);
   digitalWrite(segF, LOW);
   digitalWrite(segG, HIGH);
   break;
 case 4:
   digitalWrite(segA, LOW);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, LOW);
   digitalWrite(segE, LOW);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, HIGH);
   break;
 case 5:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, LOW);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, LOW);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, HIGH);
   break;
 case 6:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, LOW);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, HIGH);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, HIGH);
   break;
 case 7:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, LOW);
   digitalWrite(segE, LOW);
   digitalWrite(segF, LOW);
   digitalWrite(segG, LOW);
   break;
 case 8:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, HIGH);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, HIGH);
   break;
 case 9:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, LOW);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, HIGH);
   break;
 case 10:
   digitalWrite(segA, HIGH);
   digitalWrite(segB, HIGH);
   digitalWrite(segC, HIGH);
   digitalWrite(segD, HIGH);
   digitalWrite(segE, HIGH);
   digitalWrite(segF, HIGH);
   digitalWrite(segG, LOW);
   break;
 default:
   break;
   }
   
   count = 0;
 }


pluggy

The circuit you have connected to the 'button' pins would help.  Are you using a pulldown resistor ?

mrmm314

I have a 10k resistor connected from ground to the button pins, and the buttons connected to 5v, so when it is pressed, it returns (HIGH), and unpressed returns (LOW).

pluggy

Is the intended behaviour that nothing happens until the first button is pressed and then the 2nd button increments the number displayed ? So you have to press both buttons to increment the count (the number displayed ?

mrmm314

@pluggy,  Yes.  When the one button is pressed, it goes into the While loop, while it then counts the button presses of the other button.  And when the first button is let go, it should display the number of times I pressed the second button.  But it is very touchy, and only seems to work semi-consistently with 1 to 3.

Go Up