I have this simple arduino code that drives two Dc motor using L298N which is a motor driver together with two servo motors. The code is working fine for the motor driver but not for the servo motor:
#include <Servo.h>
Servo myservo;
Servo myservo2;
int num=1;
int IN1=8;
int IN2=9;
int ENA=3;
int IN3=10;
int IN4=11;
int ENA2=4;
void setup()
{
myservo.attach(40);
myservo2.attach(42);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop()
{
intialPos();
while(motor_run())
{
turnOne();
delay(3000);
intialPos();
delay(10000);
turnSecond();
delay(3000);
}
}
int motor_run()
{
analogWrite(ENA, 1500);// motor speed
digitalWrite(IN1,LOW);// rotate forward
digitalWrite(IN2,HIGH);
analogWrite(ENA2, 1500);// motor speed
digitalWrite(IN3,HIGH);// rotate forward
digitalWrite(IN4,LOW);
delay(3000);
return (1);
}
void intialPos()
{
myservo.write(70);
myservo2.write(135);
delay(2000);
}
void turnOne()
{
myservo2.write(170);
myservo.write(135);
delay(2000);
}
void turnSecond()
{
myservo2.write(70);
myservo.write(30);
delay(2000);
}
My problem is to make the dc motor code continues to execute while the servo motor do its thing which is turning. But all it does was to make the dc motor work and the servo motors are unmoving. I'm using the servo motor as a rudder in a boat so I need to make the dc motor continuously working while the servo motor turns to a direction. I've heard multi threading but it is not supported on arduino. I found another way to make them in sync with each other which is using cycle or timing, but the sample code was confusing so can someone give me a code snippet and some detailed explanation for it. And please don't give me the blinking lights sample of cycle timing.