Project tutorial
Android Controlled Toy Using Raspberry Motor Shield

Android Controlled Toy Using Raspberry Motor Shield © GPL3+

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

  • 6,973 views
  • 6 comments
  • 23 respects

Components and supplies

Apps and online services

About this project

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.

Test On The Road

Code

Wiri.h I2C Comminication and moto shield android codeArduino
I2C comminication between arduino and raspberry pi arduino code. It is for serial comminication for two devices with minumum input output source.
#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);
}
Android Remote ControllerC#
Android Remote Controller
No preview (download only).
Windows IOT Web ServerC#
Windows IOT Web Server
No preview (download only).

Custom parts and enclosures

Pin Diagram
Arduino to raspberry pin diagram
Main%20circuit%20diagram

Schematics

General Comminication Schema
for details: https://www.hackster.io/AnuragVasanwala/home-automation-0dcefc
Abstract main

Comments

Similar projects you might like

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 297,267 views
  • 98 comments
  • 706 respects

Chromotherapy with Raspberry and Arduino

Project tutorial by Davide Longo

  • 6,254 views
  • 0 comments
  • 40 respects

MannyBot365

Project showcase by 3 developers

  • 2,939 views
  • 0 comments
  • 26 respects

Robot Arm Controlled Through Ethernet

Project tutorial by Balázs Simon

  • 4,415 views
  • 0 comments
  • 32 respects

Pool Controller

Project tutorial by Mike Mackes

  • 50,033 views
  • 42 comments
  • 115 respects
Add projectSign up / Login