Ultrasonic sensor code not working with remaining components

#include <SR04.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define Password_Length 5
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 16, 2);

int pos = 0;
int LED = 13;
int trigPin = 11;
int echoPin = 10;
long distance;
long duration;

char Data[Password_Length];
char Master[Password_Length] = "1234";
byte data_count = 0, master_count = 0;

bool Pass_is_good;
bool door = false;
char customKey;

/*---preparing keypad---*/

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {0, 1, 2, 3};
byte colPins[COLS] = {4, 5, 6, 7};

Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

/*--- Main Action ---*/
void setup()
{
  myservo.attach(9);
  ServoClose();
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.print("Protected Door");
  loading("Loading");
  lcd.clear();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);

}
void loop()
{
  if (door == true)
  {
    customKey = customKeypad.getKey();
    if (customKey == '#')
    {
      lcd.clear();
      ServoClose();
      lcd.print("Door is closed");
      delay(3000);
      door = false;
    }
  }
  else
    Open();
  
    
}
void loading(char msg[])
{
  lcd.setCursor(0, 1);
  lcd.print(msg);

  for (int i = 0; i < 9; i++)
  {
    delay(1000);
    lcd.print(".");
  }
}
void clearData()
{
  while (data_count != 0)
  {
    Data[data_count--] = 0;
  }
  return;
}

void ServoClose()
{
  for (pos = 90; pos >= 0; pos -= 10)
  {
    myservo.write(pos);
    digitalWrite(LED, 0);
    delay(15);
  }
}

void ServoOpen()
{
  for (pos = 0; pos <= 90; pos += 10)
  {
    myservo.write(pos);
    digitalWrite(LED, 1);
    delay(15);
  }

}

v

oid ultra_sonic()

{

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration*0.034/2;

  }


void Open()
{
  lcd.setCursor(0, 0);
  lcd.print("Enter Password");

  customKey = customKeypad.getKey();
  if (customKey)
  {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }

  if (data_count == Password_Length - 1)
  {
    Data[data_count] = '\0'; // Null-terminate the data

    if (strcmp(Data, Master) == 0)
    {
      lcd.clear();
      ServoOpen();
      lcd.print(" Door is Open ");
      door = true;
      delay(5000);
      loading("Waiting");
      lcd.clear();
      lcd.print(" Time is up! ");
      delay(1000);
      ServoClose();
      door = false;
      
    }
    else
    {
 lcd.clear();
      lcd.print(" Wrong Password ");
      door = false;
    
    }
    delay(1000);
    lcd.clear();
    clearData();
  }
}

@finderofpeace welcome to the forum.

Is that all one sketch? If so, you should just post it as one code section.

Could you say if you tested all the hardware parts separately, and they all work?

Can you draw a schematic, show all parts and how they are connected, with particular attention to sources of power and how they are wired to the parts that need it?

Hand drawn, take a picture and post it is easiest and fastest and entirely adequate.

TIA

a7

Hello finderofpeace

Welcome to the worldbest Arduino forum ever.

I assume that you have written the programme by yourself, then it is quite easy to find the error:

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code to see the values of variables and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Before asking questions:


  • Questioners know what their hardware and software looks like but the volunteers here do not; you need to fully explain what’s not working, and how it is supposed to work.
  • Please read all the posting guidelines before asking your questions; follow these guidelines in your post.

Hardware

  • As always, show us a good schematic of your proposed circuit.
  • Show us good images of your ‘actual’ wiring.
  • Give WEB links to your major components.

Software

  • In the Arduino IDE, use Ctrl T or CMD T to format your code, then copy the complete sketch.
  • Use the < CODE / > icon from the ‘posting menu’ to attach your copied sketch.

When you follow the posting guidelines, volunteers can be more effective.


Where are you calling ultra_sonic( ) ?

Also posted here. c++ - Ultrasonic sensor code not working with remaining components - Stack Overflow

It has a little bit more text there. OP at least explained what the code was supposed to do.

1 Like

OP went on my ignore list.

2 Likes

i apologize guys here is a tinkercad design

yes the ultrasonic has been tested it works completely fine alone but when i add it to this code it does not detect movement at all

This question has a solution on Stack Overflow.

1 Like

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