I've written a code for a robot that ''will chase rats''.
The vehicle is moving using 2 DC motors and steers using a servo.
Steering is achieved through specific pulse widths for each direction (1000 left,1500 straight,2000 right, etc.) For the steering to be maintained a 20ms period is needed.
The device is using a contact switch. When the switch is pressed the vehicle turns left or right and reverses for 500ms for example.
My question is the following:
Is it possible to move backward while steering (20ms period pulses) using a delay command?
I had an argument about this and I was feeling sure that a 500ms delay cant be integrated into a program that has a repetitive action every 20ms.
*In the uploaded code I am using a millis counter not a delay
void loop() {
unsigned int numServo; // pulse control variables
numServo=1500; // initialised to 1500 (straight ahead)
timenow=millis();
// Set motors to maximum speed
analogWrite(LeftSpeed, 255);
analogWrite(RightSpeed, 255);
//Robot moves forward
digitalWrite(R1, HIGH);
digitalWrite(R2, LOW);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
//Reading the value of the infra-red sensors
digitalRead(DRight);
digitalRead(DLeft);
//Checking if the switch is pressed
digitalRead(Switch);
//When the rodent is detected straight ahead
if (DRight == HIGH && DLeft == HIGH)
{
digitalWrite (Buzzer, HIGH);
numServo=1500;
}
//When the rodent is detected right
if (DRight == HIGH && DLeft == LOW)
{
digitalWrite (Buzzer, HIGH);
//Turn the robot 35degrees to the right
numServo=1888;
}
//When the rodent is detected left
if (DRight == LOW && DLeft == HIGH)
{
digitalWrite (Buzzer, HIGH);
//Turn the robot 35degrees to the left
numServo=1112;
}
//When no rodent is detected
if (DRight == LOW && DLeft == LOW)
{
//Stop this annoying horn!
digitalWrite (Buzzer, LOW);
}
//If the switch hook is pressed again the wall, We hitted a wall!
if (Switch == HIGH)
{
timecounter=millis();
//Setting a counter to allow us to count 0.5s while simiultanesly moving backwards and sending a pwm to the servo to turn right.
//Using a delay function wouldn't allow us to execute both tasks at the same time.
//As soon as the reverse turn is completed our vehicle will go straight forward at full speed (top code snippet*)
while( (timenow-timecounter) <500)
{
// Let's move back right!
//Motors are moving the wheels backwards
digitalWrite(R1, LOW);
digitalWrite(R2, HIGH);
digitalWrite(L1, LOW);
digitalWrite(L2, HIGH);
// Set motors to maximum speed
analogWrite(LeftSpeed, 255);
analogWrite(RightSpeed, 255);
// Steering wheel turning the wheels to the right
numServo=1888;
if(( timenow-timelast ) >= 20) // need pulses to be every 20 +/- 5 ms
{
timelast=millis();// update last pulse time
// then do servo pulse:
digitalWrite(ServoPin,HIGH);
delayMicroseconds(numServo);
digitalWrite(ServoPin,LOW);
}
}
}
if(( timenow-timelast ) >= 20) // need pulses to be every 20 +/- 5 ms
{
timelast=millis();// update last pulse time
// then do servo pulse:
digitalWrite(ServoPin,HIGH);
delayMicroseconds(numServo);
digitalWrite(ServoPin,LOW);
}
}