Amarino Accelerometer

Hey everybody,

I´m relative new to this Arduino Programming.
So and now, here´s my first Problem and I´m looking forward anyone here can help me :slight_smile:

My tiny project:
I want to drive a servo using the Amarino-App. Particularly the Accelerometer.

  • I have a connection from my Phone to the Bluetooth-Module.
  • I´ve tested it with other Code and so on …

Hopeing anybody can find the mistake.
Thank you very much stranger :slight_smile:

// ********************************************* 
//       Connect Servo as follows:
//       ::: BLACK --> GND :::
//       ::: RED --> 5V :::
//       ::: Signal --> PIN 9 ::: 
// *********************************************


#include <MeetAndroid.h>
#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos=0;        // variable to store the servo position 
char value;
MeetAndroid meetAndroid;
float data[3] = {0};
int intdata[3] = {0};
int i = 0;



void setup() 
{ 
  Serial.begin(115200);  // baud rate 115200
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  meetAndroid.registerFunction(floatValues, 'A');  //use event A in amarino
} 
 
 
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
  meetAndroid.getFloatValues(data);
  
            // access the values
  for (int i=0; i<3;i++)
  {
    meetAndroid.send(data[i]);
  }
  
// ********************************************************
// ********************************************************
  
   
   
  
  //        This is for Turn Left/Turn Right
    if (-10<=data[0]<=10) 
    {
      if (data[0] <= -2)
            {          
            myservo.write(0);
            }

      else if (data[0] >= 2)
            {
            myservo.write(180); 
            }     
      
      
    }
    
}
if (-10<=data[0]<=10)

Doesn’t do what you think.
Have a look at using “&&”

Hey AWOL,
thanks for your reply.

The sketch is from a similar Project at “Instructables”: http://www.instructables.com/id/Android-RC-Car/?ALLSTEPS
It has the same scheme which I use in my Project.
Here´s the “Original” Code (he used i=1, doesnt matter at all). Look at the end. Can´t get any differences:

#include <MeetAndroid.h>

const int EnablePinDir = 10;
const int EnablePinMotor = 6;
const int LogicPin1Dir = 11;
const int LogicPin2Dir = 12;
const int LogicPin1Motor = 8;
const int LogicPin2Motor = 9;
float data[3] = {0};
int intdata[3] = {0};
int i = 1;

// 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);
  Serial.println("\t\t\t.----------------------.");
  Serial.println("\t\t\t|    Starting Up..     |");
  Serial.println("\t\t\t'----------------------'");
  pinMode(EnablePinDir, OUTPUT);
  pinMode(EnablePinMotor, OUTPUT);
  pinMode(LogicPin1Dir, OUTPUT);
  pinMode(LogicPin2Dir, OUTPUT);
  pinMode(LogicPin1Motor, OUTPUT);
  pinMode(LogicPin2Motor, OUTPUT);
  delay(1000);

  
  Serial.println("\t\t\t.----------------------.");
  Serial.println("\t\t\t|     PG-R Ready !     |");
  Serial.println("\t\t\t|      Have Fun !      |");
  Serial.println("\t\t\t'----------------------'");
  

  

  meetAndroid.registerFunction(floatValues, 'A');  


}

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
  meetAndroid.getFloatValues(data);
  
  // access the values
  for (int i=0; i<3;i++)
  {
    meetAndroid.send(data[i]);
  }
  
//        ..===========================================..
//        ||                                           ||
//        ||     Control Algorithm Beginning here      ||
//        ||                                           ||
//        ''===========================================''
  
  
//         This is for Forward/Reverse
      if (-10<=data[0]<=10)   {
//        Tilt-Proportional Speed 
        intdata[0] = int(data[0]);
        intdata[0] = intdata[0] * 24;
            if (data[0] <= -2) {
              intdata[0] = abs(intdata[0]);
              digitalWrite(LogicPin1Motor, HIGH);
              digitalWrite(LogicPin2Motor, LOW);
              analogWrite(EnablePinMotor, intdata[0]);
            }
            else if (data[0] >= 2) {
              digitalWrite(LogicPin1Motor, LOW);
              digitalWrite(LogicPin2Motor, HIGH);
              analogWrite(EnablePinMotor, intdata[0]);
            }   
            else if (-1<=data[0]<=1) {
              digitalWrite(LogicPin1Motor, LOW);
              digitalWrite(LogicPin2Motor, LOW);
              digitalWrite(EnablePinMotor, LOW);
            }  
      }

//        This is for Turn Left/Turn Right
    if (-10<=data[1]<=10)   {
                if (data[1] <= -2) {
                  digitalWrite(LogicPin1Dir, HIGH);
                  digitalWrite(LogicPin2Dir, LOW);
                  digitalWrite(EnablePinDir, HIGH);
                }
                else if (data[1] >= 2) {
                  digitalWrite(LogicPin1Dir, LOW);
                  digitalWrite(LogicPin2Dir, HIGH);
                  digitalWrite(EnablePinDir, HIGH);
                }
                else if (-1<=data[1]<=1) {
                  digitalWrite(LogicPin1Dir, LOW);
                  digitalWrite(LogicPin2Dir, LOW);
                  digitalWrite(EnablePinDir, LOW);
                }     
    }
    
}

Yup, that looks to be about the quality of stuff I've seen on the other Instructables I've been pointed to. I see that construct is used several times - I wonder if that is what the author intended?

So I´ve changed the Line

if (-10<=data[0]<=10)

into

if (data[1]>=-10 && data[1]<=10)

down´t work at all :0

any other corrections?

Have you verified you're receiving data? (In case you were wondering, the conditional would always have been true, for any value of "data[])

What do you mean with "verified"? The amarino accelerometer sends datas as an array of 3 values. Data[0] will take the first values and executes the if-case ( in the if-case is another if-case with furher instructions).

By "verified", I mean "how do you know you're receiving data?"

Got it!

if (-10<=data[0]<=10)

This code was correct.

But you have to regard one thing:
Before you Connect your Amarino with the Bluetooth-Module, start the Accelerometer-Event.
Only after you did this hit the “connect”-button.

– someone should write a goog tutorial 8) –
have a nice day
tk

Code:

if (-10<=data[0]<=10)

This code was correct.

Can you think of a value of “data[0]” that doesn’t cause that to evaluate as “true”?
You may as well have written if (1), or omitted the conditional altogether.

Explanation:
The expression is evaluated left to right.
Is data [0] less than or equal to -10?
This expression will evaluate to 0 for false, 1 for true.

Now, evaluate the next expression: Is 0 (or 1) less than or equal to +10?
In most parts of the Universe, this will always be true.