I am totaly new to programming and microcontrolers. I have been playing with m uno and using the example code that comes with the IDE I have been able to put together the following program. All it is is a life counter for when I play Magic The Gathering.
It's controled by 4 buttons 2 to rase and lower Player 1s HP (1 point per press) and 2 for player 2.
I didn't lay out my prototype on the bread board well so I don't have space (until I feel like rebuilding the circut for the bottuns so I am just using a wire to connect the 3.3v pin to the appropate alalog pit that the button would be hooked to.
Everything seems to work, however when I 'push a button' (touch the wire breafly to the appropate pin) the counter will increase several numbers (between 2 and 15). there seems to be no obviouse pattern to how much it changes.
Any ideas?
Code:
/*
Issue: When button is pressed the value will change more then 1 per press.
*/
/*
Magic The Gathering Life Counter
* v0.1 2013.06.20
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VO pin (pin 3) to ground
* NO pushbutton (Button 1) from 3.3v to A0 Player 1 HP up
* NO pushbutton (Button 2) from 3.3v to A1 Player 1 HP down
* NO pushbutton (Button 3) from 3.3v to A2 Player 2 HP up
* NO pushbutton (Button 4) from 3.3v to A3 Player 2 HP down
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buttonPin0 = A0; // the number of the pushbutton pin Player 1 HP up
const int buttonPin1 = A1; // the number of the pushbutton pin Player 1 HP down
const int buttonPin2 = A2; // the number of the pushbutton pin Player 2 HP up
const int buttonPin3 = A3; // the number of the pushbutton pin Player 2 HP down
int P1HPup_buttonState = 0; // variable for reading the pushbutton status Player 1 HP up
int P1HPdown_buttonState = 0; // variable for reading the pushbutton status Player 1 HP down
int P2HPup_buttonState = 0; // variable for reading the pushbutton status Player 2 HP up
int P2HPdown_buttonState = 0; // variable for reading the pushbutton status Player 2 HP down
int HP1 = 20; // Set Player 1 HP to 20
int HP2 = 20; // Set Player 2 HP to 20
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the pushbutton pin as an input:
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// Print a welcome message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Magic The 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 the pushbutton is pressed.
// if it is, the buttonState is 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);
}