Two Wheel Self Balance Robot.
Oops. Brought the wrong motor. I've spend $ 20 for Osepp Motor. But I don't want buy another motor shield and leave Osepp abandon. What's the problem?
Well, I've spend a week trying to make two servos work. Failed hundred of test on servo.h.
One servos works fine on CarieIMU. Tilting around. Messing around. Opening Serial monitor screen, few seconds, it crashes when you use two servos & intel arduino 101. I give up. Does anyone have better self balance robot code?
#include "CurieIMU.h"
#include <Servo.h>
Servo myservo, myservo2; // create servo object to control a servo
void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open
// initialize device
Serial.println("Initializing IMU device...");
CurieIMU.begin();
// Set the accelerometer range to 2G
CurieIMU.setAccelerometerRange(2);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10); // attaches the servo on pin 10 to the servo object
}
void loop() {
int axRaw, ayRaw, azRaw; // raw accelerometer values
float ax, ay, az;
// read raw accelerometer measurements from device
CurieIMU.readAccelerometer(axRaw, ayRaw, azRaw);
// convert the raw accelerometer data to G's
ax = convertRawAcceleration(axRaw);
ay = convertRawAcceleration(ayRaw);
az = convertRawAcceleration(azRaw);
// display tab-separated accelerometer x/y/z values
Serial.print("a:\t");
Serial.print(ax);
Serial.print("\t");
Serial.print(ay);
Serial.print("\t");
Serial.print(az);
Serial.println();
//////////////////////////////////////
//
// Balance Code failed.
//
//
/////////////////////////////////////
// wait 5 seconds
//delay(5000);
}
float convertRawAcceleration(int aRaw) {
// since we are using 2G range
// -2g maps to a raw value of -32768
// +2g maps to a raw value of 32767
float a = (aRaw * 2.0) / 32768.0;
return a;
}