I have developt a code to control my little robot from my iphone.
I wanted to control the motors and servo at the same time, but i could not find anything online to do this, so i tried to combine some codes myself .
I used these two links: GitHub - jaxzin/arduino_touchosc_servo_controller: Control a servo from an on-screen iOS dial projectallusion.com
Also i got a lot of help from the arduino forum.
I my setup are: - a arduino mega 1280
- a dagu micro servo ( i am planning to use more )
- a iphone
- a dagu rover 5
- a dagu 4 channel motor controller
this is the processing code:
import oscP5.*; // Load OSC P5 library
import netP5.*; // Load net P5 library
import processing.serial.*;
Serial arduinoPort;
OscP5 oscP5;
float [] fader = new float [3];
int [] led = new int [2];
int currentPos = 0;
int desiredPos = 0;
void setup()
{
arduinoPort = new Serial(this, Serial.list()[3], 9600);
size(360, 200);
oscP5 = new OscP5(this,8000);
println("ready for use");
}
void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
if(addr.indexOf("/1/fader") !=-1){
String list[] = split(addr,'/');
int xfader = int(list[2].charAt(5) - 0x30);
if(theOscMessage.get(0).floatValue() !=0){
fader[xfader] = theOscMessage.get(0).floatValue();
}}
if(addr.indexOf("/1/servo") !=-1){ // Filters out any toggle buttons
//int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
desiredPos = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
// Button values can be read by using led[0], led[1], led[2], etc.
}
}
void draw()
{
// -------------------------------- Motor A
if(fader[1] > 0.65){
arduinoPort.write ("AF200#");
}
if(fader[1] < 0.35){
arduinoPort.write ("AR200#");
}
//--------------------------------Motor B
if(fader[2] > 0.65){
arduinoPort.write ("BF200#");
}
if(fader[2] < 0.35){
arduinoPort.write ("BR200#");
}
//----------------------------stop commands
if(fader[1] < 0.65 && fader[1] > 0.35 ){
arduinoPort.write ("AF0#");
}
if(fader[2] < 0.65 && fader[2] > 0.35 ){
arduinoPort.write ("BF0#");
}
//--------------------------------- Servo
if(currentPos != desiredPos) {
String pos = str(desiredPos);
arduinoPort.write("UP"+pos);
arduinoPort.write("#");
currentPos = desiredPos;
}}
this is my arduino code:
#include <Servo.h>
Servo myservo;
#define PwmPinMotorA 10
#define PwmPinMotorB 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13
#define SerialSpeed 9600
#define BufferLength 16
#define LineEnd '#'
int message = 0;
int redLED = 0;
char inputBuffer[BufferLength];
void setup()
{
// motor pins must be outputs
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
myservo.attach(9);
Serial.begin(SerialSpeed);
}
// process a command string
void HandleCommand(char* input, int length)
{
Serial.println(input);
if (length < 2) { // not a valid command
return;
}
int value = 0;
// calculate number following command
if (length > 2) {
value = atoi(&input[2]);
}
int* command = (int*)input;
// check commands
// note that the two bytes are swapped, ie 'RA' means command AR
switch(*command) {
case 'FA':
// motor A forwards
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case 'RA':
// motor A reverse
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorA, LOW);
break;
case 'FB':
// motor B forwards
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, LOW);
break;
case 'RB':
// motor B reverse
analogWrite(PwmPinMotorB, value);
digitalWrite(DirectionPinMotorB, HIGH);
// servo control
case 'PU':
myservo.write(value);
break;
default:
break;
}
}
void loop()
{
// get a command string form the serial port
int inputLength = 0;
do {
while (!Serial.available()); // wait for input
inputBuffer[inputLength] = Serial.read(); // read it in
} while (inputBuffer[inputLength] != LineEnd && ++inputLength < BufferLength);
inputBuffer[inputLength] = 0; // add null terminator
HandleCommand(inputBuffer, inputLength);
}
//----------- End Arduino code ---------------