I am building a robotic car with stepper motors (for wheels) and infrared sensors(avoiding obstracles) using PIC18F4550 microcontroller.
So, I have created a function to drive the stepper motor steps by steps and it works fine.
void move_forward(int steps) { // Code to drive stepper motor step by step } Then I created a function to read the infrared sensor output.
Let's say
int read_distance_from_sensor(void) { // Code to read the analog input and return distance } It works fine too. But my problem is how do I run the read_distance_from_sensor() function while the stepper motor function is running?
From what I understand, everything gets executed one after another in the PIC18 and it is not possible to execute 2 tasks at the same time simultaneously.
If I do the following:
void main(void) { while(1) { move_forward(20); if (read_distance_from_sensor<10) { // Stop } } } It will move forward first then it stop to get the distance measurement.
I want it to return the distance measurement while it is on the move.
Do I have to call the read_distance_from_sensor() function inside the move_forward() function everytime i move a step for motor?
Is there a clever way to accomplish this?