Three buttons, counting their presses

I am just started. I wants to use 3 push bottom with LED for counting & also wants to store the data into PC. What will be the code. Kindly Help me.


Could you take a few moments to Learn How To Use The Forum. Other general help and troubleshooting advice can be found here. It will help you get the best out of the forum in the future.

Look at the examples in the IDE. The one called state change detector is what you want.

also wants to store the data into PC.

What data, and when does it need to upload?- every time you press one of the buttons? Every hour? Every day at a certain time?

people here will assist you writing your own code, but generally won’t write all your code for you.
If you are looking for someone to write out all the code for you there are places you can find programmers for hire.

if you are making effort to write this yourself then please post what you have so far or explain where you are stuck.

You could use plx-daq to upload the data to Excel, either each time one of the 3 buttons is pressed (which would require continuous connection) or perhaps with an “upload” button (which would only require ad-hoc connection.)

Or you could display the count on an lcd all the time, and when you want the numbers to go to the pc just copy them off the lcd and type them in.

The code below, which was not written to solve your problem so isn’t what you actually need (I can’t even remember why I did write it) shows at least how pressing one of three buttons causes an entry in a spreadsheet.

It also shows how Grumpy_Mike’s state change detection suggestion can work across an array of buttons.

// state change detect on a button array and reported to plx-daq
// 1 nov 2019

// the buttons
byte buttonPins[] = {3, 4, 5}; //the buttons must be wired from pin to ground, pinmodes are input_pullup
const byte howManyButtons(sizeof(buttonPins) / sizeof(byte));
bool buttonStates[howManyButtons];         // current state of the button
bool lastButtonStates[howManyButtons];     // previous state of the button

void setup()
  // initialize serial communication:
  Serial.println("setup() ... ");
  Serial.println("report state change detect on a button array to plx-daq");

  // initialize the button pins as input with pullup so active low
  //    make sure the button is from pin to ground
  for (int i = 0; i < howManyButtons; i++)
    pinMode(buttonPins[i], INPUT_PULLUP);
    //initialize button states
    buttonStates[i] = digitalRead(buttonPins[i]);
    lastButtonStates[i] = buttonStates[i];
  //plx daq stuff
  Serial.println("MSG, Button presses");

  Serial.println("setup() done");
  Serial.println("Press a button....");
  Serial.println(" ");

void loop()
} //loop

void checkForButtonStateChange()
  for (int i = 0; i < howManyButtons; i++)
    buttonStates[i] = digitalRead(buttonPins[i]);
    // compare the buttonState to its previous state
    if (buttonStates[i] != lastButtonStates[i])
      if (buttonStates[i] == LOW)  // changed to pressed
      // poor man's de-bounce
    // save the current state as the last state, for next time through the loop
    lastButtonStates[i] = buttonStates[i];
} // checkForButtonStateChange()

That code produces output like this:

Time      Date      Pin Index
07:02:56 02 02 2020 5      2
07:02:59 02 02 2020 4      1
07:03:00 02 02 2020 3      0
07:03:10 02 02 2020 4      1
07:03:15 02 02 2020 3      0
07:03:18 02 02 2020 3      0
07:03:18 02 02 2020 3      0
07:03:19 02 02 2020 4      1
07:03:20 02 02 2020 5      2

But frankly, if all you need to do is transfer 3 numbers to the PC every-now-and-then, do it by hand :wink: