0

I have faced an issue where the shock sensor is not running in the file. My idea is that when the ultrasonic sensor sense that when it is 4cm, the garage door will open. When the car reverses, the obstacle avoidance sensor will check if there is obstacle behind. These two sensors are working fine.

After that, shock sensor will be activated to see if the car has hit the wall. But from shock sensor part onwards, the shock sensor does not run. I troubleshoot by putting the shock sensor code into another sketch and it works.

I have no idea where I have done wrong. I appreciate any help

Update I have modified the code to accommadate interrupts which is advised by CodeGorilla. I need help with how can I let the interrupt know that they can execute when it reaches the if (distance == 4).

 #include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display


const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

const int shockPin = 3; // Use Pin 3 as Input


void setup() {

  Serial.begin(9600);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Distance left:");


  // Ultrasonic Sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


  // Obstacle Avoidance Sensor
  pinMode(8, INPUT);// set pin as input

  // Shock Sensor
  pinMode (shockPin, INPUT) ; // input from the KY-002

  attachInterrupt(digitalPinToInterrupt(3), detectShock, CHANGE);
  attachInterrupt(digitalPinToInterrupt(8), senseObstacle, CHANGE);
}

void detectShock ()
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Detection: ");
  if (digitalRead(shockPin) == LOW) // If we're in an alarm state
  {
    lcd.setCursor(0, 1);
    lcd.print("Shock exists");
  }
  else
  {
    lcd.setCursor(0, 1);
    lcd.print("Shock is absent");
  }
}

void senseObstacle() {
  lcd.clear();
  lcd.setCursor(0, 0);
  //delay(300);
  lcd.print("Detection:");
  if (digitalRead(8) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Obstacle Present");
  } else {
    lcd.setCursor(0, 1);
    lcd.print("No Obstacle");
  }


void loop() {

  // Clears the trigPin
  digitalWrite(trigPin , LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin , LOW);

  // Reads the echoPin, returns the sound wave
  // travel time in microseconds
  duration = pulseIn( echoPin , HIGH);

  distance = duration * 0.034 / 2;

  lcd.setCursor(0, 0);
  lcd.print("Distance left: ");

  lcd.setCursor(0, 1);
  lcd.print(distance);
  lcd.print(" CM");


  if (distance == 4) {
    // I am lost at here.
    lcd.setCursor(0, 1);
    lcd.print("Door Opens");
    delay(500);
    // Once the car enters, the obstacle avoidance sensor checks if there are obstacles in the way..
    delay(5000);
    lcd.clear();
  }

}



}
  • Do you have a make and model or datasheet for the shock sensor? – Code Gorilla Jun 19 '18 at 12:58
  • The model of shock sensor is ky-002. I havent store the data into the database yet. @CodeGorilla – Sophia Jun 19 '18 at 13:00
  • what is the purpose of exit(0)? – Juraj Jun 19 '18 at 14:05
  • 1
    First thing I would try is getting rid of delay(). For this, a finite state machine would probably help. – Edgar Bonet Jun 21 '18 at 19:03
  • 1
    @EdgarBonet, did it! Work as expected. Thanks:) – Sophia Jun 22 '18 at 12:34
0

I'm pretty certain that your code is the problem. You are relying on timing rather than events. Using delays is OK when you are just interfacing to one sensor but when you start mixing them you end up with issues like this.

You are reading the vibration sensor once every second if the distance is 4. So any vibration needs to occur for more than one second, or at exactly the right time. I think you need to look at a better way of coding the solution without using the big delay statements, maybe look at interrupts.

  • I have updated the code. But I am lost at how to let the interrupt know that when the distance is 4cm, the interrupt will run. Thanks – Sophia Jun 21 '18 at 14:25
  • Is ok I solved it. – Sophia Jun 22 '18 at 12:34
  • @Sophia - Please would you write your own answer to the question in case someone else ever has the same problem? – Code Gorilla Jun 22 '18 at 14:04
  • Why add the complexity of interrupts here? Why not just get rid of the delay calls and write non-blocking code? Seems silly to recommend an interrupt every time someone improperly uses delay. – Delta_G Sep 19 '18 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.