Arduino Template Sketch

265 views
Skip to first unread message

Dave Smart

unread,
Sep 23, 2015, 8:04:06 AM9/23/15
to AndroidScript
Hi Guys,

Here is an Arduino sketch that we used for our Instructables Robot Rover:- http://www.instructables.com/id/Smart-Robot-with-Vision-and-Voice-control

This sketch may be useful as a template for creating other Arduino programs which need communicate with DroidScript via USB or Bluetooth serial. It allows receiving of 3 letter commands with optional extra parameters.  You can easily test it from the Arduino serial console too :)


//-------------------------------------------------------
// Smart Rover - Arduino Sketch for controlling a 
// Makeblock robots from DroidScript via OTG cable.
//
// This program can be tested using the Aurduino serial
// monitor.  
//
// Examples: 
//   type 'lft100x' to turn left at speed 100.
//   type 'buzy' to turn buzzer on, 'buzn' to turn it off.
//
// Copyright: droidscript.org
// License: Creative Commons Attribution ShareAlike 3.0
//--------------------------------------------------------
 
#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

//Global variables.
char g_version[] = "0.10\n";
MeDCMotor g_motorL( M1 );  
MeDCMotor g_motorR( M2 );
MeUltrasonicSensor ultraSensor( PORT_4 ); 
unsigned long timer = 0; 

//Setup the hardware.
void setup() 
{
  //Setup USB serial comms.
  Serial.begin( 115200 );
  Serial.setTimeout( 100 );
}

//This function is called forever.
void loop() 
{
  //Read serial commands.
  while( Serial.peek() != -1 )
  {
    //Read 3 character command.
    char cmd[4] = "---";
    Serial.readBytes( cmd, 3 );
    
    //Execute command.
    if( strcmp( cmd, "buz" )==0 ) Buzzer();
    else if( strcmp( cmd, "stp" )==0 ) Stop(); 
    else if( strcmp( cmd, "fwd" )==0 ) Forward();
    else if( strcmp( cmd, "rev" )==0 ) Reverse();
    else if( strcmp( cmd, "lft" )==0 ) Left();
    else if( strcmp( cmd, "rgt" )==0 ) Right();
    else if( strcmp( cmd, "ver" )==0 ) GetVersion();
  } 
  
  //Report status every second.
  if( (millis()-timer) > 1000 ) 
  {
     timer += 1000;
     Report();
  }
}

//Get software version.
void GetVersion()
{
  Serial.print( g_version );
}

//Send status back to DroidScript App.
void Report()
{
  int dist = ultraSensor.distanceCm();
  Serial.println( String("dist:") + dist + String(";") );
}

//Control buzzer.
void Buzzer()
{
  //Read on/off parameter.
  char onOff;
  Serial.readBytes( &onOff, 1 );
  
  //Start of stop buzzer.
  if( onOff=='y' ) buzzerOn();
  else buzzerOff();
}

//Turn vehicle left.
void Left()
{
  //Read speed parameter.
  int speed = Serial.parseInt();
  
  //Turn on motors.
  g_motorL.run( -speed );
  g_motorR.run( speed );
}

//Turn vehicle right.
void Right()
{
  //Read speed parameter.
  int speed = Serial.parseInt();
  
  //Turn on motors.
  g_motorL.run( speed );
  g_motorR.run( -speed );
}

//Drive vehicle forward.
void Forward()
{
  //Read speed parameter.
  int speed = Serial.parseInt();
  
  //Turn on motors.
  g_motorL.run( speed );
  g_motorR.run( speed );
}

//Reverse vehicle.
void Reverse()
{
  //Read speed parameter.
  int speed = Serial.parseInt();
  
  //Turn on motors.
  g_motorL.run( -speed );
  g_motorR.run( -speed );
}

//Stop vehicle.
void Stop()
{
  //Turn off motors.
  g_motorL.run( 0 );
  g_motorR.run( 0 );
}


Dave Smart

unread,
Sep 23, 2015, 8:20:03 AM9/23/15
to AndroidScript
Here are some snippets of code for the DroidScript end when using USB (via OTG cable):-

//Called when application is started.
function OnStart()
{
    :
    :
    //Create USB serial object.
    usb = app.CreateUSBSerial( 115200,8,1,0 ); 
    if( !usb ) 
    {
        app.ShowPopup( "Please connect your Arduino and restart" );
        return;
    }
    usb.SetOnReceive( usb_OnReceive );
    app.ShowPopup( "Connected" );
}


//Called when we get data from Arduino.
function usb_OnReceive( data )
{
   console.log( data );
}

//Send a text command to Arduino.
function Send( cmd )
{
    console.log( cmd );
    if( !usb ) return;
    usb.Write( cmd );
}

Reply all
Reply to author
Forward
0 new messages