Volkan Sarıoğlu
Published © GPL3+

Android Controlled Toy Using Raspberry Motor Shield

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

AdvancedFull instructions provided9,306
Android Controlled Toy Using Raspberry Motor Shield

Things used in this project

Story

Read more

Custom parts and enclosures

Pin Diagram

Arduino to raspberry pin diagram

Schematics

General Comminication Schema

for details: https://www.hackster.io/AnuragVasanwala/home-automation-0dcefc

Code

Wiri.h I2C Comminication and moto shield android code

Arduino
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 Controller

C#
Android Remote Controller
No preview (download only).

Windows IOT Web Server

C#
Windows IOT Web Server
No preview (download only).

Credits

Volkan Sarıoğlu

Volkan Sarıoğlu

1 project • 3 followers

Comments