Go Down

Topic: Android accelerometer controllable Arduino tank (Read 1 time) previous topic - next topic

wizard123

Jan 09, 2013, 10:54 pm Last Edit: Jan 09, 2013, 10:57 pm by wizard123 Reason: 1
http://www.youtube.com/watch?v=R2D1OvixbP0
So i made Android app that allows to control Arduino by Android phone accelerometer. I am using JY-MCU bluetooth module which is connected to Arduino mega UART pins. This app also allows you to control servo, there is seekbar in mainscreen. The tank itself is made of Tamiya tracked vehicle chassis kit(TAM70108) and Tamiya double gearbox(70097).
If someone else wants to try, here is the code and app in attachments. This apllication will work only with android 4.0+ device. Don't even try to install this to android 2.3.
Code: [Select]
#include <Servo.h>

float xAxis = 0;
float yAxis = 0;
float zAxis = 0;
// These are the xAxis and yAxis values that are scaled to -255 to 255
int cx = 0;
int cy = 0;
int cz = 0;
// This is the servo value which is controlled by seekbar in the phone
int servo = 0;
Servo servo1;
// For mapping values
float inMin = -9.999;
float inMax =  9.999;
int   outMin = -255;
int   outMax = 255;
boolean turningLeft = false;
boolean turningRight = false;
// Channel A
const int motorLeftDirection = 12;
const int motorLeftBrake = 9;
const int motorLeftSpeed = 3;
// Channel B
const int motorRightDirection = 13;
const int motorRightBrake = 8;
const int motorRightSpeed = 11;
void setup() {
 // initialize both serial ports:
 Serial.begin(57600);
 Serial1.begin(57600);
 pinMode(motorLeftDirection, OUTPUT);
 pinMode(motorLeftBrake, OUTPUT);
 pinMode(motorRightDirection, OUTPUT);
 pinMode(motorRightBrake, OUTPUT);
 Serial.println("START");
 // Turn on servo which is connected to pin: 9
 servo1.attach(9);
}

void loop() {
 // read from port 1, send to port 0:
 if (Serial1.available()) {
   byte command = Serial1.read();
   switch(command) {
     // If one of incoming bytes are 'x', Arduino will parse the incoming bytes to first valid floating point from the serial buffer.  
     case 'x':
       xAxis = Serial1.parseFloat();
       // Scale the xAxis value from (-9.999 to 9.999) to (-255 to 255)
       // We are using this math function because the Arduino's "map" function dosen't support floating points
       cx = (xAxis - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
       // Print the scaled x-axis value to the Serial Monitor
       Serial.print("x axis: ");
       Serial.print(cx);
       Serial.println("");
       break;
     case 'y':
       yAxis = Serial1.parseFloat();
       cy = (yAxis - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
       Serial.print("y axis: ");
       Serial.print(cy);
       Serial.println("");
       break;
     case 'z':
       zAxis = Serial1.parseFloat();
       cz = (zAxis - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
       Serial.print("z axis: ");
       Serial.print(cz);
       Serial.println("");
     case 's':
       servo = Serial1.parseInt();
       Serial.print("servo: ");
       Serial.print(servo);
       Serial.println("");
       break;
     }
   
if(cx < 0 && turningLeft == false && turningRight == false ){
  int cxInverted = cx * -1;
  // Set motors direction to forward
  digitalWrite(motorLeftDirection, HIGH);
  digitalWrite(motorRightDirection, HIGH);
  // Release motors brake
  digitalWrite(motorLeftBrake, LOW);
  digitalWrite(motorRightBrake, LOW);
  // Set motors speed
  analogWrite(motorLeftSpeed, cxInverted);
  analogWrite(motorRightSpeed, cxInverted);
}

if(cx > 0 && turningLeft == false && turningRight == false){
  // Set motors direction to backward
  digitalWrite(motorLeftDirection, LOW);
  digitalWrite(motorRightDirection, LOW);
  // Release motors brake
  digitalWrite(motorLeftBrake, LOW);
  digitalWrite(motorRightBrake, LOW);
  // Set motors speed
  analogWrite(motorLeftSpeed, cx);
  analogWrite(motorRightSpeed, cx);
  }
 
if(cy < -50){
  turningLeft = true;
  int cyInverted = cy * -1;
  // Set motors direction to left
  digitalWrite(motorLeftDirection, LOW);
  digitalWrite(motorRightDirection, HIGH);
  // Release motors brake
  digitalWrite(motorLeftBrake, LOW);
  digitalWrite(motorRightBrake, LOW);
  // Set motors speed
  analogWrite(motorLeftSpeed, cyInverted);
  analogWrite(motorRightSpeed, cyInverted);
}

if(cy > 50){
  turningRight = true;
  // Set motors direction to right
  digitalWrite(motorLeftDirection, HIGH);
  digitalWrite(motorRightDirection, LOW);
  // Release motors brake
  digitalWrite(motorLeftBrake, LOW);
  digitalWrite(motorRightBrake, LOW);
  // Set motors speed
  analogWrite(motorLeftSpeed, cy);
  analogWrite(motorRightSpeed, cy);
}

if(cy > -50) {
    turningLeft = false;
  }
  if(cy < 50) {
    turningRight = false;
  }
 
 }
 servo1.write(servo);
 
}

cyclegadget


Nice work! I hope to try it out in the next few days. Thank you!

Go Up