Hi
I am new to arduino programming. My idea is a garage parking where the sensors used are ultrasonic sensor, obstacle avoidance sensor, ky-002 shock sensor.
When the ultrasonic sensor detects that it is 4cm, the garage door opens. The obstacle avoidance sensor will be activated if there are objects. Then when it reaches the shock sensor part, it doesn't work! I tried put the shock sensor code into another empty sketch, it works.
I have no idea where I have gone wrong. Would appreciate any help given. Thanks
Code
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance, temp = -1;
//Shock Sensor
int shockPin = 11; // Use Pin 10 as our Input
int shockVal = HIGH; // This is where we record our shock measurement
boolean bAlarm = false;
unsigned long lastShockTime; // Record the time that we measured a shock
int shockAlarmTime = 250; // Number of milli seconds to keep the shock alarm
high
void setup() {
lcd.init();
Serial.begin(9600);
// Ultrasonic Sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Distance left:");
// Obstacle Avoidance Sensor
pinMode(8, INPUT);// set pin as input
// Shock Sensor
pinMode (shockPin, INPUT) ; // input from the KY-002
}
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;
test.in( distance ); // software filter
distance = test.out();
// Prints the distance on the Serial Monitor
Serial.print("Distance left: ");
Serial.println(distance);
if ( temp != distance ) {
temp = distance;
lcd.setCursor(0, 1);
lcd.print(" "); //if havent detect
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" CM");
if (distance == 4) {
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..
int detect = digitalRead(8);// read obstacle status and store it into "detect"
lcd.clear();
lcd.setCursor(0, 0);
delay(300);
lcd.print("Detection:");
if (detect == LOW) {
lcd.setCursor(0, 1);
lcd.print("Obstacle Present");
} else {
lcd.setCursor(0, 1);
lcd.print("No Obstacle");
}
int shockVal = digitalRead(shockPin) ; // read the value from our sensor
if (shockVal == LOW) // If we're in an alarm state
{
lastShockTime = millis(); // record the time of the shock
// The following is so you don't scroll on the output screen
if (!bAlarm) {
Serial.println("Shock Alarm");
bAlarm = true;
}
}
else
{
if ( (millis() - lastShockTime) > shockAlarmTime && bAlarm) {
Serial.println("no alarm");
bAlarm = false;
}
}
delay(200);
exit(0);
}
}
}