Go Down

Topic: Code for ArdBot clone (Read 2347 times) previous topic - next topic

rpensotti

Hi,
this is Roberto "Squarenut" and I'm new to this forum.
I built a "Meccano" based carriage propelled by two SM-S4303R rotary servos and piloted by an Arduino UNO running the code copied from the ArdBot manual.
(see below)
It works well and does the prescribed pirouettes.
I'd like to modify this code so that the carriage travels in one direction indefinitely (< 180°) until a micro switch in front of the carriage is tripped by an obstacle and then reverses direction.
If the code is not too complex, feel free to suggest more sophisticated movements and reaction to the four micro switches that are currently at the four corners of the carriage.
Thanks a lot!
Code: [Select]
/*
ArdBot ServoTest
Tests servos of robot by moving them in different directions
Requires Arduino IDE version 0017 or later
(0019 or later preferred)
*/
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
void setup()
{
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() // Loop through motion tests
{
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
turnRight();
delay(2000);
turnLeft();
delay(2000);
stopRobot();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop
void forward()
{
servoLeft.write(0);
servoRight.write(180);
}
void reverse()
{
servoLeft.write(180);
servoRight.write(0);
}
void turnRight()
{
servoLeft.write(180);
servoRight.write(180);
}
void turnLeft()
{
servoLeft.write(0);
servoRight.write(0);
}
void stopRobot()
{
servoLeft.write(90);
servoRight.write(90);
}

Go Up