Arduino switch not working

Hello I have a problem I use a ic2 lcd and ultrasonic sensor but it malfunctions

Here's problem

I need the button(its a switch like button with two terminals not a 4 terminal tactile button) to display something but its just does it without a button press

Important points
The switch button is connected with out a resistor

#include <hd44780.h>

#include <Wire.h> 
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <ezButton.h>



//turn on an LED based on distance measured by
  //ultrasonic sensor

    


  
  int pinled=9;
  const  int trigger=6;
  const  int echo=7;
  float  distance;
  float dist_inches;
  ezButton toggleSwitch(5);  

    hd44780_I2Cexp lcd;
  
  void setup()
{  Serial.begin(9600);
    pinMode(trigger,OUTPUT);
    pinMode(echo,INPUT);
    pinMode(pinled,OUTPUT);
    toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("hello user code");
    delay(9000);
    lcd.setCursor(0,0);
    lcd.print("LOADING JAVA>____");
    delay(900);
    lcd.setCursor(0,0);
    lcd.print("LOADING JAVA>>____");
    delay(900);
    lcd.setCursor(0,0);
    lcd.print("LOADING JAVA>>>___");
    delay(900);
    lcd.setCursor(0,0);
    lcd.print("LOADING JAVA>>>>__");
    delay(900);
    lcd.setCursor(0,0);
    lcd.print("JAVA IS FINISH_>");
    lcd.setCursor(1, 1);
    lcd.print("JAVA IS NOW UP____");
    delay(3000);
    delay(900);
    lcd.setCursor(0,0);
    lcd.print("WELCOME TO N.M");
    lcd.setCursor(1,1);
    lcd.print("MOTORS VIRTUAL__");


  }
  
  void loop()
{  // Trigger the sensor to start measurement
    // Set up trigger
    lights1();
    digitalWrite(trigger,LOW);
    delayMicroseconds(5);
    
    //Start Measurement
    digitalWrite(trigger,HIGH);
    delayMicroseconds(10);
    digitalWrite(trigger,LOW);
    
    // Acquire and convert to inches
    distance=pulseIn(echo,HIGH);
    distance=distance*0.0001657;
    dist_inches=distance*39.37;
 
    if(dist_inches < 20){ //if distance of object is less than 6 inches
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Hello dear user___?_____");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("PLEASE PHONE N0_");
      delay(200);
      lcd.setCursor(1, 1);
      lcd.print("0719000873_____");
      delay(989);
      lcd.setCursor(0, 0);
      lcd.print("SIFANG TRACTORS__");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("TORS KOBOTA TRACTORS");
      delay(999);
      lcd.setCursor(0,0);
      lcd.print("KOBOTA TRACTORS___");
      delay(900);
      lcd.clear();
      lcd.setCursor(1,0);
      lcd.print("!PARTS ON SALE!!!!!__");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("0719000873_____");
      delay(99);
      lcd.setCursor(0, 1);
      lcd.print("UDAYAKUMARA");
      delay(900);
      lcd.clear();
            }
    else{ 
      lcd.setCursor(0, 0);
      lcd.print("Hello dear user___?_____");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("Use this machine___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("HELLO USE ME!!_____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("PLEASE USE ME");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("N.M MOTORS____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("VIRTUAL WORKS___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("NADHILAWEB_________");
      delay(1000);
        }
    if(dist_inches <35){
      lcd.setCursor(0, 0);
      lcd.print("Hello dear user___?_____");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("AUTO SEWAWA____");
      delay(200);
      lcd.setCursor(1, 1);
      lcd.print("sifang hub(hub only)______");
      delay(989);
      lcd.setCursor(1, 1);
      lcd.print("RS:1500____________");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("SIFANG ROTARY________");
      delay(999);
      lcd.clear();lcd.setCursor(0,0);
      lcd.print("big case_______");
      delay(900);
      lcd.clear();
      lcd.setCursor(1,0);
      lcd.print("RS:2750_____________");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("0719000873_____");
      delay(9900);
      lcd.setCursor(0, 1);
      lcd.print("UDAYAKUMARA");
      delay(900);
      lcd.clear();
        }
    else{ 
      lcd.setCursor(0, 0);
      lcd.print("Hello dear user___?_____");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("Use this machine___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("HELLO USE ME!!_____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("PLEASE USE ME");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("N.M MOTORS____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("VIRTUAL WORKS___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("NADHILAWEB_________");
      delay(1000);
      
      
      lcd.setCursor(0, 0);
      lcd.print("MUSIC ON S3RL!!!!!");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("i could be a waifu");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("HELLO USE ME!!_____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("waifu waifu waifu waifu waifu");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("FU WAIFU WAIFU WAIFU WAIFU WAIFU");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("U WAIFU WAIFU WAIFU WAIFU");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("WAIFU WAIFU WAIFU WAIFU WAIFU");
      
      
      }
   //{
   // }     

  
}
    

void lights1(){toggleSwitch.loop(); // MUST call the loop() function first

  if (toggleSwitch.isPressed())
           Serial.println("The switch: OFF -> ON");
    
    lcd.setCursor(0, 0);
    lcd.print("PRICE LIST BY ");
    delay(900);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("LIST BY NADHILAWEB");
    delay(900);
    lcd.setCursor(1, 1);
    lcd.print("SIFANG STUFF");
    delay(900);
    lcd.clear();
    lcd.setCursor(1, 1);
    lcd.print("loading list................");
    delay(900);
    lcd.clear();
    lcd.setCursor(1, 1);
    lcd.print("SIFANG BRAKE KNOB S ");
    lcd.setCursor(0, 1);
    lcd.print("RS:150");
    lcd.clear();
    delay(900);
    lcd.setCursor(1, 1);
    lcd.print("SIFANG BRAKE KNOB L ");
    lcd.setCursor(0, 1);
    lcd.print("RS:200");
    delay(900);
    lcd.clear();
    lcd.setCursor(1, 1);
    lcd.print("SIFANG disel lid");
    lcd.setCursor(0, 1);
    lcd.print("RS:360_");


  if (toggleSwitch.isReleased())
    Serial.println("The switch: ON -> OFF");
    lcd.setCursor(0, 0);
      lcd.print("Hello dear user___?_____");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("Use this machine___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("HELLO USE ME!!_____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("PLEASE USE ME");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("N.M MOTORS____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("VIRTUAL WORKS___");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("NADHILAWEB_________");
      delay(1000);
      
      
      lcd.setCursor(0, 0);
      lcd.print("MUSIC ON S3RL!!!!!");
      delay(900);
      lcd.setCursor(0, 0);
      lcd.print("i could be a waifu");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("HELLO USE ME!!_____");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("waifu waifu waifu waifu waifu");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("FU WAIFU WAIFU WAIFU WAIFU WAIFU");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("U WAIFU WAIFU WAIFU WAIFU");
      delay(900);
      lcd.setCursor(1, 1);
      lcd.print("WAIFU WAIFU WAIFU WAIFU WAIFU");
      lcd.clear();

  int state = toggleSwitch.getState();
  if (state == HIGH)
    Serial.println("The switch: OFF");

  else
    Serial.println("The switch: ON");}


PLEASE HELP ME!!!!!!!!!!!!

if you use if without braces, the condition only affects the one line immediately following the operator

PS I replaced "parenthesis" word to braces as @J-M-L mentioned

1 Like

SO WHAT SHOULD I DO I AM A VERY BIG NEWBIE
please help me

like you did with other if() in the code ➜ use braces (not parentheses) {} to group together what belongs to the if clause

if (condition) {
  // code if condition is true
  ...
} else {
 // code if condition is false
  ...
}
1 Like

Use the language reference to get correct syntax

read the manual

1 Like

When you use any button libraries, please do not use delay() function. It will block operation of button libraries

That's the very last problem on that code... :wink:

How is the button wired? Do you have a pullup or pulldown resistor connected to pin 5?

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