Buttons and diodes

hello, i am making a project where i'd like to have 4 buttons on 4 different digital pins and a common digital pin that reads whenever any of those 4 button was pressed and then procced to se which one was actually pressed. i set the buttons pins as INPT_PULLUP so when they get conected to gnd, the current flows from the positive pin to gnd but i don't know how i mus set the common pin in order to get a signal when the button is pressed

You don't need a fifth digital pin to determine whether any of the buttons are pressed nor which one is pressed. Do it in software

What are you planning to attach to the fifth pin ?

void loop() {
  Serial.println(buttonState);
  int reading = digitalRead(commonButton);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == LOW) {        
        buttonPushCounter++;
        while (buttonPushCounter != buttonPushCounter2) {
          Serial.println(buttonState);
          if (digitalRead(buttonA) == LOW) {
            radio.write(&NrfDataA, sizeof(NrfDataA));
            buttonPushCounter2 = buttonPushCounter;
            Serial.println("A");
          } else if (digitalRead(buttonB) == LOW) {
            radio.write(&NrfDataB, sizeof(NrfDataB));
            buttonPushCounter2 = buttonPushCounter;
            Serial.println("B");
          } else if (digitalRead(buttonC) == LOW) {
            radio.write(&NrfDataC, sizeof(NrfDataC));
            buttonPushCounter2 = buttonPushCounter;
            Serial.println("C");
          } else if (digitalRead(buttonD) == LOW) {
            radio.write(&NrfDataD, sizeof(NrfDataD));
            buttonPushCounter2 = buttonPushCounter;
            Serial.println("D");
          }
        }
      }
    }

  }

  lastButtonState = reading;


}

i was thinking of doing it like this, the buttonState is the fifth pin that reads whenever a button is pressed, i saw this setup on a 315mhz receiver unit that has many digital outputs and one that activated whenever i've got a signal on those chanels

Put the 4 button pins in an array and iterate through it reading the pins. When you find one pressed do what you want

An array of structs would allow you to hold multiple data items for each input to be used how you want

I quite don't know how to use arrays

Now is the time to learn

I have to go soon, but if nobody else has jumped in I will add an example tomorrow

for now i just changed it awfully

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE 9, CSN 10, ICSP; SCK MIDDLE LEFT, MOSI MIDDLE RIGHT, MISO LEFT UPPER CORNER
const uint64_t pipe = 0xE7E7E7E7E7;
const char NrfDataA[2] = "A";
const char NrfDataB[2] = "B";
const char NrfDataC[2] = "C";
const char NrfDataD[2] = "D";

const int buttonA = 2;
const int buttonB = 3;
const int buttonC = 4;
const int buttonD = 5;

int buttonStateA = HIGH;
int lastButtonStateA = HIGH;

int buttonStateB = HIGH;
int lastButtonStateB = HIGH;

int buttonStateC = HIGH;
int lastButtonStateC = HIGH;

int buttonStateD = HIGH;
int lastButtonStateD = HIGH;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {

  pinMode(buttonA, INPUT_PULLUP);
  pinMode(buttonB, INPUT_PULLUP);
  pinMode(buttonC, INPUT_PULLUP);
  pinMode(buttonD, INPUT_PULLUP);
  
  Serial.begin(9600);
  radio.begin();
  radio.setChannel(108);
  radio.openWritingPipe(pipe);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setAutoAck(false);
  radio.setRetries(3, 5); // delay, count
  radio.setPayloadSize(2);
  radio.stopListening();
}

void loop() {

  int readingA = digitalRead(buttonA);
  int readingB = digitalRead(buttonB);
  int readingC = digitalRead(buttonC);
  int readingD = digitalRead(buttonD);

  if ((readingA != lastButtonStateA) || (readingB != lastButtonStateB)|| (readingC != lastButtonStateC)|| (readingD != lastButtonStateD)) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    
    if (readingA != buttonStateA) {
      buttonStateA = readingA;
      if (buttonStateA == LOW) {
        radio.write(&NrfDataA, sizeof(NrfDataA));
                Serial.println("a");
      }
    } else if (readingB != buttonStateB) {
      buttonStateB = readingB;
      if (buttonStateB == LOW) {
        radio.write(&NrfDataB, sizeof(NrfDataB));
                Serial.println("b");
      }
    }else if (readingC != buttonStateC) {
      buttonStateC = readingC;
      if (buttonStateC == LOW) {
        radio.write(&NrfDataC, sizeof(NrfDataC));
                Serial.println("c");
      }
    }else if (readingD != buttonStateD) {
      buttonStateD = readingD;
      if (buttonStateD == LOW) {
        radio.write(&NrfDataD, sizeof(NrfDataD));
        Serial.println("d");
      }
    }
  }

  lastButtonStateA = readingA;
  lastButtonStateB = readingB;
  lastButtonStateC = readingC;
  lastButtonStateD = readingD;

}

Here is an example using an array of structs

struct t_dataLayout //layout of data about each pin
{
  const byte pinNumber; //the pin number
  const char message; //message to print to screen
  const char data;  //data to send
};

t_dataLayout buttonData[] =
{
  {A3, 'a', 'A'}, //fill in the details for each button
  {A2, 'b', 'B'},
  {A1, 'c', 'C'},
  {A0, 'd', 'D'}
};

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int p = 0; p < 4; p++) //set up the pinmodes for input pins
  {
    pinMode(buttonData[p].pinNumber, INPUT_PULLUP);
  }
}

void loop()
{
  for (int p = 0; p < 4; p++) //read each button pin
  {
    if (digitalRead(buttonData[p].pinNumber) == LOW)  //if the button is pressed
    {
      Serial.println(buttonData[p].message);  //print message to screen
      //radio.write(&buttonData[p].data, sizeof(buttonData[p].data)); //send the data
    }
  }
}

NOTE

  • the radio code has been omitted in this example
  • for clarity I have not included any debouncing, but it can be added
  • all the time a button is pressed a message will be sent
  • multiple buttons can be pressed at once

Ask questions if you need any clarification

1 Like

thanks for your time, so this code does the same as mine with buttons but is shorter ?

It detects when a button is pressed and takes different action based on which one is pressed

I think that is what you want
Give it a try and see

I experimented and i ended up fraying the regulator on pro mini by shorting raw and gnd while i was measuring the input voltage and output afterwards. the red led just instantly went down and it won't work anymore, as i heard that it will block the circuit until the short is removed but it frayed. i had to put an external asm1117 5v regulator and i supply 5v at vin pin and it works for the moment. i was wondering if i get the input voltage wrong, if it needs 3.3 and i give 5, what would happen ? as if it is 5v and i give 3.3 it will light weaker