Thanks for the replies. After the first response by michinyon, I took out the ln part of the Serial.println, and it worked great, but of course since the data was not separated by anything at all, (0 and 6 would come out as 606060606060 which could be interpreted as 6, and 0, or 0, and 6), I added in something to show what data was what. I added in a '~' before one of the values, and none of the values excede single digits, so then you could tell which piece of data was what. Although, there is occasionally an extra '678' somewhere in there which is throwing the bot off. This then makes the robot less smooth at moving, stuttering a lot, which will eventually burn out the motors. I am looking to make the FuzzBot move more smoothly (somewhere in the code), any ideas?
I have adjusted the code again, and here is the new code for the Bots:
/* 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;
int lastTurnVal;
int thirdVal;
int fourthVal;
void setup() {
Serial.begin(9600);
}
void loop() {
while(Serial.available()) {
if(Serial.read() == '~') {
int turnVal = Serial.read() - '0';
int potVal = Serial.read() - '0';
Serial.print(turnVal);
Serial.println(potVal);
int speedVal = map(potVal, 0, 9, 0, 25);
switch (turnVal) {
case 1:
if(lastTurnVal == 1) {
if(thirdVal == 1) {
if(fourthVal == 1) {
break;
}
}
}
motors.setLeftSpeed(speedVal);
motors.setRightSpeed(9*speedVal);
break;
case 2:
if(lastTurnVal == 2) {
if(thirdVal == 2) {
if(fourthVal == 2) {
break;
}
}
}
motors.setLeftSpeed(2*speedVal);
motors.setRightSpeed(8*speedVal);
break;
case 3:
if(lastTurnVal == 3) {
if(thirdVal == 3) {
if(fourthVal == 3) {
break;
}
}
}
motors.setLeftSpeed(3*speedVal);
motors.setRightSpeed(7*speedVal);
break;
case 4:
if(lastTurnVal == 4) {
if(thirdVal == 4) {
if(fourthVal == 4) {
break;
}
}
}
motors.setLeftSpeed(4*speedVal);
motors.setRightSpeed(6*speedVal);
break;
case 5:
if(lastTurnVal == 5) {
if(thirdVal == 5) {
if(fourthVal == 5) {
break;
}
}
}
motors.setLeftSpeed(5*speedVal);
motors.setRightSpeed(5*speedVal);
break;
case 6:
if(lastTurnVal == 6) {
if(thirdVal == 6) {
if(fourthVal == 6) {
break;
}
}
}
motors.setLeftSpeed(6*speedVal);
motors.setRightSpeed(4*speedVal);
break;
case 7:
if(lastTurnVal == 7) {
if(thirdVal == 7) {
if(fourthVal == 7) {
break;
}
}
}
motors.setLeftSpeed(7*speedVal);
motors.setRightSpeed(3*speedVal);
break;
case 8:
if(lastTurnVal == 8) {
if(thirdVal == 8) {
if(fourthVal == 8) {
break;
}
}
}
motors.setLeftSpeed(8*speedVal);
motors.setRightSpeed(2*speedVal);
break;
case 9:
if(lastTurnVal == 9) {
if(thirdVal == 9) {
if(fourthVal == 9) {
break;
}
}
}
motors.setLeftSpeed(9*speedVal);
motors.setRightSpeed(speedVal);
break;
}
thirdVal = lastTurnVal;
lastTurnVal = turnVal;
}
}
}
And here is the newest controller code:
/* 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>
#include <SoftwareSerial.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;
SoftwareSerial Serial7Segment(7, 8); //RX pin, TX pin
int cycles = 0;
int runClock = HIGH;
int i = 3000;
int sensorValue; // integer for potentiometer
void setup() {
Serial.begin (9600);
Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
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(newValue, 0, 300, 0, 9);
int printPot = map(sensorValue, 0, 1023, 0, 9);
Serial.write(0x7E);
Serial.print(printVal);
Serial.print(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 data I'm getting from the bot's side after I try and decode the data:
46
46
46
46
678
46
678
46
678
46
678
46
678
46
678
46
678
46
46
46
46
46
Thanks,
Quin