It´s code to controll a robot. The controller has 4 pot and 8 buttons.
Sending Xbee code (Which seems to work as I read it correctly in the serial monitor:
// <HjulServo1,HjulServo2,ÖgonServo1,ÖgonServo2, Led (0= Av med allt 1=Röst 2= Hjärta 3=OK 4=Glad 5=Sur 6=Pepparkaka 7=Julgran 8=Loading, Voice data) >
//
#define SERVOMIN_EyeDown 1250 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_EyeUp 1750 // this is the 'maximum' pulse length count (out of 4096)
#define SERVOMIN_EyeRight 1250 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_EyeLeft 1750 // this is the 'maximum' pulse length count (out of 4096)
#define SERVOMIN_RightWheelFW 1430 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_RightWheelBW 1570 // this is the 'maximum' pulse length count (out of 4096)
#define SERVOMIN_LeftWheelFW 1430 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_LeftWheelBW 1570 // this is the 'maximum' pulse length count (out of 4096)
int potpin1 = A0; //Ögonservo1 Blå_A0 Grön_GND Orange_5V
int potpin2 = A1; //Ögonservo2 Röd_A1 Grå_GND Rosa_5V
int potpin3 = A2; //Hjulservo1-Höger Blå_A0 Grön_GND Orange_5V
int potpin4 = A3; //Hjulservo-Väster Blå_A0 Grön_GND Orange_5V
int voicePin = A4; //Läser Voice data
int button1 = 4; //Voice -rosa
int button2 = 5; //Heart -orange
int button3 = 6; //OK -rosa
int button4 = 7; //Glad -orange
int button5 = 8; //Sur - gul
int button6 = 9; //Pepperkaka -vit
int button7 = 10;//Julgran -orange
int button8 = 11;//Loading - gul
int angv1 = 0;
int angv2 = 0;
int angv3 = 0;
int angv4 = 0;
int ledType = 0;
int voiceData = 0;
const int sampleWindow = 20; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample = 0;
void setup() {
Serial.begin(9600);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(button5, INPUT_PULLUP);
pinMode(button6, INPUT_PULLUP);
pinMode(button7, INPUT_PULLUP);
pinMode(button8, INPUT_PULLUP);
}
void loop() {
getJoystickData();
calcJoystickData();
getLedData();
getVoiceData();
sendData();
//delay(10); //For stability
}
void getJoystickData() {
angv1 = analogRead(potpin1);
angv2 = analogRead(potpin2);
angv3 = analogRead(potpin3);
angv4 = analogRead(potpin4);
}
void calcJoystickData() {
angv1 = map(angv1, 95, 285, SERVOMIN_EyeDown, SERVOMAX_EyeUp);
angv2 = map(angv2, 110, 250, SERVOMIN_EyeRight, SERVOMAX_EyeLeft);
angv3 = map(angv3, 120, 240, SERVOMIN_RightWheelFW, SERVOMAX_RightWheelBW);
angv4 = map(angv4, 130, 240, SERVOMIN_LeftWheelFW, SERVOMAX_LeftWheelBW);
}
void sendData() {
Serial.print("<");
Serial.print(angv1);
Serial.print(",");
Serial.print(angv2);
Serial.print(",");
Serial.print(angv3);
Serial.print(",");
Serial.print(angv4);
Serial.print(",");
Serial.print(ledType);
Serial.print(",");
Serial.print(voiceData);
Serial.println(">");
}
void getLedData() {
ledType = 1; //If nothing else is on Go Voice Mode
if (digitalRead(button2) == LOW) { //Heart on
ledType = 2;
//Serial.println(ledType);
}
if (digitalRead(button3) == LOW) { //OK On
ledType = 3;
}
if (digitalRead(button4) == LOW) { //Glad. If off revert to Voice control
ledType = 4;
}
if (digitalRead(button5) == LOW) { //Sur. If off revert to Voice control
ledType = 5;
}
if (digitalRead(button6) == LOW) { //Pepparkaka. If off revert to Voice control
ledType = 6;
}
if (digitalRead(button7) == LOW) { //Julgran. If off revert to Voice control
ledType = 7;
}
if (digitalRead(button8) == LOW) { //Loading. If off revert to Voice control
ledType = 8; //
}
if ((digitalRead(button1) == HIGH) && (ledType == 1)) {
//Voice control DEACTIVATED if button is turned off and
// no other buttons are pressed = show nothing
ledType = 0;
}
}
void getVoiceData() {
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(voicePin);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
voiceData = (peakToPeak ); // convert to volts
//Serial.print("voiceData= ");
//Serial.println(voiceData);
}
Please have a look at the receiving code:
int angv1 = 0; //Ögon Upp/Ner
int angv2 = 0; //Ögon Höger/Vänster
int angv3 = 0; //Hjul Höger som står på konsol
int angv4 = 0; //Hjul Vänster somstår på konsol
int ledType = 0;
int voiceDataIn = 0;
int servo1 = 1;
int servo2 = 2;
int servo3 = 3;
int servo4 = 4;
const byte numChars = 32; //<1234,6789,1234,6789,0,1234>
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker ) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
parseData();
//showParsedData();
newData = false;
}
}
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars, ","); // get the first part - the string
angv1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
angv2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
angv3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
angv4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
ledType = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
voiceDataIn = atoi(strtokIndx); // convert this part to an integer
newData = false;
}
void showParsedData() {
Serial.print("<");
Serial.print(angv1);
Serial.print(",");
Serial.print(angv2);
Serial.print(",");
Serial.print(angv3);
Serial.print(",");
Serial.print(angv4);
Serial.print(",");
Serial.print(ledType);
Serial.print(",");
Serial.print(voiceDataIn);
Serial.println(">");
}
What i receive is distorted:
This just in ... 1713,1328,1434,16288,1,0
This just in ... 1713,1325,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,2
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1,1,0
This just in ... 1713,1325,1434,162825,1434,1628
This just in ... 1713,130
This just in ... 1713,1328
<1713,1328,1434,1628
This just in ... 1713,13281,0
This just in ... 1713,1,0
This just in ... 1713,1328,1434,1628,1,1
This just in ... 1713,628,1,0
This just in ... 1713,,1,0
This just in ... 1713,1,0
This just in ... 1713,1317,1433,162928,1434,1628
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,1
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,0
This just in ... 1713,1328,1434,1628,1,0
Is there a way to discard corrupt parts?
The Xbees are close to one and other (40cm).
All help is welcome.
Thank you!
/O