I’m really getting to like this arduino..
Want to control a motor? Read a temperature sensor? Know where you are in the world? Build a robot? Open/Close a door? buld a Christmas display worthy of a CNN segment?
Grab an arduino, spend something like 10 minutes in research and go build it. It is an amazing little tinker-toy.
I’m working on a little autonomous ‘bot that will have as it’s brain, an arduino Duelimanove. It will be mobile, tracked, contain a 7 seperate sensors, 3 motors, a built in video screen, a camera, and wireless internet connectivity. All based on a single, $29.00, processor that can be programmed with very little programming skill.
Think about that for a minute… As a guy that worked with Basic Stamps for years, controlling 7 separate sensors on one tiny little pic is….. well, it’s pretty cool, in a geeky way. Add motors, wireless, cameras, motors, etc., and you are in a realm where you needed (at best) a full-blown laptop a couple of years ago.
Now, couple that with open-source development and the ability to simply go online to find code examples for your needs, and you are opening the world of robotics, controllers, animatronics and anything else you can imagine to anyone with an internet connection and the ability to read. I’m completely geeked-out to see what comes of this little controller in the next several years!
OK – off of the soapbox; lets look at a little sample code: (controlling a motorized platform)
One thing to note: The ArduMoto controller comes in two versions, one that uses Pin 10 and one that uses Pin 3. The earlier version (uses Pin 10) interferes with servos (which also need pin 10) and may need a modification if you intend to use servos with it. (I’ll actually discuss this mod in my next post) In the case of this application, I don’t need any servos, so pin 10 is fine as is.
The following code allows you to drive two seperate motors, controlling speed and direction, which will allow you to drive a bot base in any direction.
/*
v.2.0 Jerry Parker
piggy back the ArduMoto on top of the Arduino.
Connect a power supply to the Power connector. and hook up a dc motor to pins
1 and 2, and pins 3 and 4 on the OUTPUT of the ArduMoto. No other conenctions are necessary
Enter 1,2,3,4,5,6,7,8,9 for Speed setting (1 = LOW, 5=MED, 9=HIGH)
then enter motor Control Character
Q-W-E Forward Left – Forward – Forward Right
A-S-D Rotate Left – ALL STOP – Rotate Right
Z-X-C Reverse Left – Reverse – Reverse Right
*/
// defines for ArduMoto
#define MotorAPin 10
#define MotorBPin 11
#define MotorADirection 12
#define MotorBDirection 13
// variables needed by the sketch
int Command; // raw user input
int MotorSpeed = 0; //set speed to LOW
//use the following setting to balance your motor speed when turning (drifting) left or right
float InsideMotorSpeedAdjust = .5;
void setup()
{
// motor pins must be outputs
pinMode(MotorAPin, OUTPUT);
pinMode(MotorBPin, OUTPUT);
pinMode(MotorADirection, OUTPUT);
pinMode(MotorBDirection, OUTPUT);
Serial.begin(9600); //Start serial console
HelpMenu(); // and display the menu
}
void loop(){
// wait for serial input
Command = 0;
if (Serial.available() > 0) { //Wait for Serial port to come ready
Command = Serial.read(); //read incoming byte
// Read a character from the serial port, and act accordingly.
// Characters read in this way come in as ints, and need to be converted to their ascii equivalent.
// there is a great Arduino character chart here:
// http://www.arduino.cc/en/Reference/ASCIIchart
if (Command == 63) { // entered “?”, show menu
HelpMenu();
}
if (Command == 49) { // entered 1; SPEED 10%
MotorSpeed=25;
}
if (Command == 50) { // entered 2; Speed 20%
MotorSpeed=51;
}
if (Command == 51) { // entered 3; Speed 30%
MotorSpeed=76;
}
if (Command == 52) { // entered 4; Speed 40%
MotorSpeed=102;
}
if (Command == 53) { // entered 5; Speed 50%
MotorSpeed=127;
}
if (Command == 54) { // entered 6; Speed 60%
MotorSpeed=153;
}
if (Command == 55) { // entered 7; Speed 70%
MotorSpeed=178;
}
if (Command == 56) { // entered 8; Speed 80%
MotorSpeed=204;
}
if (Command == 57) { // entered 9; Speed 90%
MotorSpeed=229;
}
if (Command == 48) { // entered 0; Speed 100%
MotorSpeed=255;
}
// Following are the MOVE commands
if (Command == 83) { // entered S; Stop All
ProcessCommand(0);
}
if (Command == 81) { // entered Q; Left Forward
ProcessCommand(1);
}
if (Command == 87) { // entered W; Forward
ProcessCommand(2);
}
if (Command == 69) { // entered E; Forward RIGHT
ProcessCommand(3);
}
if (Command == 65) { // entered A; Rotate Left
ProcessCommand(4);
}
if (Command == 68) { // entered D; Rotate Right
ProcessCommand(5);
}
if (Command == 90) { // entered Z; Reverse Left
ProcessCommand(6);
}
if (Command == 88) { // entered X; Reverse
ProcessCommand(7);
}
if (Command == 67) { // entered C; Reverse Right
ProcessCommand(8);
}
}
}
// process a command string
void ProcessCommand(int Command) {
Serial.print(“Speed: “);
Serial.println(MotorSpeed);
// check commands
switch(Command) {
case 0:
//all stop
Serial.println(“All Stop”);
analogWrite(MotorAPin, 0);
analogWrite(MotorBPin, 0);
break;
case 1:
Serial.println(“FORWARD LEFT”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, LOW);
analogWrite(MotorBPin, (MotorSpeed*InsideMotorSpeedAdjust));
digitalWrite(MotorBDirection, HIGH);
break;
case 2:
Serial.println(“FORWARD”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, LOW);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, HIGH);
break;
case 3:
Serial.println(“FORWARD RIGHT”);
analogWrite(MotorAPin, (MotorSpeed*InsideMotorSpeedAdjust));
digitalWrite(MotorADirection, LOW);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, HIGH);
break;
case 4:
Serial.println(“Rotate LEFT”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, LOW);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, LOW);
break;
case 5:
Serial.println(“Rotate RIGHT”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, HIGH);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, HIGH);
break;
case 6:
Serial.println(“REVERSE LEFT”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, HIGH);
analogWrite(MotorBPin, (MotorSpeed*InsideMotorSpeedAdjust));
digitalWrite(MotorBDirection, LOW);
break;
case 7:
Serial.println(“REVERSE”);
analogWrite(MotorAPin, (MotorSpeed));
digitalWrite(MotorADirection, HIGH);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, LOW);
break;
case 8:
Serial.println(“REVERSE RIGHT”);
analogWrite(MotorAPin, (MotorSpeed*InsideMotorSpeedAdjust));
digitalWrite(MotorADirection, HIGH);
analogWrite(MotorBPin, (MotorSpeed));
digitalWrite(MotorBDirection, LOW);
break;
}
}
void HelpMenu() {
Serial.println(“Arduino MINI_BOT Robot Controller V2.0 by Jerry Parker”);
Serial.println(“Menu. Enter letter to execute”);
Serial.println(“? – Help Menu”);
Serial.println(“—-Motor Commands—-”);
Serial.println(“1-0 – Motor Speed in percent (0=100)”);
Serial.println(“Q-W-E – Forward LEft – Forward – Forward Right”);
Serial.println(“A-S-D – Rotate Left – STOP – Rotate Right”);
Serial.println(“Z-X-C – Rev Left – Reverse – Rev Right”);
}