My project is 3 Infrared Distance Sensor and 3 vibration motor(small size vibration motor)
And the problem is the motor dont run at the same time but it run the first one and go to second, third..
I am using Arduino UNO R3
This is my code:
if(distance1 < 80)
{
digitalWrite(MotorF,HIGH);
delay(1000);
digitalWrite(MotorF,LOW);
delay(10);
}
if(distance2 < 80)
{
digitalWrite(MotorD, HIGH);
delay(1000);
digitalWrite(MotorD, LOW);
delay(10);
}
if(distance3 < 80)
{
digitalWrite(MotorL, HIGH);
delay(1000);
digitalWrite(MotorL, LOW);
delay(10);
}
if(distance4 < 80 )
{
digitalWrite(MotorR, HIGH);
delay(1000);
digitalWrite(MotorR, LOW);
delay(10);
}
For anything "at the same time" you need to use cooperative multitasking with millis() timing, instead of delay().
http://forum.arduino.cc/index.php?topic=223286.0
pylon
October 12, 2017, 3:54pm
3
Take a look at the example BlinkWithoutDelay, it shows you how to eliminate the use of the delay() function completely. Once you've done that your motors may run together.
aarg:
For anything "at the same time" you need to use cooperative multitasking with millis() timing, instead of delay().
Demonstration code for several things at the same time - Project Guidance - Arduino Forum
Can I have an example code for this vibration motor? I have tried to understand this blinkwithoutdelay. But I dont understand it.
system
October 12, 2017, 4:32pm
5
Maybe spend a little longer than half an hour on working it out and understanding it?