Push Rollbot Version 2
Version 2: Decided to power one wheel to add a little directionability and to get it out of trouble if it gets stuck or stalled.
Version 1
Simple single servo, single sensor and Arduino robot that rolls back and forth somewhat unpredictably. Change direction by kicking it. Probably could add directionability with another servo. Second half of video gives good handle on details. Here is program:
// Push rolling robot with single servo, tilt-sensor and Arduino Uno
// by Jim Demello 1/12/2015
#include <Servo.h>
Servo myservo2;
int Forward = 1;
int rollCntr = 0;
int val; // variable to read the value from the analog pin
int gyroPin = 5; //tiltmeter VTI SCA610 inclinometer chip (gyro?)
int gyroVal = 0;
void setup()
{
myservo2.attach(9); // attach servo pin 9
myservo2.writeMicroseconds(1500); // center servo
delay(15);
Serial.begin(9600);
Serial.println("Program start...");
}
void loop()
{
val=90; //servo centered at 90 degrees
gyroVal = analogRead(gyroPin);
gyroVal = map(gyroVal, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
delay(10);
if(rollCntr > 4) { if (Forward) {Forward = 0;} // reverse direction
else Forward = 1;
rollCntr = 0;}
if(Forward && (gyroVal > (val - 5) and gyroVal < (val +25 )))
{ myservo2.write(40); delay(500); myservo2.write(90); // go forward
rollCntr = rollCntr + 1;
}
if (!Forward && (gyroVal > (val - 25) and gyroVal < (val + 5 )))
{ myservo2.write(150); delay(500); myservo2.write(90); // go reverse
rollCntr = rollCntr + 1;
}
Serial.print(" level @ : ");Serial.print(val);Serial.print(" angle: ");Serial.print(gyroVal);
Serial.print(" Forward: ");Serial.println(rollCntr);
delay(40); //
}
chopstick rolls robot forward and back