This slideswitch is not turning on the Piezo buzzer as intended

Hello, I am working on a home security system in Arduino R3 using Tinkercad for a school project. I'm just stuck on this slide switch problem. According to the code, the Piezo buzzer is supposed to turn on if the slide switch is in the HIGH state, but even if I turn it to the HIGH state during the simulation it doesn't turn on. The only time it works is when it starts at the HIGH state before running the simulation.

I tried turning the variable into a static variable like how other solutions suggested, but it still didn't work.

Screenshot 2021-04-10 164002.png

Screenshot 2021-04-10 164002.png

Neither the value of checkDoor or checkArmed will change in loop() as posted so the test of its value makes no sense

Please post your complete sketch and a wiring diagram of your circuit

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to read, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

int sensorValue = 0;
//int potentiometer1 = 0;

bool checkArmed = false;

int checkDoor = digitalRead(13);

int firstNum = 0;
int secondNum = 0;

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int displaystate = 0; 

int distanceThreshold = 0;
int cm = 0;
int inches = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(13, INPUT); //slideswitch
  pinMode(A0, INPUT);
  pinMode(9, OUTPUT);
  
  lcd.begin(16, 2); //LCD
  pinMode(10, INPUT);
  pinMode(9, INPUT);
  
  pinMode(7,OUTPUT); // piezo 
  pinMode(A1, INPUT);
}


void loop()
{
  //photoresistor();
  //display();
  //distance_sensor();
  if(checkDoor == HIGH)
  {
    tone(7,440,5000);
  }
  Serial.print(checkArmed);
  Serial.println(checkDoor);
}
/*
void photoresistor()
{
  // read the value from the sensor
  sensorValue = analogRead(A0);
  // print the sensor reading so you know its range
  //Serial.println(sensorValue);
  // map the sensor reading to a range for the LED
  analogWrite(9, map(sensorValue,0,1023,0,255));
  if(sensorValue > 0 && sensorValue < 858)
  {
    digitalWrite(9,HIGH);
  }
  else
  {
    digitalWrite(9,LOW);
  }
}
*/
/*
void potentiometer()
{
  potentiometer1 = analogRead(A1);
  // print the sensor reading so you know its range
  Serial.println(potentiometer1);
  // map the sensor reading to a range for the LED
  analogWrite(9, map(potentiometer1,0,1023,0,255));
} */

/*
long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
  //I got this from tinkercad tutorial about ultrasonic distance sensor.
}
*/

void distance_sensor()
{
  //Serial.print(digitalRead(13));
  // set threshold distance to activate LEDs
  //distanceThreshold = 100;
  // measure the ping time in cm
  //cm = 0.01723 * readUltrasonicDistance(7, 7);
  // convert to inches by dividing by 2.54
  //inches = (cm / 2.54);
  //Serial.print(cm);
  //Serial.print("cm, ");
  //Serial.print(inches);
  //Serial.println("in");
  /*
  if(cm >= distanceThreshold && cm < 200 && displaystate == 1)
  {
    //led blinks
    Serial.println("if statement is running");
    noTone(7);
    //tone(7,440,5000);
    delay(2000);
    
    noTone(7);
  }
  else
  {
    //led turns on
    //digitalWrite(A1, HIGH);
    delay(1000);
       
  }
  
  delay(100);
  */
  
  if(checkArmed == true && checkDoor == HIGH)
  {
    tone(7,440,5000);
  }
  
}
void display()
{
  firstNum= digitalRead(10);
  secondNum = digitalRead(8);
  //Serial.print(firstNum);
  //Serial.println(secondNum);
  checkArmed = true;
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("ARMED");
  displaystate = 1;
  delay(500);

  /*
  checkArmed = false;
  lcd.clear();
  lcd.setCursor(0,1);
  lcd.print("UNARMED");
  displaystate = 0;
  delay(500);
  */
  
}

Sorry about earlier, here is the complete code, the only part I'm worried about is the loop(). I'm just trying to test the slide switch and Piezo buzzer to see if they work, but it doesn't even seem to work as intended.

You need to read the state of pin 13 each time through loop() if you want the value of checkDoor to ever change. You should also look at the state change detection example in the IDE (File->examples->02.digital->StateChangeDetection) since you will only want to set the buzzer when the switch goes HIGH, not continually set it while the switch is HIGH

so instead of creating the variable at the very top of the code, i should just make it in the loop statement?

You can declare the variables at the top of the code but unless you read the state of the inputs again before you test them then their state will not change

ohhhhhhhhhhhh that makes so much sense, I will try that now, thanks!

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