Hi,
Ive spent hours trying to figure this out and I give up. Any help would be more than greatly appreciated.
I have this sequence for a relay. It opens and closes for a sequence it needs to run. I have a sonar sensor and I need it to restart the sequence when an object is detected less than 70 cm away. As a begginer, ive spent at least 8 hours trying to figure this out but I give up. Your solution is my last hope!
Ive labeled where exactly im having trouble. Thank you so much for your help.
//Begin
#define echoPin 10
#define trigPin 12
long duration;
long distance;
void(*resetFunc)(void) = 0;
void setup() {
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(3, OUTPUT);
Serial.print("Restart");
// Tap Sequence
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
}
void loop()
{
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/58.2;
Serial.print(distance);
delay(50);
[b]//this is the code I need to inturupt and start again if the sonar senses something
[/b]
//start
{
if (distance < 70) resetFunc();
delay(1000);
digitalWrite(3, LOW);
delay(4400);
digitalWrite(3, HIGH);
delay(3500);
delay(1000);
digitalWrite(3, LOW);
delay(2800);
delay(1000);
digitalWrite(3, HIGH);
delay(2000);
delay(1000);
digitalWrite(3, LOW);
while (1) {};
//end
}
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2;
Serial.print(distance);
delay(50);
}
My latest attempt was putting
if (distance < 70) resetFunc();
in between every change of LOW or HIGH of the relay but the value read from the sensor freezes and doesnt update after the relay code has started.