Hi, I'm working on a school project and my code seems to be buggy. variable values are not supposed to be adding up when in the main menu or inside a category. But when for example, I'm in menu 3 and vote for CD1 there, then that particular variable should get added up. But it seems that CD3 in every menu get added up when choosing the sub-menu or voting. All CD3 get added up.
If anyone knows how to fix this, pls help.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define S1 7
#define S2 6
#define S3 5
#define S4 4
#define S5 3
#define S6 2
int vote1 = 0;
int vote2 = 0;
int vote3 = 0;
int vote4 = 0;
int vote5 = 0;
int vote6 = 0;
int vote7 = 0;
int vote8 = 0;
int vote9 = 0;
int vote10 = 0;
int vote11 = 0;
int vote12 = 0;
int vote13 = 0;
int vote14 = 0;
int vote15 = 0;
int vote16 = 0;
int stat = 0;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.backlight();
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(S4, INPUT);
pinMode(S5, INPUT);
pinMode(S6, INPUT);
digitalWrite(S1, HIGH);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
digitalWrite(S4, HIGH);
digitalWrite(S5, HIGH);
digitalWrite(S6, HIGH);
}
void loop()
{
/* Serial.print("1:");
Serial.println(vote13);
Serial.print("2:");
Serial.println(vote14);
Serial.print("3:");
Serial.println(vote15);
Serial.print("4:");
Serial.println(vote16);
delay(100);*/
if (stat == 0) {
lcd.setCursor(0, 0);
lcd.print("VCE");
lcd.setCursor(1, 1);
lcd.setCursor(4, 0);
lcd.print("CCA");
lcd.setCursor(5, 1);
lcd.setCursor(8, 0);
lcd.print("DIS");
lcd.setCursor(9, 1);
lcd.setCursor(12, 0);
lcd.print("SPT");
lcd.setCursor(13, 1);
}
if (digitalRead(S6) == 0 && stat >= 1) {
lcd.clear();
stat = 0;
}
if (digitalRead(S1) == 0 && stat == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pick");
lcd.setCursor(0, 1);
lcd.print("Candidate");
stat++;
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CD1");
lcd.setCursor(6, 0);
lcd.print("CD2");
lcd.setCursor(13, 0);
lcd.print("CD3");
}
if (digitalRead(S2) == 0 && stat == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pick");
lcd.setCursor(0, 1);
lcd.print("Candidate");
stat = 2;
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CD1");
lcd.setCursor(6, 0);
lcd.print("CD2");
}
if (digitalRead(S3) == 0 && stat == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pick");
lcd.setCursor(0, 1);
lcd.print("Candidate");
stat=3;
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CD1");
lcd.setCursor(6, 0);
lcd.print("CD2");
lcd.setCursor(13, 0);
lcd.print("CD3");
}
if (digitalRead(S4) == 0 && stat == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pick");
lcd.setCursor(0, 1);
lcd.print("Candidate");
stat = 4;
delay(1500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CD1");
lcd.setCursor(9, 0);
lcd.print("CD2");
}
if (stat == 1 ); {
if (digitalRead(S1) == 0)
vote1++;
delay(10);
while (digitalRead(S1) == 0);
if (digitalRead(S2) == 0)
vote2++;
delay(10);
while (digitalRead(S2) == 0);
if (digitalRead(S3) == 0)
vote3++;
delay(10);
while (digitalRead(S3) == 0);
if (digitalRead(S4) == 0)
delay(10);
else if (stat != 1 && stat!=2 && stat!=3 && stat!=4 ){
}}
if (stat == 2 ); {
if (digitalRead(S1) == 0)
vote5++;
delay(10);
while (digitalRead(S1) == 0);
if (digitalRead(S2) == 0)
vote6++;
delay(10);
while (digitalRead(S2) == 0);
if (digitalRead(S3) == 0)
delay(10);
while (digitalRead(S3) == 0);
if (digitalRead(S4) == 0)
delay(10);
else if (stat != 1 && stat!=2 && stat!=3 && stat!=4 ){
}}
if (stat == 3 ); {
if (digitalRead(S1) == 0)
vote9++;
delay(10);
while (digitalRead(S1) == 0);
if (digitalRead(S2) == 0)
vote10++;
delay(10);
while (digitalRead(S2) == 0);
if (digitalRead(S3) == 0)
delay(10);
while (digitalRead(S3) == 0);
if (digitalRead(S4) == 0)
delay(10);
else if (stat != 1 && stat!=2 && stat!=3 && stat!=4 ){
}}
if (stat == 4 ); {
if (digitalRead(S1) == 0)
vote13++;
delay(10);
while (digitalRead(S1) == 0);
if (digitalRead(S2) == 0)
vote14++;
delay(10);
while (digitalRead(S2) == 0);
if (digitalRead(S3) == 0)
delay(10);
while (digitalRead(S3) == 0);
if (digitalRead(S4) == 0)
delay(10);
else if (stat != 1 && stat!=2 && stat!=3 && stat!=4 ){
}}
while (digitalRead(S4) == 0);
if (digitalRead(S5) == 0 && stat == 1)
{
int vote = vote1 + vote2 + vote3 + vote4;
if (vote)
{
if ((vote1 > vote2 && vote1 > vote3 && vote1 > vote4))
{
lcd.clear();
lcd.print("VICE:CD1");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote1);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote2 > vote1 && vote2 > vote3 && vote2 > vote4))
{
lcd.clear();
lcd.print("VICE:CD2");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote2);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote3 > vote1 && vote3 > vote2 && vote3 > vote4))
{
lcd.clear();
lcd.print("VICE:CD3");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote3);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote4 > vote1 && vote4 > vote2 && vote4 > vote3)
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Error B4 E1");
lcd.print(vote3);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote1 == 0 && vote2 == 0 && vote3 == 0 && vote4 == 0) {
{
lcd.clear();
lcd.print(" Tie Up Or ");
lcd.setCursor(0, 1);
lcd.print(" No Result ");
delay(3000);
lcd.clear();
stat = 0;
}
}
lcd.print("System reset triggered");
Serial.println("category 1");
Serial.print("CD1 ");
Serial.println(vote1);
Serial.print("CD2 ");
Serial.println(vote2);
Serial.print("CD3 ");
Serial.println(vote3);
Serial.println("category 2");
Serial.print("CD1 ");
Serial.println(vote5);
Serial.print("CD2 ");
Serial.println(vote6);
Serial.println("category 3");
Serial.print("CD1 ");
Serial.println(vote9);
Serial.print("CD2 ");
Serial.println(vote10);
Serial.print("CD3");
Serial.println(vote11);
Serial.println("category 4");
Serial.print("CD1");
Serial.println(vote13);
Serial.print("CD2 ");
Serial.println(vote14);
delay(750);
}
else
{
lcd.clear();
lcd.print("No Voting....");
stat = 0;
delay(3000);
lcd.clear();
}
vote1 = 0; vote2 = 0; vote3 = 0; vote4 = 0, vote = 0;
lcd.clear();
}
while (digitalRead(S4) == 0);
if (digitalRead(S5) == 0 && stat == 2)
{
int voterr = vote5 + vote6 + vote7 + vote8;
if (voterr)
{
if ((vote5 > vote6 && vote5 > vote7 && vote5 > vote8))
{
lcd.clear();
lcd.print("CCA: CD1");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote5);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote6 > vote5 && vote6 > vote7 && vote6 > vote8))
{
lcd.clear();
lcd.print("CCA: CD2");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote6);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote7 > vote5 && vote7 > vote6 && vote7 > vote8))
{
lcd.clear();
lcd.print("DISC:CD3");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote11);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote8 > vote5 && vote8 > vote6 && vote8 > vote7)
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Error B4 E21");
lcd.setCursor(0,1);
lcd.print(vote12);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote5 == 0 && vote6 == 0 && vote7 == 0 && vote8 == 0) {
{
lcd.clear();
lcd.print(" Tie Up Or ");
lcd.setCursor(0, 1);
lcd.print(" No Result ");
delay(3000);
lcd.clear();
stat = 0;
}
}
lcd.print("System reset triggered");
Serial.println("category 1");
Serial.print("CD1 ");
Serial.println(vote1);
Serial.print("CD2 ");
Serial.println(vote2);
Serial.print("CD3 ");
Serial.println(vote3);
Serial.println("category 2");
Serial.print("CD1 ");
Serial.println(vote5);
Serial.print("CD2 ");
Serial.println(vote6);
Serial.println("category 3");
Serial.print("CD1 ");
Serial.println(vote9);
Serial.print("CD2 ");
Serial.println(vote10);
Serial.print("CD3");
Serial.println(vote11);
Serial.println("category 4");
Serial.print("CD1");
Serial.println(vote13);
Serial.print("CD2 ");
Serial.println(vote14);
delay(750);
}
else
{
lcd.clear();
lcd.print("No Voting....");
stat = 0;
delay(3000);
lcd.clear();
}
vote5 = 0; vote6 = 0; vote7 = 0; vote8 = 0, voterr = 0;
lcd.clear();
}
while (digitalRead(S4) == 0);
if (digitalRead(S5) == 0 && stat == 3)
{
int votes = vote9 + vote10 + vote11 + vote12;
if (votes)
{
if ((vote9 > vote10 && vote9 > vote11 && vote9 > vote12))
{
lcd.clear();
lcd.print("DISC: CD1");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote9);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote10 > vote9 && vote10 > vote11 && vote10 > vote12))
{
lcd.clear();
lcd.print("DISC:CD2");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote10);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote11 > vote9 && vote11 > vote10 && vote11 > vote12))
{
lcd.clear();
lcd.print("DISC:CD3");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote11);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote12 > vote9 && vote12 > vote10 && vote12 > vote11)
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Error B4 E21");
lcd.setCursor(0,1);
lcd.print(vote12);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote9 == 0 && vote10 == 0 && vote11 == 0 && vote12 == 0) {
{
lcd.clear();
lcd.print(" Tie Up Or ");
lcd.setCursor(0, 1);
lcd.print(" No Result ");
delay(3000);
lcd.clear();
stat = 0;
}
}
lcd.print("System reset triggered");
Serial.println("category 1");
Serial.print("CD1 ");
Serial.println(vote1);
Serial.print("CD2 ");
Serial.println(vote2);
Serial.print("CD3 ");
Serial.println(vote3);
Serial.println("category 2");
Serial.print("CD1 ");
Serial.println(vote5);
Serial.print("CD2 ");
Serial.println(vote6);
Serial.println("category 3");
Serial.print("CD1 ");
Serial.println(vote9);
Serial.print("CD2 ");
Serial.println(vote10);
Serial.print("CD3");
Serial.println(vote11);
Serial.println("category 4");
Serial.print("CD1");
Serial.println(vote13);
Serial.print("CD2 ");
Serial.println(vote14);
delay(750);
}
else
{
lcd.clear();
lcd.print("No Voting....");
stat = 0;
delay(3000);
lcd.clear();
}
vote9 = 0; vote10 = 0; vote11 = 0; vote12 = 0, votes = 0;
lcd.clear();
}
while (digitalRead(S4) == 0);
if (digitalRead(S5) == 0 && stat == 4)
{
int votess = vote13 + vote14 + vote15 + vote16;
if (votess)
{
if ((vote13 > vote14 && vote13 > vote15 && vote13 > vote16))
{
lcd.clear();
lcd.print("SPRT:CD1");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote13);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote14 > vote13 && vote14 > vote15 && vote14 > vote16))
{
lcd.clear();
lcd.print("SPRT: CD2");
lcd.setCursor(0, 1);
lcd.print("is Winner");
lcd.setCursor(10, 1);
lcd.print(vote14);
delay(3000);
lcd.clear();
stat = 0;
}
else if ((vote15 > vote13 && vote15 > vote14 && vote15 > vote16))
{
lcd.clear();
lcd.print("Error b4 e31");
lcd.setCursor(0, 1);
lcd.setCursor(10, 1);
lcd.print(vote15);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote16 > vote13 && vote16 > vote14 && vote16 > vote15)
{
lcd.setCursor(0, 0);
lcd.clear();
lcd.print("Error B4 E32");
lcd.print(vote16);
delay(3000);
lcd.clear();
stat = 0;
}
else if (vote13 == 0 && vote14 == 0 && vote15 == 0 && vote16 == 0) {
{
lcd.clear();
lcd.print(" Tie Up Or ");
lcd.setCursor(0, 1);
lcd.print(" No Result ");
delay(3000);
lcd.clear();
}
}
lcd.print("System reset triggered");
Serial.println("category 1");
Serial.print("CD1 ");
Serial.println(vote1);
Serial.print("CD2 ");
Serial.println(vote2);
Serial.print("CD3 ");
Serial.println(vote3);
Serial.println("category 2");
Serial.print("CD1 ");
Serial.println(vote5);
Serial.print("CD2 ");
Serial.println(vote6);
Serial.println("category 3");
Serial.print("CD1 ");
Serial.println(vote9);
Serial.print("CD2 ");
Serial.println(vote10);
Serial.print("CD3");
Serial.println(vote11);
Serial.println("category 4");
Serial.print("CD1");
Serial.println(vote13);
Serial.print("CD2 ");
Serial.println(vote14);
delay(750);
}
else
{
lcd.clear();
lcd.print("No Voting....");
stat = 0;
delay(3000);
lcd.clear();
}
vote13 = 0; vote14 = 0; vote15 = 0; vote16 = 0, votess = 0;
lcd.clear();
}
}```