Go Down

Topic: simple parking sensor (Read 652 times) previous topic - next topic

billb57

Hi Guys & gels,
I'm doing a simple project, its a parking sensor that gives a warning when to close to a wall. this is being situated out side and i already have a waterproof sensor which is the MY007Y from ebay.
       I already have constructed project but i'm stuck on a bit of code that will able to turn the led's off and come back on when something gets in the way of the sensor.
       I've searched google and nearly everyone i've looked at includes using a button which i don't want to use. I'm using the attached code on a attiny85 and it works quite well, I just want to be able to turn the led's off after 30seconds or so. I've tried using the wdt and iterrupts for the attiny but the seem to just hang and do nothing.
     Can anyone help or put me in the right direction to solve my little issue.
                                                   regards bill

AWOL

Code: [Select]
#include <NewPing.h>
 
 const int triggerPin = 3; //output
 const int echoPin = 4; //input

 int redPin=0;
 int yellowPin=1;
 int greenPin=2;
 long duration;
 long distance;
NewPing sonar(0, 1, 500);       //constructor for NewPing sonar(triggerPin, echoPin, max_CM)
 void setup(){
   pinMode (0, OUTPUT); //RED
   pinMode (1, OUTPUT); //YELLOW
   pinMode (2, OUTPUT); //GREEN
   
   //Serial.begin(9600);
 }

 void loop()
 {
   int stopDistance=24;                                                                             
   int warnDistance=60;
                                  //to check how long car has been parked in order to enter power save
   pinMode(echoPin, INPUT);
   pinMode(triggerPin, OUTPUT);
   digitalWrite(triggerPin, LOW);
   
   delayMicroseconds(100);
   digitalWrite(triggerPin, HIGH);
   delayMicroseconds(100);
   digitalWrite(triggerPin, LOW);
   
   duration = pulseIn(echoPin, HIGH);
   distance = duration / 72 / 2;
   if (distance >= warnDistance){
     digitalWrite (redPin, LOW);
     digitalWrite (yellowPin, LOW);
     digitalWrite (greenPin, HIGH);

    else if((distance>stopDistance) && (distance<warnDistance)){
     digitalWrite (redPin, LOW);
     digitalWrite (yellowPin, HIGH);
     digitalWrite (greenPin, LOW);
    } 
    else {
     digitalWrite (redPin, HIGH);
     digitalWrite (yellowPin, LOW);
     digitalWrite (greenPin, LOW);
   }   
 
   } 
     
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Hutkikz

#2
Sep 10, 2016, 05:56 pm Last Edit: Sep 10, 2016, 05:56 pm by Hutkikz
You just need to set a timing sequence when the LED's are lit that will turn them off after your chosen interval.

Code: [Select]


unsigned long startTime = 0;
const long interval = 1000;  // 1 second = 1000 millis
bool timeFlag = false;

void setup()
{
}

void loop()
{
  if (something you want to Time)
  {
    timeFlag = true;
    startTime = millis(); // Begin timing sequence
    // do stuff here when timing sequence starts
  }

  unsigned long currentTime = millis();
  if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
  {
    timeFlag = false;
    // do stuff here when time is up
  }
}



Further reading:Demonstration code for several things at the same time
"Progressivism turns into socialism, socialism turn into communism, communism turns into dictatorship. It is the nature of government to gain ground & for liberty to yield." - Thomas Jefferson

billb57

Thanks Hutkikz for your reply and help....I'm a newbie and i wouldn't no where to start putting that in to my code... I would like my program to keep running on the sensor but i just want to turn off the 3 led's when nothings detected or no movement. then when we park on the drive it pics up the car and lights the first led.... sorry for being thick but I'm trying to learn.
                                   Regards Bill

Hutkikz

That's why I included the link at the bottom of my post.

Here are some more:

Blink Without Delay

Multitasking the Arduino
"Progressivism turns into socialism, socialism turn into communism, communism turns into dictatorship. It is the nature of government to gain ground & for liberty to yield." - Thomas Jefferson

Hutkikz

#5
Sep 10, 2016, 07:51 pm Last Edit: Sep 10, 2016, 07:51 pm by Hutkikz
Here I changed the comment's etc. to more directly apply to your situation.
Code: [Select]


unsigned long startTime = 0;
const long interval = 1000;  // 1 second = 1000 millis
bool LedIsOnFlag = false;

void setup()
{
}

void loop()
{
  if (condition to turn on LED)
  {
    LedIsOnFlag = true;
    startTime = millis(); // Begin timing sequence by recording the time
    // turn your LED on
  }

  unsigned long currentTime = millis(); // record current time
  if (LedIsOnFlag == true && currentTime - startTime >= interval) // End timing sequence by subtracting the start time from the current time
  {
    LedIsOnFlag = false;
    // turn your LED off
  }
}


"Progressivism turns into socialism, socialism turn into communism, communism turns into dictatorship. It is the nature of government to gain ground & for liberty to yield." - Thomas Jefferson

billb57

sorry Hutkikz for not getting back in touch, I've not tried it yet because my father inlaw got taken in to hospital, so I've not had chance to look at it properly.

                                        Thanks for your help.
                                   
                                           Kind Regards Bill.

Hutkikz

No worries here, Hope he has a fast recovery.
"Progressivism turns into socialism, socialism turn into communism, communism turns into dictatorship. It is the nature of government to gain ground & for liberty to yield." - Thomas Jefferson

Go Up