I'm trying to use Amarino to send data from my android phone's accelerometer to arduino in order to turn on or off four led lights that simulate the direction of a robot consisting of two motors. The motors can go forward or reverse. The code is copied and modified from this:
http://www.instructables.com/id/Hercules-The-Motion-Controlled-Android-Robot/step8/Step-8-Programming-the-Arduino/I would expect from my modified code that when I tilted the phone forward on the x axis, the forward light would turn on. When I tilt the phone backward on the axis, the backward led turns on. When the phone is tilted right on the y axis, the right led is turned on, and when the phone is tilted left, the left led is turned on. If the phone is tilted purely on the axis, only one led should be on at a time, but if the phone is tilted simultaneously on the x and y axis, two leds should be on at one time and by using pwm, should be lit with a brightness corresponding to the angle of the tilt. ex. forward-right, back-left, etc.
However, this is not the case. When the phone is tilted forward, no lights. When the phone is tilted backward, no lights.
When the phone is tilted right,the right led comes on, and when it is tilted left the left led comes on. it seems to work the right and left lights seem to work best when it is tilted diagonally in any of the four diagonal directions. But the most I can ever see on the forward and reverse is a barely detectable flicker when the phone is tilted in one of the diagonal directions.
Originally I tried to modify the origional code to drive two motors using an h bridge but got the same results as the leds. I wired up the leds as a simple simulation to try to find a problem. I've tried to change the baud rates, but my program works the same at 9600, 57600, or 115200.
Here is my code:
#include <MeetAndroid.h>
#define Forwardled 3
#define Reverseled 11
#define Rightled 5
#define Leftled 6
float data[3] = {
0};
int intdata[3] = {
0};
int i = 1;
MeetAndroid meetAndroid(error);
void error(uint8_t flag, uint8_t values)
{
Serial.print("ERROR: ");
Serial.print(flag);
}
void setup() {
Serial.begin(57600);
pinMode(Forwardled, OUTPUT);
pinMode(Reverseled, OUTPUT);
pinMode(Leftled, OUTPUT);
pinMode (Rightled, OUTPUT);
delay(1000);
meetAndroid.registerFunction(floatValues, 'A');
}
void loop()
{
meetAndroid.receive();
}
void floatValues(byte flag, byte numOfValues)
{
meetAndroid.getFloatValues(data);
for (int i=0; i<3;i++)
{
meetAndroid.send(data[i]);
}
//This is for Forward/Reverse
if (-10<=data[0]<=10)
{
intdata[0] = int(data[0]);
intdata[0] = intdata[0] * 24;
if (data[0] <= -2)
{
intdata[0] = abs(intdata[0]);
digitalWrite(Reverseled,LOW);
digitalWrite(Rightled,LOW);
digitalWrite(Leftled,LOW);
analogWrite(Forwardled, intdata[0]);
}
else if (data[0] >= 2)
{
digitalWrite(Forwardled,LOW);
digitalWrite(Rightled,LOW);
digitalWrite(Leftled,LOW);
analogWrite(Reverseled, intdata[0]);
}
else if (-1<=data[0]<=1)
{
digitalWrite(Reverseled,LOW);
digitalWrite(Rightled,LOW);
digitalWrite(Leftled,LOW);
digitalWrite(Forwardled,LOW);
}
}
//This is for Turn Left/Turn Right
if (-10<=data[1]<=10)
{
if (data[1] <= -2)
{
digitalWrite(Reverseled,LOW);
digitalWrite(Forwardled,LOW);
digitalWrite(Leftled,LOW);
analogWrite(Rightled, intdata[0]);
}
else if (data[1] >= 2)
{
digitalWrite(Reverseled,LOW);
digitalWrite(Forwardled,LOW);
digitalWrite(Rightled,LOW);
analogWrite(Leftled, intdata[0]);
}
else if (-1<=data[1]<=1)
{
digitalWrite(Reverseled,LOW);
digitalWrite(Rightled,LOW);
digitalWrite(Leftled,LOW);
digitalWrite(Forwardled,LOW);
}
}
}
Any suggestions?