Go Down

Topic: Need Help (Read 782 times) previous topic - next topic


Oct 27, 2012, 08:52 pm Last Edit: Oct 27, 2012, 09:13 pm by JHurley747 Reason: 1
I am trying to program my 7 segment LED display to display a number, based on the number of times a button is pressed.
For Example: Press the Button three times, a 3 appears on the LED display.
I wanted a pretty simple code and I am thinking using an interrupt will work. BTW I am new to the forums and to the Arduino UNO.

Code: [Select]

const int aPIN = 9;
const int bPIN = 8;
const int cPIN = 7;
const int dPIN = 10;
const int ePIN = 11;
const int fPIN = 13;
const int gPIN = 12;
const int buttonPIN = 2;                               // I left this in here from when i was trying this same idea with ridiculous if statements and booleans
int pressCount;                                           // I tried this as a volatile but it didn't seem to work

void setup()
 pinMode(aPIN, OUTPUT);
 pinMode(bPIN, OUTPUT);
 pinMode(cPIN, OUTPUT);
 pinMode(dPIN, OUTPUT);
 pinMode(ePIN, OUTPUT);
 pinMode(fPIN, OUTPUT);
 pinMode(gPIN, OUTPUT);
 attachInterrupt(0, count, RISING);            // I tried the first value set to both 2 and 0 neither worked

void loop()
int pressCount = 0;                                    // I think this is my issue:
                                                               // 1. void count can't edit this var because it is in my loop.
                                                               // 2. everytime this loop runs it resets the pressCount to 0  
                                                               // So If i move this statement to the bottom it should work right?
                                                               // Wrong Becuase apparently void count will not edit vars in void loop
unsigned long readTime = 0;                      // Stores run time at beginning of loop for read time, resets it everytime the loop runs

readTime = millis();

if ((readTime == 7000) && (pressCount == 1)) // 7 second to press the button 1 time, this code is only for testing 1 press = the number 1 to appear
   digitalWrite(bPIN, HIGH);
   digitalWrite(cPIN, HIGH);

void count()
 pressCount = (pressCount + 1);

If you read my comments on  "   int pressCount = 0;   "
and go to http://arduino.cc/en/Reference/AttachInterrupt  and look at the code on this page it makes me think that voids can edit variables across each other...

Any Ideas?


You have two pressCounts .
I'd leave just the global one.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Why are you using an interrupt handler to poll a switch? Way overkill.


Why are you using an interrupt handler to poll a switch? Way overkill.

Polling is as easy as using digitalRead() ?
Or the idea behind it?


Polling is as easy as using digitalRead() ?

Polling IS using digitalRead().


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131