Hello all!
This is what I am looking at. The sketches I'm about to show are for my robot I'm building. So far I've built it in a superposition style- treads, arm, and sensors, each with its own corresponding arduino and processing sketches. The latest one I've made is having combines my code for the arm and treads, and I'm running into a problem that I've never seen before, and I'm at a loss as to what is going wrong.
Before I post my codes, here is what we are working with. My OS is OS X 10.6.3, I'm using Processing 1.2.1, Arduino 0022, and the Duemilanove with the Atmega328. The problem is the Processing code is the being sent properly, but it is not being received on the end of the arduino. I know this because I have a bit of code that causes an indicator LED that lights up when Serial.available() returns true, and the LED does not light up.
What confuses me is that I can still upload the older sketches, the ones that run just the treads or just the arm, and they work perfectly. This makes me think that it might not be a driver problem of some sort.
For some reason this one is simply not cooperating with me. Any ideas?
Here is my Arduino sketch:
/**************************************
***************************************
ArmTreadGUIArduino
By Matt Aud
Overview: This program will allow the user to contol the robot's treads and
arm via the keypad. Program will also incorporate the GUI
Works in Conjunction With: ArmTreadGUIProcessing
***************************************
***************************************/
/**************************************
***************************************
Set up the Servos
***************************************
***************************************/
//include the servos library
#include <Servo.h>
//now the servos themselves
Servo LEFTTREAD;
Servo RIGHTREAD;
Servo SHOULDER;
Servo ELBOW;
Servo GRIPPER;
/**************************************
***************************************
Set up the incoming byte array
***************************************
***************************************/
byte incoming[6];
//our servo initial positions
int Lspeed = 90; //left tread stop
int Rspeed = 90; //right tread stop
int shoulder = 90; //shoulder center
int elbow = 90; //elbow center
int gripper = 90; //gripper center
/**************************************
***************************************
Misc set up
***************************************
***************************************/
//our increment for the arm servos
//adjust this to adjust the speed
int increment = 3;
//set up out key
byte key = 4;
/*This byte "KEY" is one of the key obstacle to overcome. When a 2 byte array
was send over the serial cable, the data finds the proper place in the arduino code
without fail. Howver, once that byte array is increased in size to 3 or more bytes, the
incoming array in the arduino code will consistanty place the wrong data bytes into the
improper slots. This key, however, is the way to get around this obstacle. The arduino code
loads the array and does not begin placing the data into the proper cells until the "KEY"
is loaded. This allows the data to be placed in the proper place without error.
*/
//define our tester LED
#define LED 13
//This light wil light up when the incoming data is being received
/**************************************
***************************************
The Setup
***************************************
***************************************/
void setup()
{
/**************************************
ATTACH THE SERVOS
***************************************/
/*
Layout:
Servo ------- Pin Number
Left tread ------ 9
Right Tread ----- 10
Shoulder -------- 5
Elbow ----------- 6
Gripper --------- 3
*/
LEFTTREAD.attach(9);
LEFTTREAD.write(Lspeed);
RIGHTREAD.attach(10);
RIGHTREAD.write(Rspeed);
SHOULDER.attach(5);
SHOULDER.write(shoulder);
ELBOW.attach(6);
ELBOW.write(elbow);
GRIPPER.attach(3);
GRIPPER.write(gripper);
//our tester LED is an output
pinMode(LED, OUTPUT);
//turn on the serial port
Serial.begin(9600);
}
void loop()
{
//BRACE YOURSELVES!
/**************************************
LISTEN FOR OUT INCOMING ARRAY
***************************************/
if(Serial.available() == true)
{
digitalWrite(LED, HIGH);
}
while(Serial.available() == 6)
{
//once 5 bits are had, turn our out signal LED
// digitalWrite(LED, HIGH);
//look for our key before loading our array
if(Serial.read() == key)
{
//key has been found, load 'er up!
for(int n = 1; n < 6; n++)
{
incoming[n] = Serial.read();
}
}
}
//The array has bee loaded!
//Now we will interperet the data!
/**************************************
***************************************
INCOMING ARRAY
Array Order:
0 - key
1 - left tread
2 - right tread
3 - shoulder servo
4 - elbow servo
5 - gripper servo
***************************************
***************************************/
/**************************************
***************************************
BEGIN INPUT PROCESSING FOR THE TREADS
***************************************
***************************************/
if(incoming[1] == 0)
{
Lspeed = 178;
LEFTTREAD.write(Lspeed);
}
if(incoming[1] ==1)
{
Lspeed = 2;
LEFTTREAD.write(Lspeed);
}
if(incoming[1] == 2)
{
Lspeed = 90;
LEFTTREAD.write(Lspeed);
}
if(incoming[2] == 0)
{
Rspeed = 2;
RIGHTREAD.write(Rspeed);
}
if(incoming[2] ==1)
{
Rspeed = 178;
RIGHTREAD.write(Rspeed);
}
if(incoming[2] == 2)
{
Rspeed = 90;
RIGHTREAD.write(Rspeed);
}
/**************************************
***************************************
BEGIN INPUT PROCESSING FOR THE ARM
***************************************
***************************************/
//shoulder servo
if( incoming[3] == 0 && shoulder > 0)
{
shoulder -= increment;
SHOULDER.write(shoulder);
delay(10);
}
if( incoming[3] == 1 && shoulder < 180)
{
shoulder += increment;
SHOULDER.write(shoulder);
delay(10);
}
//elbow servo
if(incoming[4] == 0 && elbow > 0)
{
elbow -= increment;
ELBOW.write(elbow);
delay(10);
}
if(incoming[4] == 1 && elbow < 180)
{
elbow += increment;
ELBOW.write(elbow);
delay(10);
}
//gripper servo
if(incoming[5] == 0 && gripper > 0)
{
gripper -= increment;
GRIPPER.write(gripper);
delay(10);
}
if(incoming[5] == 1 &&gripper < 180)
{
gripper += increment;
GRIPPER.write(gripper);
delay(10);
}
}