Pages: [1]   Go Down
Author Topic: Amarino accelerometer  (Read 770 times)
0 Members and 1 Guest are viewing this topic.
Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if ((-10<=data[0]) && (data[0]<=10))
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same results with comparison operators modified.

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(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);
    }    
  }
}

« Last Edit: December 24, 2012, 01:41:05 pm by TeslaIaint » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello, sorry for my poor english (google translator style)
This code works, I modified the example cited above
Code:
#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);
  }
           
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

"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.

Pages: [1]   Go Up
Jump to: