HazardsMind:
I'm not home right now, but I will give you a sample as soon as I can.
Edit: I was able to get this from a post I did a while back. Wow my iPhone likes to stretch things out. Sorry about that.
/*
control test
*/
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
...
Hi HazardsMind,
I went through the sample you suggested and noted what "I think" each line does. The only one which I am figuring out are:
- void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ???
- move(x, y, s); // DO NOT UNDERSTAND "move()"
//from http://arduino.cc/forum/index.php?topic=142777.0
/*
control test //name of program
*/
#include<string.h> //name of library needed
int DRV1,DRV2,STRR,STRL; //declare 4 integers
int x = 0; //intialize x
int y = 0; //initialize y
int s = 0; //initialize s
void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ???
//double z; //double the z variable (for more precision)
String val = ""; //set some buffer aside for val
String X= ""; //set some buffer aside for X
String Y= ""; //set some buffer aside for Y
String state = ""; //set some buffer aside for state
//int state = 0; //initialize state to 0
int currentCommand = 0; //initialize current command to 0
int ledpin = 13; //set LED pin
byte Mopen = 4; //sets Mopen to B0100
byte Mclosed = 2; //sets Mclosed to B00010
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM
void setup()
{
pinMode(ledpin, OUTPUT); // pin 13 (on-board LED) as OUTPUT
pinMode(Mopen, OUTPUT); // pin Mopen is set as an output
pinMode(Mclosed, OUTPUT); // pin Mclosed is set as an output
pinMode(M1L, OUTPUT); //pin MIL is set as an output
pinMode(M1R, OUTPUT); // in MIR is set as an output
pinMode(M2L, OUTPUT); //pin M2L is set as an output
pinMode(M2R, OUTPUT); // pin M2R is set as an output
Serial.begin(9600); // start serial communication at 115200bps
}
void loop() {
if( Serial.available()) // if data is available to read, this is how to check the serial part for strings
{
digitalWrite(ledpin, HIGH); // if data is detected set the pint set to ledpin to HIGH (ON)
char c= Serial.read(); // the string in serial read() is buffered in to c as characters
if (c == ','){ //look at the incoming chars, if it is a ',' then switch the case
currentCommand++; //for some reason currentCommand increments????
}
else { //if it is not ',' then store chars in string, keep storing the string until a , is found
val += c; // same as (val + c = c)val is the next string + the previous character stored in buffer
//Serial.println(val);
switch (currentCommand) { // for each string, currentCommand appears to increment by 1
case 0: //in the even there was only 1 string before the ',' X is equal to the val in buffer and val buffer is cleared ""
X += val; // same as (X + val = val), new variable X is equal to the whole string val before the ','
val = ""; // clear the information in the val buffer
break; // break out of case 0
case 1: //if currentCommand equals 1
Y += val; // same as (Y + val = val), Y is now the string waiting in the val buffer
val = ""; // clear out the val buffer
break; //get out of case 2
case 2:
//Serial.println("X: "+X);
//Serial.println("Y: "+Y);
state = val; // this will only receive 1 value
currentCommand = 0; //reset currentCommand counter to 0
val = ""; // clear out the val buffer
//Serial.println("state: "+state);
//Serial.println();
x=X.toInt(); //string to int , convert string X to integer
y=Y.toInt(); //string to int, convert string Y to integer
s=state.toInt(); //string to int, convert state to integer
X=""; Y=""; state=""; //clear the X, Y and state buffer information
move(x, y, s); // DO NOT UNDERSTAND "move()"
break; // get out of case 3
}
}
}
}
//***ALL THIS STUFF BELOW IS FOR SERVO MOVEMENT
//***SENDS OUT VOLTAGE VALUES
void move(int x, int y, int s)
{
x=constrain(x, -90, 90);
y=constrain(y, -90, 90);
//Movement varibles
int DRV2 = map(x, -90, 0, 255, 0);
int DRV1 = map(x, 0, 90, 0, 255);
int STRR = map(y, -90, 0, 255, 0);
int STRL = map(y, 0, 90, 0, 255);
if(x > 0)//forwards
{
//Serial.println("Driving");
analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));
analogWrite(M2L, 0); analogWrite(M2R, 0);
}
else if(x < 0)//backwards
{
//Serial.println("Driving");
analogWrite(M1L, 0); analogWrite(M1R, 0);
analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));
}
else if(x == 0 && y >0)//right
{
//Serial.println("Driving");
analogWrite(M1L, STRL); analogWrite(M1R, 0);
analogWrite(M2L, 0); analogWrite(M2R, STRL);
}
else if(x == 0 && y < 0)//left
{
//Serial.println("Driving");
analogWrite(M1L, 0); analogWrite(M1R, STRR);
analogWrite(M2L, STRR); analogWrite(M2R, 0);
}
else //full stop
{
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
if(s == 1)
{
//Serial.println("Claw Opening");
digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
}
if(s == 2)
{
//Serial.println("Claw Closing");
digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
}
if(s == 0)
{
digitalWrite(Mopen, LOW); digitalWrite(Mclosed, LOW);
}
else{
digitalWrite(ledpin, LOW);
x=0; y=0; s=0;
}
}