/* Simple Counter
* ——————
*
* This is a simple counter that takes a digital input
*
*/
int ledPin=13; // choose the pin for the LED
int switchPinUp=2; // choose the input pin (for a pushbutton)
int switchPinMin=3;
int val = 0; // variable for reading the pin status
int counter = 0;
int currentStateUp = 0;
int previousStateUp = 0;
int currentStateMin = 0;
int previousStateMin = 0;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPinUp, INPUT); // declare pushbutton as input
pinMode(switchPinMin, INPUT); // declare pushbutton as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(switchPinUp); // read input value
if (val == LOW) { // check if the input is HIGH (button released)
currentStateUp = 1;
}
else {
currentStateUp = 0;
}
if(currentStateUp != previousStateUp){
if(currentStateUp == 1){
counter = counter+1;
Serial.println(counter);
}
}
previousStateUp = currentStateUp;
val = digitalRead(switchPinMin); // read input value
if (val == LOW) { // check if the input is HIGH (button released)
currentStateMin = 1;
}
else {
currentStateMin = 0;
}
if(currentStateMin != previousStateMin){
if(currentStateMin == 1){
counter = counter-1;
Serial.println(counter);
}
}
previousStateMin = currentStateMin;
if (counter== 5)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(100);
}