Pages: [1]   Go Down
Author Topic: Need Help  (Read 336 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Thanks!
« Last Edit: October 27, 2012, 02:13:52 pm by JHurley747 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have two pressCounts .
I'd leave just the global one.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Polling is as easy as using digitalRead() ?
Polling IS using digitalRead().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks!
Logged

Pages: [1]   Go Up
Jump to: