Arduino problem .. Any help?

The ultrasound sensor will be always ON except when enter the correct password .
If detects any object less than 20cm , will activate the buzzer sound for one second… BUT if the correct password entered 789 ,
servo motor will rotates 90 deg and ultrasound should not activates the buzzer . # button returns the servo to its locked position

Green led is ON always … Green Led is ON when the pass is correct .

The problem : the buzzer sound is always ON !

#include <Keypad.h>
#include <Servo.h>
Servo servo_Motor;
char* password = “789”;
int position = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int duration, distance; //to measure the distance and time taken
char keys[ROWS][COLS] = {
{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{’ ', ‘0’, ‘#’, ‘D’}
};
byte rowPins[ROWS] = { 10,9,8,7 };
byte colPins[COLS] = { 6,5,4,3 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
int redPin = 12;
int greenPin = 13;
// Pins
const int TRIG_PIN = A1;
const int ECHO_PIN = A2;

const int Buzzer = 2;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
servo_Motor.attach(11);
setLocked(true);
// The Trigger pin will tell the sensor to range find
pinMode(TRIG_PIN, OUTPUT);
pinMode(Buzzer, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
Serial.begin(9600);
}

void loop()
{

char key = keypad.getKey();
if (key == ‘#’)
{
position = 0;
setLocked(true);
}
if (key == password[position])
{
Serial.println(key);
position ++;
}
if (position == 3)
{
setLocked(false);
}
delay(100);
}

void setLocked(int locked)
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
servo_Motor.write(11);
myfunc();
Serial.println(“Locked”);
delay(100);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
servo_Motor.write(90);
digitalWrite(Buzzer,LOW);
Serial.println(“Unlocked”);
delay(1000);

}
}
void sonar_distance()
{
// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = (duration/2) / 29.1;
}

void myfunc()

{

if (distance <= 20 || distance >= 0)
{

      digitalWrite(Buzzer,HIGH);
      delay(2000);
      
    }

}

I tried to change || to && but same result :frowning: … Also replaced the buzzer with a led , I noticed the led always ON .

Hello @Khaled_1758
Could you also upload the LiitleBits connection scheme ?
It would be easier to troubleshoot your problem :wink:

Thanks for your reply … I did the circuit now using FRITZING … The problem I have as I mentioned above , the buzzer still ON all the time unless I enter the correct pass

Hi @Khaled_1758, how does littleBits fit into this project?

I see neither the buzzer nor ultrasonic in your circuit. When you posted the code, it got corrupted by the forum software. You need to use the code icon in the menu to keep that from happening.

note: the image has been modified from the op

Thanks , I uploaded another image …

Could you please help me solve this issue