Deal All,
Straight to the point.
I want my led to turn on when the object is at < 100cm.
I want my buzzer to turn on after point #1 above stays for 2000 ms.
After I run the sketch, and put the object in front of the sensor for 2000 ms, my buzzer stays quiet. Help me.
this is the sketch (I'm super new, please be gentle):
#define trigPin 10
#define echoPin 9
#define pinLed 7
#define buzzer 6
#define pulsa 5
float duration, distance;
long safedistime;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pinLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(pulsa, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0343;
Serial.print("Distance = ");
if (distance >= 300 || distance <= 30) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
if (distance <= 100){
digitalWrite(pinLed, HIGH);
}
else {
digitalWrite(pinLed, LOW);
}
safedistime = pulseIn(pulsa, HIGH);
if (safedistime > 2000){
digitalWrite(buzzer, HIGH);
}
else{
digitalWrite(buzzer, LOW);
}
delay(500);
}
J-M-L
November 17, 2018, 1:10pm
2
Straight to the point...
--> Read how to use the forum and code tags.
thx
what's this supposed to do in your opinion?
safedistime = pulseIn(pulsa, HIGH);
safedistime = pulseIn(pulsa, HIGH);
if (safedistime > 2000)
{
digitalWrite(buzzer, HIGH);
}
The default timeout of pulseIn() is one second so your code will not work.
Instead, save the value of millis() when you turn on the LED, then each time through loop() check whether the object is still within range. If 2000 milliseconds elapses with the object in range then turn on the buzzer.
See Using millis() for timing. A beginners guide , Several things at the same time and look at the BlinkWithoutDelay example in the IDE.
J-M-L:
Straight to the point...
--> Read how to use the forum and code tags.
thx
what's this supposed to do in your opinion?
safedistime = pulseIn(pulsa, HIGH);
Dear Sir, I just browsed around, couldnt find the post you referred.
what's this supposed to do in your opinion?
I just couldnt find a unique name, so that what i came up with.
I just couldnt find a unique name, so that what i came up with.
Yes, but what is it actually supposed to do ?
UKHeliBob:
Yes, but what is it actually supposed to do ?
pulseIn (duration of HIGH state in ms, i guess). cmiiw
duration of HIGH state in ms, i guess
Stop guessing and read pulseIn()
pulseIn() returns a value in microseconds (or zero if no pulse was received) but crucially if you do not specify a timeout (you don't) then it will return a zero in one second anyway so without a specific timeout your attempt to time 2 seconds will not work
Dear Sir, I just browsed around, couldnt find the post you referred.
It is called "How to use this forum - please read " and is posted at the top of every forum section.