Being able to recognize a button being pushed?

Hey everyone. I'm working on a take-home test for my robotics class. My instructor said we can use any resources available to us, including these forums, and I'm in need of some help.

My situation is, I have 5 push buttons attached to the A0 pin in an Arduino simulator. Now the assignment is to write a program that outputs the analog reading value of each button when pushed. As I have it now my code:

#define PIN_BTN A0

void setup() {
  
  Serial.begin(9600);
  pinMode(PIN_BTN, INPUT);
    
}

void loop() {
  
  int output = 0;
      
  output = analogRead(PIN_BTN);
  Serial.println(output);
  delay(1000);
  
  while(1==1){
    
  }
}

It ends up continuously looping, which means it prints out the analogRead output over and over again, as expected. I put the while statement in there to prevent it for a moment while I try and figure out how to make the Arduino recognize that I only want Serial.println(output) to work when a button is pressed.

I've tried doing:

int buttonState = 0;

buttonState = digitalRead(PIN_BTN);

if(buttonState == HIGH){
  ...
  Serial.println(output);
}
else{
  ...
}

But the resistors on two of the buttons he gave me in the schematics are too high so it won't recognize those two buttons being pressed. I even tried buttonState = analogRead(PIN_BTN), but that also didn't help. Does anyone have any suggestions, or any way for the buttonState to still register the two buttons with the higher resistors?

Show us the schematic diagram please.

Absolutely. I have attached the schematic, a picture of the simulator that I think is easier to look at, and a table that tells you the properties of each piece, like the resistance.

I imagine that your schematic will show that a different voltage appears at A0 depending upon which of the 5 buttons is pushed.

In pseudocode, you'd need to do something like this in your loop:

buttonpressedlast = buttonpressed;
buttonState = analogRead(PIN_BTN);

if(buttonState > BUTTON_5_THRESHOLD) buttonpressed = 5;
else if(buttonState > BUTTON_4_THRESHOLD) buttonpressed = 4;
else if(buttonState > BUTTON_3_THRESHOLD) buttonpressed = 3;
else if(buttonState > BUTTON_2_THRESHOLD) buttonpressed = 2;
else if(buttonState > BUTTON_1_THRESHOLD) buttonpressed = 1;
else buttonpressed = 0;

if(buttonpressedlast != buttonpressed) Serial.println(buttonState);

The THRESHOLD constants would set depending upon the expected value of the ADC when each button is pushed. I assumed that Button 5 would produce the highest ADC value in my code. If that's not true, then the order of the if statements would need to be changed.

If you only want it to print once when the button is first pushed, then you'd need to keep track of whether the button pressed changes (hence my buttonpressedlast vs buttonpressed statements).

I ended up doing something very similar to that actually. I created a global variable to match against my output variable in the loop. I made it so that if they match, then the loop ends, but if they are different then the loop runs through again. Here's what I came up with:

#define PIN_BTN A0

int G_OUTPUT = 0;

void setup() {
  
  Serial.begin(9600);
  pinMode(PIN_BTN, INPUT);
    
}

void loop() {
  
  int output = analogRead(PIN_BTN);
  
  if(output != G_OUTPUT){    
   Serial.println(output);
   delay(1000);
    G_OUTPUT = output;
  }
  else if(G_OUTPUT == output){
  }
}

And it seemed to work just as needed. I really appreciate your help.

No problem. I guess if it wasn't part of your assignment to figure out which button was then you didnt need the if-else statements in my post.

One improvement you could make to your code is to output only when ADC value changes by more than a certain amount. Real ADCs are rarely going to give you the exact number twice so checking for them to be != would print multiple times as the ADC value varied by a few bits even though the same button was being pushed.

Actually, you are correct. The second part of my assignment is to print out which button is pressed, so I will need your code. Thank you. And I will definitely change my code to reflect possible fluctuations. Thanks again for all your help!