Alright guys... I have some code now. I've been testing it to various levels of success. Hopefully there is something glaringly obvious to those who are more coding proficient.
Here is my Arduino Code:
//include the software serial library
#include <SoftwareSerial.h>
//define the software serial tx and rx pins
#define rxPin 9
#define txPin 8
// set up the 'fake' serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
/**
- the following is WORKING code that sets the forward speed of both motors.
**/
void put(unsigned int m1, unsigned int m2){
//we use the put command to set the speed of the motors
mySerial.print(0x80,BYTE); //start byte
mySerial.print(0x07,BYTE); //device id
mySerial.print(0x55,BYTE); //command number
mySerial.print(m1,BYTE); //m1 speed
mySerial.print(m2,BYTE); //m2 speed
}
int serialInArray[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int m1=0;
int m2=0; // Starting position of the ball
boolean firstContact = false; // Whether we've heard from the microcontroller
void setup() {
Serial.begin(9600);
//setup for mySerial port
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
mySerial.begin(19200); // this must be 19200 because that is the standard for the TreX motor controller
}
void loop(){
// read a byte from the serial port:
int inByte = Serial.read();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
Serial.flush(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
Serial.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 1 ) {
m1 = serialInArray[0];
m2 = serialInArray[1];
// print the values (for debugging purposes only):
Serial.println(m1 + "\t" + m2);
// Send a capital A to request new sensor readings:
Serial.write('A');
// Reset serialCount:
serialCount = 0;
}
}
put(m1,m2);
}
Note that if when I just use put(200,200) i get a response from my motor controller. This tells me that there is no problem with my software serial connection, and it has to be with the connection between the Arduino and processing.
Here is the code I use for processing:
import processing.serial.*;
Serial myPort; // The serial port
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
void setup() {
size(256, 256); // Stage size
noStroke(); // No border on the next thing drawn
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
establishContact(); // send a byte to establish contact until Arduino responds
}
void draw() {
{
// if we get a valid byte, read analog ins:
if (myPort.available() > 0) {
// get incoming byte:
inByte = myPort.read();
// send sensor values:
myPort.write(100);
myPort.write(100);
}
}
}
void establishContact() {
while (myPort.available() <= 0) {
myPort.write('A'); // send a capital A
delay(300);
}
}
Thoughts?