Hardware guy just getting into the software side.
I wanted to modify the button push counter program to have it count button pushes from 2 different buttons. I doubled everything, re-naming variables such as:
*/
// this constant won't change:
const int buttonPin2 = 2; // the pin that the pushbutton is attached to
const int buttonPin1 = 1; // the pin that the pushbutton is attached to
const int ledPin = 12; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0; // previous state of the button
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
But I can't figure out how to put duplicate data in the Loop to read from each swith to each displayed output. My pathetic attempt is below:
/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.
This example shows how to detect when a button or button changes from off to on
and on to off.
The circuit:
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED attached from pin 13 to ground (or use the built-in LED on
most Arduino boards)
created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin2 = 2; // the pin that the pushbutton is attached to
const int buttonPin1 = 1; // the pin that the pushbutton is attached to
const int ledPin = 12; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonState2 = 0; // current state of the button
int lastButtonState2 = 0; // previous state of the button
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin2, INPUT);
// initialize the LED as an output:
pinMode(buttonPin1, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter2++;
Serial.println("on");
Serial.print("Pump 2 Strokes: ");
Serial.println(buttonPushCounter2);
}
else {
// if the current state is LOW then the button
// went from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState2 = buttonState2;
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter1++;
Serial.println("on");
Serial.print("Pump 1 Strokes: ");
Serial.println(buttonPushCounter1);
}
else {
// if the current state is LOW then the button
// went from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter1 % 1 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Moderator edit: code tags added.