i am making a rover. I have a ultrasonic sensor attached to a servo, and i am using lookAhead function like this:
void lookAhead(){
servo1.write(90);
delay(1000);//doesn't work
}
i am calling this function in another function like this:
lookAhead();
if (distance >= 15){
motor1.run(FORWARD);
motor2.run(FORWARD);
}
i want to have a delay so that the servo is rotated properly to measure the distance by ultrasonic sensor. the distance is continuously calculated in loop(). and if the distance is greater than 15, the rover will go forward.
but the delay() doesn't work, as soon as i give the command to go forward, the servo rotates, and also simultaneously, the motors turn. whereas i would like for the motors to wait for the delay(), and then turn so that the servo is properly at its position.
the delay works in loop() though.
also, i am using the wire library to communicate with esp8266, which is acting as a master, and it is running a web server to take commands to move the rover.
any idea what am i doing wrong?
I am using cpp file as i am using platformio in vscode.
Here is the important part of the program:
void lookAhead(){
servo1.write(90);
delay(1000); //not working
}
void setup()
{
Serial.begin(115200);
Wire.begin(1);
Wire.onReceive(receiveEvent); // this is the handler function where i am doing everything
}
void loop()
{
//calculate distance from ultrasonic sensor and store in a global variable
delay(300) //it works
}
void receiveEvent(){
// parse incoming string from master(esp8266) as json, and check for the direction (forward, left, right backward)
if (strcmp(direction, "forward") == 0)
{
lookAhead(); //calling lookAhead function
// here the delay in lookAhead should have worked, but immediately the following code executes
if (distance >= 15){
motor1.run(FORWARD);
motor2.run(FORWARD);
}
}
i have read somewhere that the delay() will not work in an interrupt handler. is the "onReceive" function which handles the receive event for i2c interface a type of interrupt handler?
TheMemberFormerlyKnownAsAWOL:
delay in interrupt context? :o
hmm, i suspected this was the problem, i guess delay() and millis() won't work in interrupt handler. so is it that the "master-slave" arrangement with nodemcu and arduino uno won't work here?. i need the nodemcu for wifi. i am using arduino because the motor driver (adafruit motor shield v 1) has some issues with nodemcu. i can try to make it work with nodemcu or maybe use a wifi module with arduino, instead of using nodemcu as master?
i actually didn't know of this before, only after i posted this question, i did some research and found it out. also, i didn't know that the onReceive method is an interrupt handler.
but even if i use just the nodemcu, which will run a server and listen for the the get requests, isn't that request handler also an interrupt handler?
is there a way to create delay in interrupt handlers, using timers etc?
TheMemberFormerlyKnownAsAWOL:
An interrupt is meant for a processor to record and possibly acknowledge a low-latency signal.
There is no place for delay in such a scheme
vikrant47:
so how to make sure that a servo has rotated to its position, before executing the next line of code? or is that simply not possible?
Implement your delay outside of the interrupt routine. It's certainly possible. If you need handshake with processes in the ISR, use shared flags and variables.
Nobody can really help you do that, until you post your entire sketch.
aarg:
Implement your delay outside of the interrupt routine. It's certainly possible. If you need handshake with processes in the ISR, use shared flags and variables.
Nobody can really help you do that, until you post your entire sketch.
uploaded the file with question.
" use shared flags and variables" can you give me an example, how do i go about doing that?. i can create a global variable, but as long as i call another function from the handler, it is executing inside the handler.