Sensor for car lift

Hello

i have a project in my workshop. I have a lift for cars but my problem is that my my ceiling is too short so a car can hit the ceiling.

So i have a project to instal hc-sr04.

so i have the code that disables the lift (using optocoupler). My problem now is that i want a buzzer when the sensor and arduino stop the car. I want that in final position red LED would light up and stay on. At the same time the buzzer would make a noise for about 5 seconds and then turn off. But the red LED would stay on for as long the car is in the final position.

My program:
#define trigPin 13

#define echoPin 12

#define led 11

#define rele 10

void setup()

{ Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(rele, OUTPUT);

}

void loop()

{ long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 10)

{ digitalWrite(led,HIGH);

digitalWrite(rele,LOW);

}

else {

digitalWrite(led,LOW);

digitalWrite(rele,HIGH);

}

Serial.print(distance);

Serial.println(" cm");

delay(500);

}

In this program i dont have the buzzer yet because i did everything wrong that i tried.
i tried:

const unsigned long BUZZERTime=2000;
unsigned long TimeCaptured = millis();
#define buzzer 3
void setup() {
pinMode(3,OUTPUT);
tone(buzzer, 2500);

}

void loop() {

if ( (millis() - TimeCaptured) >= BUZZERTime )
{

noTone(buzzer);

}
}

no luck.....

my buzzer is working and it is piezo.

thanks for the help...and sorry for my poor english :slight_smile:

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

This sketch

const unsigned long BUZZERTime = 2000;
unsigned long TimeCaptured;

# define buzzer 3

void setup() {
  pinMode(3, OUTPUT);

  tone(buzzer, 2500);
  TimeCaptured = millis();  // buzeer went on.
}

void loop() {
  if ((millis() - TimeCaptured) >= BUZZERTime)
  {
    noTone(buzzer);
  }
}

should… sound the buzzer for two seconds after reset and the startup process is done, a few seconds itself.

I'll test it next time I'm in my laboratory.

In your code, do the same thing. When you want the buzzer, turn it on and set TimeVaptured to millis().

And place the if statement you wrote that will turn it off after BUZZERTime milliseconds somewhere it will get executed periodically, like at the top or bottom of your loop() function. Just so it gets attention every time and can decide about the tone.

When you say the buzzer works, what code did you test it with and is it remaining wired the same way?

a7

OK the Umbrella Academy assures me the small sketch works. I did not test it, neither did any of them, but they are confident.

Now in the main sketch to which you wish to add the buzzer thing, what you are missing is the detection of the change of state.

When the distance goes below the limit, rather than when the distance is below the limit is what you should use to trigger the temporary buzzer sound.

"state change detection" is a mouthful, but a simple concept.

In the IDE see

File / Examples / 02.Digital / StateChangeDetection

See if you can understand how it is working with a pushbutton digital signal. If you get it, then go further to see if you can replace the logical HIGH / LOW of a pushbutton and substitute for it instead a "too close" / "not too close" logical value.

In the example, the switch is read only on one line:

  // read the pushbutton input pin:
  buttonState = !digitalRead(buttonPin);

so all you have to do is change that to something like

// check the lift altitude
 bool okSoFar = distance > 10;

so okSoFar will be true if there is still room, and false if not.

It is the change in the state of being okSoFar that you want to detect. And act on when it goes into not OK too close to the ceiling. (turn on the tone and note TimeCaptured when you did.)

HTH

a7

I'm hoping that this is a school exercise and not a real world problem which, in this case, would appear to require a very robust solution.

The Mk II Eyeball gets tired after a while.

Thanks for your help.

alto777 can you help m implement this code to my code please. I am new to arduino world and my knowledge stops here in my code. Just for this code i spent 2 weeks and internet help to get this far.

Thanks.

This is not a project....this is real world. But this is at my home and i work only on my persobal cars. My program is set that in case arduino dies the lift automaticly stops.

I'd be inclined to forget about Arduino and ultrasonics and just have a microswitch activated by a lever with a padded weight hanging from it at the top level. Wire the microswitch to stop the lift, sound the buzzer, and switch on the light.

1 Like

I see the problem here that the top of the car is not always flat and not the same height in different areas. If the car has, for example, an antenna or a high spoiler, then neither a ultrasonic nor a microswitch will help

in this case i set the 10 cm limit (for cars with shark antenna). For standard antenas i always remove them before lifting (old habit).

If someone can help me figure out this code for buzzer to only buzz for 5 seconds.....in worst case i will put another arduino (leonardo) to control only the buzzer. It is cheap so it could be the solution.

i hope this will work
(it will work only if you have active piezo buzzer)
also i am not a professional


#define trigPin 13
#define echoPin 12
#define led 11
#define rele 10
#define buzzer 9

int buzzerState = 0;
unsigned long TimeCaptured;

void setup() {

  Serial.begin (9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(led, OUTPUT);

  pinMode(buzzer, OUTPUT);

  pinMode(rele, OUTPUT);

}

void loop() {

  long duration, distance;

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration / 2) / 29.1;

  if (distance < 10)

  { digitalWrite(led, HIGH);
    if (buzzerState = 0)
    {
      TimeCaptured = millis();
      digitalWrite(buzzer, HIGH);
      if ((millis() - TimeCaptured) >= 5000)
      { digitalWrite(buzzer, LOW);
        buzzerState = 1;
      }
    }
    digitalWrite(rele, LOW);

  }

  else {

    digitalWrite(led, LOW);
    buzzerState = 0;
    digitalWrite(rele, HIGH);

  }

  Serial.print(distance);

  Serial.println(" cm");

  delay(500);

}

    if (buzzerState = 0)

You need to correct this

What is wrong with that?

i uploaded this and nothing happens to the buzzer.

Is the problem in the buzzer. Because when i played with this buzzer i need to write frequency for the buzzer. In my case it was 25000.

To sound the buzzer my command was:

tone(buzzer, 2500);

Are you understand the difference between

if (buzzerState = 0)

and

if (buzzerState == 0)
1 Like

That don't look right. If TimeCaptured gets millis(), the if expression will always be false.

Did you test your program?

a7


#define trigPin 13
#define echoPin 12
#define led 11
#define rele 10
#define buzzer 9

int buzzerState = 0;
unsigned long currentMillis;
unsigned long previousMillis = 0;
const long interval = 5000;

void setup() {

 Serial.begin (9600);

 pinMode(trigPin, OUTPUT);

 pinMode(echoPin, INPUT);

 pinMode(led, OUTPUT);

 pinMode(buzzer, OUTPUT);

 pinMode(rele, OUTPUT);

}

void loop() {

 long duration, distance;

 digitalWrite(trigPin, LOW);

 delayMicroseconds(2);

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10);

 digitalWrite(trigPin, LOW);

 duration = pulseIn(echoPin, HIGH);

 distance = (duration / 2) / 29.1;

 if (distance < 10)

 { digitalWrite(led, HIGH);
   if (buzzerState == 0)
   {
     currentMillis = millis();
     tone(buzzer, 2500);
       if (currentMillis - previousMillis >= interval)
        {
         previousMillis = currentMillis;
         noTone(buzzer);
         buzzerState = 1;
        }
   }
   digitalWrite(rele, LOW);

 }

 else {

   digitalWrite(led, LOW);
   noTone(buzzer);
   buzzerState = 0;
   digitalWrite(rele, HIGH);

 }

 Serial.print(distance);

 Serial.println(" cm");

 delay(500);

 }

this will definitely work and thanks to all to point out my mistakes :smiling_face:

nope, still a bunch of bugs

really what is it?