5 Buttons, counting, servo Motor, LCD Display

Hi Guys, i need your help. I started with an arduino uno and now i want to make an projekt. There are 5 Buttons, that can trigger an Servo in two ways:

  1. All 5 Buttons are pressed
  2. One or two buttons is pressed and after some time the servo moves
    Now i want to show how many buttons were pressed,. Here the code:
#include <Servo.h>
#include "IRremote.h"
#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 1, 3, 4, 5, 6);
Servo myservo;

int ledPin = 5;
int button5 = 12;
int button4 = 11;
int button3 = 10;
int button1 = 9;
int button2 = 8;
int RECV_PIN = 6;
int latchPin = 3;
int clockPin = 4;
int dataPin = 2;


byte leds = 0;


int pos = 0;
int pos2=400;
int pos3= 1000;
int pos4= 500;
int pos5= 1000;
int count=1;



void setup() 
{
  lcd.begin(16, 2);
  lcd.print("Treffer");
  pinMode(RECV_PIN, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(button1, INPUT_PULLUP);  
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);  
  Serial.begin(9600);
  myservo.attach(7);
  myservo.write(pos);
  
}


void loop() 
{
  if (digitalRead(button1) == LOW or (digitalRead(button2)== LOW) or (digitalRead(button3)== LOW) or (digitalRead(button4)== LOW) or (digitalRead(button5)== LOW))
  {
   count++;

  }
  

  if (digitalRead(button1) == LOW && (digitalRead(button2)== LOW) && (digitalRead(button3)== LOW) && (digitalRead(button4)== LOW) && (digitalRead(button5)== LOW)) 
  {
    myservo.write(pos2);
    delay(pos4);
    myservo.write(pos);
    lcd.setCursor(0, 1);
  // print the number of seconds since reset:
    lcd.print(count);
    delay(1000);
    count=0;             
  }
  else if(digitalRead(button1) == LOW or (digitalRead(button2)== LOW) or (digitalRead(button3)== LOW) or (digitalRead(button4)== LOW) or (digitalRead(button5)== LOW))
  {
    delay(pos3);
    myservo.write(pos2);
    delay(pos4);
    myservo.write(pos);
    lcd.setCursor(0, 1);
  // print the number of seconds since reset:
    lcd.print(count);
    delay(1000);
    count=0;
  } 
  
   
}

Its mostly code of the starter kit mashed together and remade to fit my needs, but i can`t get the Programm to count right and display that variable count on the second row of the LCD Display

You don't seem to have a problem with the IDE and hence your topic has been moved to a more suitable location on the forum.

@justinfischer welcome to the forum.


  if (digitalRead(button1)== LOW or (digitalRead(button2)== LOW) or (digitalRead(button3)== LOW) or (digitalRead(button4)== LOW) or (digitalRead(button5)== LOW))
 
  if (digitalRead(button1)== LOW && (digitalRead(button2)== LOW) && (digitalRead(button3)== LOW) && (digitalRead(button4)== LOW) && (digitalRead(button5)== LOW)) 
  
  if (digitalRead(button1)== LOW or (digitalRead(button2)== LOW) or (digitalRead(button3)== LOW) or (digitalRead(button4)== LOW) or (digitalRead(button5)== LOW))
  

There's your three *if* statement conditional expressions.

I removed all the other code to see if my initial impression was correct and sprinkled in some harmless spaces so they'd line up for inspection.

My impression was incorrect. No surprise.

Confirm that you want

the first one: if any button is pressed
the second: if all buttons are pressed
the third: if any button is pressed

That's how I read it; it seems it would be difficult to press all five buttons at once instantly, never tripping the prior test for any (even several but not all) to be briefly the case.

a7

1 Like

Yeah IT Sounds kinda unrealistic, but IT IS for Biathlon, which means i want to count the Hits. In Future there gönne be 5 Sensors for every target. My Problem is the counting. My my final Goal ist an System where every Person has after the Training His Times and His Hits. So i need to be able to count the "Button presses" and give an Output. Later i want to send this information via Bluetooth to another System which keeps Count of the persons and where they shoot.

When button X is pressed - or sensor X gives an output - use 'state change detection':

IDE->file/examples/digital/

to do one increment of the counter; which counter is set to zero before the string begins.

Mind you, you'll need a state change detect for each switch/input.

2 Likes

Intersting. When in the context of a bialthlon, or ever, really, will it be reasonable to expect anyone or any group of people to

press all five buttons at once instantly

?

a7

not instantly, but the buttons are kept down by the fallen target, so i thought for the programme it looks like they are all pressed and that i can use &&

OK THX you got me to think about it better.

I suggest you move the LCD printing to before the servo waggling in those cases where that happens. That gets the text up without any delay.

Those delays are interfering and inadvertently helping, but you can't count the way you have that logic, expecially if this is a "press and hold" thing.

For counting, you must debounce the buttons, then recognize that for buttons that have become pressed, that is have just gone from not presst to presst.

For each such button that has become pressed, increment the count.

Debouncing and state change detection are straight forward.

If you place a small delay in your loop, the debouncing will be taken care of. You already use delay(), so a bit more that always happens won't kill you.

For state change detection, there is an example in the IDE, and I wrote a version that is identical but different, and have a wokwi simulation you can visit to play with it.

See this post and the thread it is on

Try it here

Wokwi_badge State Change Detection


Count every detected press
Do something if any button is being pressed
Do something if all buttons are being pressed

You don't need to use if/else for the second two, but it does not hurt.

Now I think you will be unhappy with the delays causing the system to react slowly to the changing conditions in real life.

After you get this working, nothing you do will be wasted time including learning about state change detection, you can work on changing out the timing method (delays) for something that responds nearly instantly.

Part of that will be properly denouncing the buttons.

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.