Hi all,
I am working on a project for Maker Faire this year as part of the Atmel booth, this part is basically a controller that controls a robot. One side has a rotary encoder with an XBee, NeoPixel ring, and the other is an XBee plus a ZumoBot (Pololu). XBee settings are correct, Series 1 directly to one another. I have successfully coded the Rotary encoder, NeoPixel ring, and FuzzBot, with the data sent via XBee being mapped (with the map() function) from 0-9, so it is a two-digit repeating data stream that the XBee should be receiving.
However, when I use a Serial Echo program (prints whatever it's receiving basically, with no complex functions at all), the numbers come out fine, so the problem isn't in the XBees is my understanding. I followed an XBee tutorial from Jeremy Blum, but the video said he was using a pre Arduino 1.0 version of the software. The part of the code: Serial.read() - '0'; he said was to convert the data into an integer, but I thought that was what I was doing before. Then, I saw that he had Serial.flush() at the end of his code (which is not supported in 1.0), and I think that is where the problems are in the code.
Any help?
Thanks,
q
The code below is for the Bot:
/* MFBA '14 Bot (NeoPixel Controller)
Uses FuzzBot + mount with XBee and LED's, received by the Neo-
Pixel controller board.
created 5.4.14
made by Quin Etnyre
kudos to Bildr, Adafruit
*/
#include <ZumoMotors.h>
ZumoMotors motors;
int turnVal;
int potVal;
void setup() {
Serial.begin(9600);
}
void loop() {
while(Serial.available() == 0);
int turnVal = Serial.read() - '0';
int potVal = Serial.read() - '0';
Serial.print(turnVal);
Serial.println(potVal);
int speedVal = map(potVal, 0, 9, 0, 5);
switch (turnVal) {
case 1:
motors.setLeftSpeed(9*speedVal);
motors.setRightSpeed(speedVal);
Serial.println("1");
delay(2);
break;
case 2:
motors.setLeftSpeed(8*speedVal);
motors.setRightSpeed(2*speedVal);
delay(2);
break;
case 3:
motors.setLeftSpeed(7*speedVal);
motors.setRightSpeed(3*speedVal);
delay(2);
break;
case 4:
motors.setLeftSpeed(6*speedVal);
motors.setRightSpeed(4*speedVal);
delay(2);
break;
case 5:
motors.setLeftSpeed(5*speedVal);
motors.setRightSpeed(5*speedVal);
delay(2);
break;
case 6:
motors.setLeftSpeed(4*speedVal);
motors.setRightSpeed(6*speedVal);
delay(2);
break;
case 7:
motors.setLeftSpeed(3*speedVal);
motors.setRightSpeed(7*speedVal);
delay(2);
break;
case 8:
motors.setLeftSpeed(2*speedVal);
motors.setRightSpeed(8*speedVal);
delay(2);
break;
case 9:
motors.setLeftSpeed(speedVal);
motors.setRightSpeed(9*speedVal);
delay(2);
break;
}
}
The code below is for the controller:
/* MFBA Controller
Using NeoPixel Ring (14) x2, rotary encoder (SFE, R/G),
slider pot (sm/med).
created 5.4.14
made by Quin Etnyre
kudos to Bildr, Adafruit
*/
#include <Adafruit_NeoPixel.h>
#define PIN 4 // NeoPixel Pin #
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(16, PIN); //x16
uint32_t color = 0xFFA500; // Start 'Qtechknow' Green
//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
int sensorValue; // integer for potentiometer
void setup() {
Serial.begin (9600);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
pixels.begin();
pixels.setBrightness(60); // 1/3 brightness
}
void loop(){
sensorValue=analogRead(0); // slider pot
int newValue = abs(encoderValue);
int finalValue = map(newValue, 300, 0, 0, 15);
pixels.setPixelColor(finalValue-1, 0);
pixels.setPixelColor(finalValue+3, 0);
pixels.setPixelColor(finalValue, color);
pixels.setPixelColor(finalValue+1, color);
pixels.setPixelColor(finalValue+2, color);
pixels.show();
int printVal = map(finalValue, 15, 0, 0, 9);
int printPot = map(sensorValue, 0, 1023, 0, 9);
Serial.print(printVal);
Serial.println(printPot);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
}
And here is the weird output I've been getting from the Bot's side's XBee output:
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
6-35
-387
67
6-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
6-35
-38-35
-38-35
-387
67
6-35
-387
67
6-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
67
67
6-35
-387
6-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
67
6-35
-38-35
-387
6-35
-387
67
6-35
-387
6-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
6-35
-38-35
-387
67
67
6-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-38-35
-387
6-35
-387
6-35
-387
67
67
6-35
-387
67
67
6-35
-38-35