Trouble executing control structure

Hello!

I'm trying to make a project using ultrasonic sensor and LCD with scrollable menu display. It's still unfinished, but so far there's no error message and I can change the display menu from one to the other without problem.

The problem is when I upload the program to the simulator and my arduino, it won't execute the first 'if' on the case 2. The other 'if's in case 2 works fine tho.

Is there any mistake on my codes?

#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 7, 5, 4, 3, 2);

#define tombolMenu 8     //Tombol menu pada pin D8
#define tombolOk 9       //Tombol ok pada pin D9
#define tombolNaik 10    //Tombol naik pada pin D10
#define tombolTurun 11   //Tombol turun pada pin D11

const int trigPin = 13;
const int echoPin = 12;
long durasi;
int jarak;
int tekanNaik;
int tekanTurun;
int tekanOk;

int jenisKelamin;
int umur = 30;

char menu;

void setup() {
  // put your setup code here, to run once:

  lcd.begin(16,2);
  pinMode(tombolMenu, INPUT_PULLUP);    
  pinMode(tombolOk, INPUT_PULLUP);
  pinMode(tombolNaik, INPUT_PULLUP);                
  pinMode(tombolTurun, INPUT_PULLUP);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  }

void loop() {
  // put your main code here, to run repeatedly:
  switch (menu){
  case 0:
  sensorJarak();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Kalibrasi");
  lcd.setCursor(4,1);
  lcd.print("cm");
  lcd.setCursor(0,1);
  lcd.print(jarak);
  if(digitalRead(tombolTurun) == LOW){   
    delay(200); 
    menu = 1;  
  }
  if(digitalRead(tombolNaik) == LOW){   
    delay(200); 
    menu = 3;  
  }
  break;
  
  case 1:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Umur");
  lcd.setCursor(0,1);
  lcd.print(umur);
  lcd.print("   tahun"); 
  if(digitalRead(tombolTurun) == LOW){  
    delay(200); 
    menu = 2;  
  }
  if(digitalRead(tombolNaik) == LOW){   
    delay(200); 
    menu = 0;  
  }
  if(digitalRead(tombolOk) == LOW){   
    delay(200); 
    menu = 4;  
  }
  break;
   
  case 2:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Jenis Kelamin");
  lcd.setCursor(0,1);
  lcd.print("Pilih"); 
  if(digitalRead(tombolOk) == LOW){   
    delay(200); 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Laki");  
  }
  if(digitalRead(tombolMenu) == LOW){
    delay(200);
    menu = 2;
  }
  if(digitalRead(tombolTurun) == LOW){   
    delay(200); 
    menu = 3;  
  }
  if(digitalRead(tombolNaik) == LOW){   
    delay(200); 
    menu = 1;  
  }
  break;
  
  case 3:
  lcd.clear();
  sensorJarak();
  lcd.setCursor(0,0);
  lcd.print("Tinggi lutut");
  lcd.setCursor(0,1);
  lcd.print(jarak);
  lcd.setCursor(4,1);
  lcd.print("cm"); 
  if(digitalRead(tombolTurun) == LOW){   
    delay(200); 
    menu = 0;  
  }
  if(digitalRead(tombolNaik) == LOW){   
    delay(200); 
    menu = 2;  
  }
  break;

  case 4:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Umur");
  lcd.setCursor(0,1);
  lcd.print(umur);
  lcd.print("   tahun"); 
  if(digitalRead (tombolNaik) == LOW){
      delay(200);
      umur = umur + 1;
      lcd.setCursor(0,1);
      lcd.print(umur); 
    }
    if(digitalRead (tombolTurun) == LOW){
      delay(200);
      umur = umur - 1;
      lcd.setCursor(0,1);
      lcd.print(umur);    
    }
    if(digitalRead (tombolMenu) == LOW){
      delay(200);
      menu = 1;
    }
  break;
  }
  }

void sensorJarak() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  durasi = pulseIn(echoPin, HIGH);
  jarak = durasi*0.034/2;
  delay(500);
}

if tombolOk is indeed LOW then you'll enter the if and as you don't change state you'll loop through this a lot of times and probably due to bouncing

the clear also erases the screen so you might have a dimm display

can you activate the Serial output?

add Serial.begin(115200); in the setup

and try this in your case 2

case 2:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Jenis Kelamin");
  lcd.setCursor(0,1);
  lcd.print("Pilih"); 
  if(digitalRead(tombolOk) == LOW){   
    Serial.println("IN FIRST IF OF CASE 2");
    delay(200); 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Laki");  
  }
  else if(digitalRead(tombolMenu) == LOW){
    delay(200);
    menu = 2;
  }
   else if(digitalRead(tombolTurun) == LOW){   
    delay(200); 
    menu = 3;  
  }
   else if(digitalRead(tombolNaik) == LOW){   
    delay(200); 
    menu = 1;  
  }
  break;

Note that you don't have 'else' in between your ifs, that might cause some issues if 2 digitalRead can be LOW at the same time, the first one will be ignored.

1 Like

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