hello i am newbie programmer arduino. i make project automatically water, soap, hand sanitizer and hand. i use statemachice like this, but i cannot make conditions I want. Can anyone help me to make condition like this? i dont understand make loop statemachine in void loop() thank you
conditions :
- condition 1 = when visitors come, PIR is active and turn on 0001.mp3
- condition 2 = detection of visitor body temperature display on LCD and turn on 0002.mp3 / 0003.mp3
- condition 3 = activate the relay (can be turned on / off continuously until condition 4)
- condition 4 = if the visitor is not detected it will return to condition 1
this code :
void loop() {
// put your main code here, to run repeatedly:
statemachine();
}
void statemachine(){
int statusIR1 = digitalRead(IR1);
int statusIR2 = digitalRead(IR2);
int statusIR3 = digitalRead(IR3);
int statusIR4 = digitalRead(IR4);
int statusIR5 = digitalRead(IR5);
val_PIR = digitalRead(PIR);
control_SM_prev = control_SM;
switch (control_SM){
case 0: //RESET
control_SM = 1;
case 1: // START PIR
if (val_PIR == HIGH) {
myDFPlayer.playMp3Folder(1);
Serial.println("Motion Detected");
delay(5000);
control_SM = 2;
}
else {
Serial.println("No Motion");
myDFPlayer.playMp3Folder(3);
delay(1000);
control_SM = 1;
}
case 2: // STAR MLX
delay(1000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Non-contact");
lcd.setCursor(2,1);
lcd.print("Suhu: ");
lcd.print(mlx.readObjectTempC());
lcd.print("C ");
delay(2000);
if(mlx.readObjectTempC() <= 37 and (mlx.readObjectTempC() >= 27)){
digitalWrite(laser, HIGH);
lcd.clear();
lcd.setCursor(1,0);
delay(500);
lcd.print("Suhu Tubuh Anda");
lcd.setCursor(5,1);
lcd.print("Normal");
delay(2000);
control_SM = 2;}
else if (mlx.readObjectTempC() >= 37){
digitalWrite(laser, HIGH);
lcd.clear();
lcd.setCursor(1,0);
delay(500);
lcd.print("Suhu Tubuh Anda");
lcd.setCursor(2,1);
lcd.print("Tidak Normal");
delay(2000);
control_SM = 2;}
else if (mlx.readObjectTempC() <= 25){
digitalWrite(laser, LOW);
lcd.clear();
lcd.setCursor(2,0);
delay(500);
lcd.print("auto WITEZER");
lcd.setCursor(5,1);
lcd.print("ONLINE");
delay(2000);
control_SM = 3;}
case 3: // STAR RELAY
// variabel statusIR yang menyimpan nilai dari hasil pembacaan pinIR
// Program sensor IR
aktifkan_IR:
if(statusIR1 == HIGH)
{
digitalWrite(PUMP1, LOW);
Serial.println("Pompa Air Dimatikan");
} else {
digitalWrite(PUMP1, HIGH);
Serial.println("Pompa Air Diaktifkan");
}
if(statusIR2 == HIGH) {
digitalWrite(PUMP2, LOW);
Serial.println("Pompa Sabun Dimatikan");
} else{
digitalWrite(PUMP2, HIGH);
Serial.println("Pompa Sabun Diaktifkan");
}
if(statusIR3 == HIGH) {
digitalWrite(PUMP3, LOW);
Serial.println("Pompa Hand Sanitizer Dimatikan");
} else {
digitalWrite(PUMP3, HIGH);
Serial.println("Pompa Hand Sanitizer Diaktifkan");
}
if(statusIR4 == HIGH) {
digitalWrite(HDRYER4, LOW);
Serial.println("Hand Dryer Dimatikan");
} else {
digitalWrite(HDRYER4, HIGH);
Serial.println("Hand Dryer Diaktifkan");
}
if(statusIR5 == LOW) {
control_SM = 3;
Serial.println("Pengunjung Terdeteksi");
} else {
control_SM = 4;
Serial.println("Pengunjung Tidak Terdeteksi");}
case 4: // TRIGGED
control_SM = 0;
}
}