Request for help modifying program

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.

Assuming you use an Arduino Uno you should first connect buttonPin1 to another input then 1. Inputs 0 and 1 are used for serial communication. For the ledPin you have to connect another LED or for instance use the sum of your button counters to change the state of the onboard LED:

  if ((buttonPushCounter1 + buttonPushCounter2) % 2 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

And: Modulo 1 always returns 0. Modulo 2 returns 0 or 1...

Much thanks for pointing that out. So I should read about input states, uses, etc.

I now have a functional pump stroke counter for oil drilling rigs, yay.

Thanks again.

jacksonguitardog:
I now have a functional pump stroke counter for oil drilling rigs, yay.

May I propose the use of safety PLCs for your oil platform ]:slight_smile:

If you want to be fancy, you could also do:

digitalWrite(ledPin,(buttonPushCounter1 + buttonPushCounter2) % 2 == 0 ? High : LOW);

/*
Pump Stroke counter for two pumps.

Pump 2 = Pin 2 for switch, pin 13 for LED
Pump 1 = Pin 3 for switch, pin 12 for LED

Unless otherwise modified.

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.

*/

// this constant won't change:
const int buttonPin2 = 2; // the pin that the pushbutton is attached to
const int buttonPin1 = 3; // the pin that the pushbutton is attached to
const int ledPin2 = 13; // the pin that the LED is attached to
const int ledPin1 = 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(ledPin2, OUTPUT);
// initialize serial communication:
pinMode(ledPin1, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState2 = digitalRead(buttonPin2);
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);

// 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;

if (buttonState2 == HIGH) {
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}

// 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;

if(buttonState1 == HIGH) {
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}

}