Parking sensor

A short project about coding Arduino Due with proximity sensor.

 

In association with


Submission


Code

#include <LiquidCrystal.h>

LiquidCrystal lcd(49, 47, 45, 35, 33, 31, 29);


unsigned short mode = 0;

unsigned int echo = 9;

unsigned int trig = 8;

unsigned int red = 5;

unsigned int blue = 4;

unsigned int green = 3;

unsigned int buttonPin = 2;

unsigned int buzzer = 10;

unsigned long endEcho = 0;

unsigned long start = 0;

unsigned long then = 0;

unsigned long echoDelay = 0;

unsigned long distance = 0;

unsigned long buzzerDelay = 0;

unsigned long buzzerDelayNew = 0;

unsigned long buzzerDelayOld = 2000;

char echoState;

char button;


void setup() {

  pinMode(trig,OUTPUT);

  pinMode(echo,INPUT);

  pinMode(red,OUTPUT);

  pinMode(blue,OUTPUT);

  pinMode(green,OUTPUT);

  pinMode(buzzer, OUTPUT);

  pinMode (buttonPin, INPUT_PULLUP);

  Serial.begin(9600);

  lcd.clear();


}


void loop() {


  button = digitalRead(buttonPin);

  if (button == LOW && mode == 1){

    mode = 0;

    delay(1000);

  }

  do{

    button=digitalRead(buttonPin);

  } while(button == HIGH && mode == 0);

  if (mode == 0){

    mode = 1;

    delay(1000);

  }

 

  digitalWrite(green, HIGH);

  digitalWrite(red, HIGH);

  digitalWrite(blue, HIGH);

 

  digitalWrite(trig, HIGH);

  delayMicroseconds(10);  

  digitalWrite(trig, LOW);


  do{

    start = micros();

  } while (digitalRead(echo) == LOW);

 

  do{

    echoState = digitalRead(echo);

  } while (digitalRead(echo) == HIGH);

  endEcho = micros();

 

  echoDelay = endEcho - start;

  echoDelay = echoDelay / 2;

  distance = echoDelay * 0.34;

  buzzerDelayNew = echoDelay;

  Serial.print(distance);

  lcd.clear();

  lcd.print(distance);


  if (distance > 300){

    digitalWrite(green, LOW);

  }

  if (distance >= 100 && distance <= 300){

    digitalWrite(blue, LOW);

  }

  if (distance < 100){

    digitalWrite(red, LOW);

  }


  if (buzzerDelayNew > 2000){

    buzzerDelayOld = 2000;

  }

  else if (buzzerDelayNew < buzzerDelayOld){

    buzzerDelayOld = buzzerDelayNew;

    buzzerDelay = then + buzzerDelayOld;

  }

 

  if (millis() > buzzerDelay){

    then = millis();

    buzzerDelay = then + buzzerDelayNew;

    digitalWrite(buzzer, HIGH);

    delay(100);

    digitalWrite(buzzer, LOW);

  }


}


Previous
Previous

MMA logo

Next
Next

FEA project