Android Controlled Toy Using Raspberry Motor Shield

The terrain vehicle which is managed with raspberry pi, arduino and controlled via android software.

Story

At the end of the project we will manage a terrain vehicle which controlled by android device's accelemoter sensor

The project contains Motor shield, raspberry pi, arduino and dc motors devices.

STEP 1

First of all we should make I2C bus between raspberry pi and arduino. You can see why we choose I2C from here.

You can find required code which contains both I2C and motor shield process as show below.

#include 
#define MyAddress 0x40
#include 
AF_DCMotor motorhiz(3);
AF_DCMotor motoryon(4);
byte DataToBeSend[1];
byte ReceivedData;
int counter = 0;
void setup()
{
Serial.begin(9600);
  /* Initialize I2C Slave & assign call-back function 'onReceive' on 'I2CReceived'*/
  Wire.begin(MyAddress);
  Wire.onReceive(I2CReceived);
  Wire.onRequest(I2CRequest);
  motorhiz.setSpeed(254);
  motorhiz.run(RELEASE);
  motoryon.setSpeed(254);
  motoryon.run(RELEASE);
}
void loop()
{
  /* Increment DataToBeSend every second and make sure it ranges between 0 and 99 */
  //DataToBeSend[0] = (DataToBeSend[0] >= 99) ? 0 : DataToBeSend[0] + 1;
}
/* This function will automatically be called when RPi2 sends data to this I2C slave */
void I2CReceived(int NumberOfBytes)
{
 //counter++;
 //String counterStr = String(counter);
 //Serial.println("ReceivedData :"+counterStr);
  /* WinIoT have sent data byte; read it */
  ReceivedData = Wire.read();
  int ReceivedDataInt = (int)ReceivedData;
  String ReceivedDataStr = String(ReceivedData);
  Serial.println(ReceivedDataInt);
  if(ReceivedDataInt >=100)      //X Datası
  {
      Serial.println("DataX :"+ReceivedDataStr);
      if(ReceivedDataInt > 145 && ReceivedDataInt < 154)
      {
         Serial.println("RELEASE");
         motorhiz.run(RELEASE);
      }
      else if(ReceivedDataInt >= 100 && ReceivedDataInt < 104)
      {
         Serial.println("RELEASE");
         motorhiz.run(RELEASE);
      }
      else if(ReceivedDataInt >= 155)
      {
        ReceivedDataInt = ReceivedDataInt -155;
        int motorSpeed = (ReceivedDataInt * 10)+50;
        if(motorSpeed > 254)
        {
          motorSpeed = 254;
        }
        motorhiz.setSpeed(motorSpeed);
        motorhiz.run(BACKWARD); 
        String motorSpeedStr = String(motorSpeed);
        Serial.println("MotorHiz :"+motorSpeedStr);
      }
      else if(ReceivedDataInt >= 105  )
      {
          ReceivedDataInt = ReceivedDataInt -105;
          int motorSpeed = (ReceivedDataInt * 10)+50;
          if(motorSpeed > 254)
          {
           motorSpeed = 254;
          }
          motorhiz.setSpeed(motorSpeed);
          motorhiz.run(FORWARD); 
          String motorSpeedStr = String(motorSpeed);
          Serial.println("MotorHiz :"+motorSpeedStr);
      }
  }
  else  // Y Datası
  {
      Serial.println("DataX :"+ReceivedDataStr);
      if(ReceivedDataInt > 45 && ReceivedDataInt < 54)
      {
         Serial.println("RELEASE");
         motoryon.run(RELEASE);
      }
      else if(ReceivedDataInt >= 0 && ReceivedDataInt < 4)
      {
         Serial.println("RELEASE");
         motoryon.run(RELEASE);
      }
      else if(ReceivedDataInt >= 55)
      {
        ReceivedDataInt = ReceivedDataInt -55;
        int motorSpeed = (ReceivedDataInt * 12)+50;
        if(motorSpeed > 254)
        {
          motorSpeed = 254;
        }
        motoryon.setSpeed(motorSpeed);
        motoryon.run(BACKWARD); 
        String motorSpeedStr = String(motorSpeed);
        Serial.println("MotorHiz :"+motorSpeedStr);
      }
      else if(ReceivedDataInt >= 5  )
      {
          ReceivedDataInt = ReceivedDataInt -5;
          int motorSpeed = (ReceivedDataInt * 12)+50;
          if(motorSpeed > 254)
          {
           motorSpeed = 254;
          }
          motoryon.setSpeed(motorSpeed);
          motoryon.run(FORWARD); 
          String motorSpeedStr = String(motorSpeed);
          Serial.println("MotorHiz :"+motorSpeedStr);
      }
  }
}
/* This function will automatically be called when RPi2 requests for data from this I2C slave */
void I2CRequest()
{
//Serial.println("DataToBeSend");
  /*Send data to WinIoT */
  //Wire.write(DataToBeSend,1);
}
I2C's pin schema for comminication between arduino and raspberry pi in picture as     shown above. It's no need to use external power supply for arduino. Arduino will get  power from raspberry pi's Vout pin.

STEP 2

The required code for raspberry pi(working as a web server) which sends datas to arduino via I2C as shown below.

Code could find the codes end of the page (Code Section).

STEP 3

We develop Visual Studio Cordova Android Application that sends accelerometer sensor's data to web server using ajax requests.

Code could find the codes end of the page (Code Section).

BRIEFLY

1-) Android application sends sensor data(android phone accelerometer data) to web server which runs in raspberry pi.

2-) The web server sends data(raspberry pi gets data from android phone's accelerometer sensor) to arduino using I2C.

3-) Arduino process datas and runs motors using motors shields.

Schematics

Code

#include <Wire.h>
#define MyAddress 0x40
#include <AFMotor.h>

AF_DCMotor motorhiz(3);
AF_DCMotor motoryon(4);

byte DataToBeSend[1];
byte ReceivedData;
int counter = 0;

void setup()
{
  Serial.begin(9600);
    /* Initialize I2C Slave & assign call-back function 'onReceive' on 'I2CReceived'*/
    Wire.begin(MyAddress);
    Wire.onReceive(I2CReceived);
    Wire.onRequest(I2CRequest);
    motorhiz.setSpeed(254);
    motorhiz.run(RELEASE);

    motoryon.setSpeed(254);
    motoryon.run(RELEASE);
}

void loop()
{
    /* Increment DataToBeSend every second and make sure it ranges between 0 and 99 */
    //DataToBeSend[0] = (DataToBeSend[0] >= 99) ? 0 : DataToBeSend[0] + 1;
}

/* This function will automatically be called when RPi2 sends data to this I2C slave */
void I2CReceived(int NumberOfBytes)
{
   //counter++;
   //String counterStr = String(counter);
   //Serial.println("ReceivedData :"+counterStr);
   
    /* WinIoT have sent data byte; read it */
    ReceivedData = Wire.read();
    int ReceivedDataInt = (int)ReceivedData;
    String ReceivedDataStr = String(ReceivedData);
   
    Serial.println(ReceivedDataInt);
    if(ReceivedDataInt >=100)      //X Datası
    {
        Serial.println("DataX :"+ReceivedDataStr);
        if(ReceivedDataInt > 145 && ReceivedDataInt < 154)
        {
           Serial.println("RELEASE");
           motorhiz.run(RELEASE);
        }
        else if(ReceivedDataInt >= 100 && ReceivedDataInt < 104)
        {
           Serial.println("RELEASE");
           motorhiz.run(RELEASE);
        }
        else if(ReceivedDataInt >= 155)
        {
          ReceivedDataInt = ReceivedDataInt -155;
          int motorSpeed = (ReceivedDataInt * 10)+50;
          if(motorSpeed > 254)
          {
            motorSpeed = 254;
          }
          motorhiz.setSpeed(motorSpeed);
          motorhiz.run(BACKWARD); 
          String motorSpeedStr = String(motorSpeed);
          Serial.println("MotorHiz :"+motorSpeedStr);
        }
        else if(ReceivedDataInt >= 105  )
        {
            ReceivedDataInt = ReceivedDataInt -105;
            int motorSpeed = (ReceivedDataInt * 10)+50;
            if(motorSpeed > 254)
            {
             motorSpeed = 254;
            }
            motorhiz.setSpeed(motorSpeed);
            motorhiz.run(FORWARD); 
            String motorSpeedStr = String(motorSpeed);
            Serial.println("MotorHiz :"+motorSpeedStr);
        }
      
    }
    else  // Y Datası
    {
        Serial.println("DataX :"+ReceivedDataStr);
        if(ReceivedDataInt > 45 && ReceivedDataInt < 54)
        {
           Serial.println("RELEASE");
           motoryon.run(RELEASE);
        }
        else if(ReceivedDataInt >= 0 && ReceivedDataInt < 4)
        {
           Serial.println("RELEASE");
           motoryon.run(RELEASE);
        }
        else if(ReceivedDataInt >= 55)
        {
          ReceivedDataInt = ReceivedDataInt -55;
          int motorSpeed = (ReceivedDataInt * 12)+50;
          if(motorSpeed > 254)
          {
            motorSpeed = 254;
          }
          motoryon.setSpeed(motorSpeed);
          motoryon.run(BACKWARD); 
          String motorSpeedStr = String(motorSpeed);
          Serial.println("MotorHiz :"+motorSpeedStr);
        }
        else if(ReceivedDataInt >= 5  )
        {
            ReceivedDataInt = ReceivedDataInt -5;
            int motorSpeed = (ReceivedDataInt * 12)+50;
            if(motorSpeed > 254)
            {
             motorSpeed = 254;
            }
            motoryon.setSpeed(motorSpeed);
            motoryon.run(FORWARD); 
            String motorSpeedStr = String(motorSpeed);
            Serial.println("MotorHiz :"+motorSpeedStr);
        }
    }
    
}

/* This function will automatically be called when RPi2 requests for data from this I2C slave */
void I2CRequest()
{
  //Serial.println("DataToBeSend");
    /*Send data to WinIoT */
    //Wire.write(DataToBeSend,1);
}

Source: Android Controlled Toy Using Raspberry Motor Shield


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top