Go Down

Topic: Amarino accelerometer (Read 925 times) previous topic - next topic

TeslaIaint

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:
Code: [Select]
#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?

PaulS

Code: [Select]
  if (-10<=data[0]<=10)             
User invented shortcuts have never worked. I'm amazed at the number of people that keep hoping, though.


PeterH


I already know it doesn't work? Did you have a suggestion?


If you want to compare a variable against two values then you need two comparisons.

Code: [Select]

if ((-10<=data[0]) && (data[0]<=10))
I only provide help via the forum - please do not contact me for private consultancy.

TeslaIaint

#3
Dec 24, 2012, 07:34 pm Last Edit: Dec 24, 2012, 07:41 pm by TeslaIaint Reason: 1
Same results with comparison operators modified.

Code: [Select]
#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(9600);      
 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 ((data[0] >= -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 ((data[0] >= -1) || (data[0] <= 1))  
   {
     digitalWrite(Reverseled,LOW);  
     digitalWrite(Rightled,LOW);
     digitalWrite(Leftled,LOW);
     digitalWrite(Forwardled,LOW);
   }
 }

 //This is for Turn Left/Turn Right
 if ((data[1] >= -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 ((data[1] >= -1) || (data[1] <= 1))  
   {
     digitalWrite(Reverseled,LOW);  
     digitalWrite(Rightled,LOW);
     digitalWrite(Leftled,LOW);
     digitalWrite(Forwardled,LOW);
   }    
 }
}



PaulS

I think you really need a way of debugging what the phone is sending you. Without that, your are just whistling in the dark.

An LCD or a FTDI cable to create a second serial port is most likely going to be essential.

leoman

hello, sorry for my poor english (google translator style)
This code works, I modified the example cited above
Code: [Select]
#include <Servo.h>
#include <MeetAndroid.h>

float data[3] = {0};

int intdata[3] = {0};

int i = 1;

Servo myservo;
Servo myservo2;

int pos = 0;
int pos2 = 0;

// MeetAndroid meetAndroid();
// you can define your own error function to catch messages
// where not fuction has been attached for
MeetAndroid meetAndroid(error);

void error(uint8_t flag, uint8_t values){
  Serial.print("ERROR: ");
  Serial.print(flag);
}


  void setup() {
  Serial.begin(9600); //begin serial link @ 9600bps
  // register callback functions, which will be called when an associated event occurs.
  // - the first parameter is the name of your function (see below)
  // - match the second parameter ('A', 'B', 'a', etc...) with the flag on your Android application
  meetAndroid.registerFunction(floatValues, 'A'); 
  myservo.attach(11); //Attach the servo to pin 11
  myservo2.attach(12); //Attach the servo to pin 12

}

void loop()
{
  meetAndroid.receive(); // you need to keep this in your loop() to receive events
}
void floatValues(byte flag, byte numOfValues)
{
  // create an array where all event values should be stored
  // the number of values attached to this event is given by
  // a parameter(numOfValues)
 
  // call the library function to fill the array with values (floats)
  meetAndroid.getFloatValues(data);
 

  for (int i=0; i<3;i++)
  {
    meetAndroid.send(data[i]);//Used for debug purposes
  }

  if (-10<=data[1]<=10)   {
     intdata[1] = int(data[1]); //Cast the X axis float value to int
     intdata[1] = intdata[1]+10;
     pos = intdata[1]*9;
     myservo.write(pos);
  }

  if (-10<=data[2]<=10)   {
     intdata[2] = int(data[2]); //Cast the Y axis float value to int
     intdata[2] = intdata[2]+10;
     pos2 = intdata[2]*9;
     myservo2.write(pos2);
  }
           
}

AWOL

Code: [Select]
if (-10<=data[1]<=10)
Such expressions can be really useful, when you know what you're doing.
Until then, it's best to do the test as two separate expressions, with a suitable boolean operator.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up