I want to use both the ultrasonic sensor and IR sensor with Arduino + motors for a car project. Will I have to do any kind of multithreading on Arduino, or is it something else
2 Answers
While it is completely possible to hook two or more sensors to Arduino, once you talk about multithreading, you need to think about RTOS support. You can achieve parallel processing by creating 2 diffenent tasks using xTaskCreate in freeRTOS. Latest development support in Arduino allow to create such tasks.
https://github.com/feilipu/Arduino_FreeRTOS_Library
While the concepts of RTOS are vast, you can quickstart with Arduino freeRTOS library and check whether it meets your requirements. I used to do such project for obstable avoider bot and faced lot of challenges if there is no parallel processing. I am sure you'll be able to acheive much better results with multithreading.
Regarding sensor, make sure ultrasonic sensor is giving reliable data for every readings and don't use IR sensor under sunlight conditions otherwise it'll give erratic results. All the best with your project!
Arduinos don't do multithreading. There are a number of attempts at implementing such things, but they're a waste of time and resources.
Instead you need to get into the habit of two things:
- Write non-blocking code
- Make use of the internal hardware peripherals when possible
The motors, using the hardware PWM, are very much "fire and forget" - set them going and you can forget about them until you want to change their speed or direction.
Ultrasonic sensors can be made both more accurate and more "in the background" by using the Input Capture peripheral of the Arduino.
IR sensors (I assume you mean proximity sensors) are usually either just like a button or a potentiometer - you just read them when you want to know what it's doing.
It's your task to learn how to fit all that together into a single program that reacts to the inputs in the way you want and control the motors in the right way.