I am Making an Voting Machine So How Could I Disable Switches So That I Can Receive One Vote at a time

I am making a voting machine using arduino UNO with four push buttons. Now I don't want to receive For push button inputs at the same time so how could I disable the other push buttons when one push button is active. I am facing some problem with the code can anyone help me out!

Thanks early!

Please provide your code in code tags(the <CODE/> button).

Also only read one button state at a time.

1 Like

How do you plan to prevent more than one button being pressed at the same time? After one button is pressed, how can you stop the other buttons being pressed? Some kind of sliding door mechanisms that covers the other 3 buttons? That sounds very difficult.

I would suggest that, instead of trying to prevent the other buttons being pressed, you simply ignore any other button presses, after one button is pressed. If you want to give immediate feedback to the people pressing the buttons, use illuminated buttons and only illuminate the button that was pressed first.

1 Like

CODE -

int s1 = 2;
int s2 = 3;
int s3 = 4;
int s4 = 5;
int b = 6;
int r = 8;
int t = 1000;
int vc = 0;
int t2 = 1000;/*Delay Time after each vote*/
int l1 = 9;
int l2 =10;
int l3 = 11;
int l4 = 12;
void setup() {
  Serial.begin(9600);
 
  pinMode(s1,INPUT_PULLUP);
  pinMode(s2,INPUT_PULLUP);
  pinMode(s3,INPUT_PULLUP);
  pinMode(s4,INPUT_PULLUP);
  pinMode(6,OUTPUT);
  pinMode(r,INPUT_PULLUP);
  pinMode(l1,OUTPUT);
  pinMode(l2,OUTPUT);
  pinMode(l3,OUTPUT);
  pinMode(l4,OUTPUT);
}

void loop() {
  if(digitalRead(s1) == LOW)
  {
    Serial.println("Candidate 1 Vote Registered");
    vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l1,HIGH);

    delay(t);
    }
  else {
    digitalWrite(b,LOW);
    digitalWrite(l1,LOW);
    }
  
  if(digitalRead(s2) == LOW)
  {
    Serial.println("Candidate 2 Vote Registered");
    vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l2,HIGH);
    delay(t);
    }
  else {
    digitalWrite(b,LOW);
    digitalWrite(l2,LOW);
    }
  if(digitalRead(s3) == LOW)
  {
    Serial.println("Candidate 3 Vote Registered");
     vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l3,HIGH);
    delay(t);
    }
  else {
    digitalWrite(b,LOW);
    digitalWrite(l3,LOW);
    }
  if(digitalRead(s4) == LOW)
  {
    Serial.println("Candidate 4 Vote Registered");
     vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l4,HIGH);
    delay(t);
    }
   else {
    digitalWrite(l4,LOW);
    digitalWrite(b,LOW);
    } 
   digitalWrite(b,LOW);
   digitalWrite(l1,LOW);
   digitalWrite(l2,LOW);
   digitalWrite(l3,LOW);
   digitalWrite(l4,LOW);
   delay(t2);

if(digitalRead(r) == LOW)
{
  vc = 0;
  Serial.println("Votecount has been reset");
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  }   
}

I Know Actually That it is difficult to do so but for you knowledge I am Student who is making a voting machine for school purpose now to tackle with fake votes some measures is needed to be taken ( actually to tackle those naughty boys ). I know its hard but still hoping for soultions :smiley:

Read about arrays…
An array of buttons might make your code simpler!

3 Likes

Hello rajarshi-mandal-1807

Welcome to the worldbest Arduino forum ever.

What are the full variable names s, b, r, t, vc and l?

Hello I am a student Arduino Programmer! :innocent:

I am using Arduino UNO (later I will use MEGA) with Some buttons to make a voting machine. Now the machine has one glitch that when two buttons are pressed around the same time ( within 1 second ) , Arduino is Picking up the systematically set up first as in the circuit not the one which has been pressed first in the instance of 1-0.5 second.

I am not sure that is it due to high delay between the vote time or so

Thank you Before
I Appreciate your replies :smiley:
CODE

int switch1 = 2;
int switch2 = 3;
int switch3 = 4;
int switch4 = 5;
int buzzer = 6;
int reset = 8;
int time = 5000;
int votecount = 0;
int time2 = 3000;/*Delay Time after each vote*/
int led1 = 9;
int led2 =10;
int led3 = 11;
int led4 = 12;
void setup() {
  Serial.begin(9600);
 
  pinMode(s1,INPUT_PULLUP);
  pinMode(s2,INPUT_PULLUP);
  pinMode(s3,INPUT_PULLUP);
  pinMode(s4,INPUT_PULLUP);
  pinMode(6,OUTPUT);
  pinMode(r,INPUT_PULLUP);
  pinMode(l1,OUTPUT);
  pinMode(l2,OUTPUT);
  pinMode(l3,OUTPUT);
  pinMode(l4,OUTPUT);
}

void loop() {
  if(digitalRead(s1) == LOW)
  {
    Serial.println("Candidate 1 Vote Registered");
    vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l1,HIGH);

    delay(t);
    }
  
  
  else if(digitalRead(s2) == LOW)
  {
    Serial.println("Candidate 2 Vote Registered");
    vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l2,HIGH);
    delay(t);
    }
 
  else if(digitalRead(s3) == LOW)
  {
    Serial.println("Candidate 3 Vote Registered");
     vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l3,HIGH);
    delay(t);
    }
 
  else if(digitalRead(s4) == LOW)
  {
    Serial.println("Candidate 4 Vote Registered");
     vc = vc + 1;
    Serial.println("Total Vote polled");
    Serial.println(vc);
    digitalWrite(b,HIGH);
    digitalWrite(l4,HIGH);
    delay(t);
    }
   
   digitalWrite(b,LOW);
   digitalWrite(l1,LOW);
   digitalWrite(l2,LOW);
   digitalWrite(l3,LOW);
   digitalWrite(l4,LOW);
   delay(t2);

if(digitalRead(r) == LOW)
{
  vc = 0;
  Serial.println("Votecount has been reset");
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  digitalWrite(b,HIGH);
  delay(250);
  digitalWrite(b,LOW);
  delay(250);
  }   
}

Why did you start another thread?

Please flag your own post and ask a moderator to attach it to the one you are already running on this topic?

a7

Short answer without lookin further, the advcie that you'll get no matter where you post:

You cannot get anywhere with a timing and switches problem if your code is so heavily reliant on going dumb for long periods of time.

That is to say you must find a way to eliminate the use of delay() in the code.

a7

1 Like

Okay OKay Dont be so rude I am Just newbie btw Thanks for your help

Your two topics on the same or similar subject have been merged.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future.

Thank you.

1 Like

s for switch
b for buzzer
r for reset button for vote count
vc for votecount
t for time
t2 for another time
l for leds (l1,l2,l3,l4)

Thanks for your interest in helping out :smile:

to your question

Say someone comes and press a button to vote
then someone else comes and wants to vote. How will your code know to accept the new vote?
do you plan to have a button to activate the voting capability?

using 1 letter name for variables is just a pain for other to read and for you to maintain... searching for s or b for example will lead to tons of matches...

1 Like

Modify your sketch and repost.

done

please anyone close this topic as i have got the solution

Does the compiler translate the programme?

Well please share the solution that you have got!

Your problem was an interesting one, ppl pointed out the challenges, so I am not the only one wondering where you landed up.

TIA

a7

i have got the solution it had something to do with else if as i was only using if instead of else if
thanks man