Counting Issue
i totaly new programming , microcontrolers. have been playing m uno , using example code comes ide have been able put following program. is life counter when play magic gathering.
it's controled 4 buttons 2 rase , lower player 1s hp (1 point per press) , 2 player 2.
i didn't lay out prototype on bread board don't have space (until feel rebuilding circut bottuns using wire connect 3.3v pin appropate alalog pit button hooked to.
everything seems work, when 'push button' (touch wire breafly appropate pin) counter increase several numbers (between 2 , 15). there seems no obviouse pattern how changes.
any ideas?
code:
it's controled 4 buttons 2 rase , lower player 1s hp (1 point per press) , 2 player 2.
i didn't lay out prototype on bread board don't have space (until feel rebuilding circut bottuns using wire connect 3.3v pin appropate alalog pit button hooked to.
everything seems work, when 'push button' (touch wire breafly appropate pin) counter increase several numbers (between 2 , 15). there seems no obviouse pattern how changes.
any ideas?
code:
code: [select]
/*
issue: when button pressed value change more 1 per press.
*/
/*
magic gathering life counter
* v0.1 2013.06.20
circuit:
* lcd rs pin digital pin 12
* lcd enable pin digital pin 11
* lcd d4 pin digital pin 5
* lcd d5 pin digital pin 4
* lcd d6 pin digital pin 3
* lcd d7 pin digital pin 2
* lcd r/w pin ground
* lcd vo pin (pin 3) ground
* no pushbutton (button 1) 3.3v a0 player 1 hp up
* no pushbutton (button 2) 3.3v a1 player 1 hp down
* no pushbutton (button 3) 3.3v a2 player 2 hp up
* no pushbutton (button 4) 3.3v a3 player 2 hp down
*/
// include library code:
#include <liquidcrystal.h>
// initialize library numbers of interface pins
liquidcrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonpin0 = a0; // number of pushbutton pin player 1 hp up
const int buttonpin1 = a1; // number of pushbutton pin player 1 hp down
const int buttonpin2 = a2; // number of pushbutton pin player 2 hp up
const int buttonpin3 = a3; // number of pushbutton pin player 2 hp down
int p1hpup_buttonstate = 0; // variable reading pushbutton status player 1 hp up
int p1hpdown_buttonstate = 0; // variable reading pushbutton status player 1 hp down
int p2hpup_buttonstate = 0; // variable reading pushbutton status player 2 hp up
int p2hpdown_buttonstate = 0; // variable reading pushbutton status player 2 hp down
int hp1 = 20; // set player 1 hp 20
int hp2 = 20; // set player 2 hp 20
void setup() {
// set lcd's number of columns , rows:
lcd.begin(16, 2);
// initialize pushbutton pin input:
pinmode(buttonpin0, input);
pinmode(buttonpin1, input);
pinmode(buttonpin2, input);
pinmode(buttonpin3, input);
// print welcome message lcd.
lcd.setcursor(0, 0);
lcd.print("magic gathering");
lcd.setcursor(0, 1);
lcd.print(" life counter ");
delay(750);
lcd.setcursor(0, 0);
lcd.print(" ");
lcd.setcursor(0, 1);
lcd.print(" ");
delay(500);
}
void loop() {
lcd.setcursor(0, 0);
lcd.print("player1 player2"); // display players on line 0
lcd.setcursor(0, 1); // display player 1 hp
lcd.print("hp");
lcd.setcursor(4, 1);
lcd.print(hp1);
lcd.setcursor(9, 1); // display player 2 hp
lcd.print("hp");
lcd.setcursor(13, 1);
lcd.print(hp2);
// check if pushbutton pressed.
// if is, buttonstate high:
p1hpup_buttonstate = digitalread(buttonpin0);
if (p1hpup_buttonstate == high) {
hp1 = hp1 + 1;
delay(500);
}
else {hp1 = hp1;
}
p1hpdown_buttonstate = digitalread(buttonpin1);
if (p1hpdown_buttonstate == high) {
hp1 = hp1 - 1;
delay(500);
}
else {hp1 = hp1;
}
p2hpup_buttonstate = digitalread(buttonpin2);
if (p2hpup_buttonstate == high) {
hp2 = hp2 + 1;
delay(500);
}
else {hp1 = hp1;
}
p2hpdown_buttonstate = digitalread(buttonpin3);
if (p2hpdown_buttonstate == high) {
hp2 = hp2 - 1;
delay(500);
}
else {hp2 = hp2;
}
delay(1000);
}
what happening touch wire , un-touch wire, there moment wire connected , not connected. @ point can "bounce" in , out, connecting , unconnecting undetermined number of times before far enough away can't connect anymore. you, happens in instant , shouldn't problem. microcontroller happens quite , looks multiple button pushes.
the same thing happen in contacts of button when hook up. adding button won't problem.
you need debounce code. word, debounce. want detect transitions in state of button , ignore come after it. easy way ignore within 2ms of detecting transition.
the same thing happen in contacts of button when hook up. adding button won't problem.
you need debounce code. word, debounce. want detect transitions in state of button , ignore come after it. easy way ignore within 2ms of detecting transition.
Arduino Forum > Using Arduino > Project Guidance > Counting Issue
arduino
Comments
Post a Comment