Hello, been lurking around here a few weeks. Some of you have some very cool projects. First a warning, I’m a hardware guy not a programmer so I have little knowledge of code, but I can cut and paste really good :mrgreen:
I am currently working on a remote control system for a differential drive rover with a ArduinoFio/2.4 series 1 xbee and Wii chuck on the transmitter end
and Arduino Uno/2.4 series 1 xbee for a receiver (servos for wheel motors right now).
Now, the problem, I have the TX end mixing the wii chuck x/y. Monitoring output of the TX on serial seems to be pretty responsive. Now my lacking ability of code work the receiver
lags horribly while trying to read the incoming packets. This is where I could use some help… (code is a mess of stuff I wrote and snippets I found online)
TX Code (some stuff is commented out for debugging and I am missing a LCD on the TX right now)
ETA: Code
/*
* WiiChuck-Fio Trasmitter //(with LCD output)
*
*
*
*/
#include <Wire.h>
//#include <LiquidCrystal.h>
#include "nunchuck_funcs.h"
#define debounce 10 // ms debounce
#define holdTime 2000 // ms hold period: how long to wait for press+hold event
#define DEBUG false // set to false to stop serial messages
int loop_cnt=0;
int loop_cnt2=0;
int throttle;
int ledPin0 = 9;
int ledPin1 = 10;
int ledPin2 = 11;
int ledPin3 = 13;
byte accx,accy,zbut,cbut,xjoy,yjoy,xraw, yraw;
//LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // LCD pins
int cbutVal = 0; // value read from button
int cbutLast = 0; // buffered value cbut previous state
int zbutVal = 0;
int zbutLast = 0;
long btnDnTime; // time the button was pressed down
long btnUpTime; // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
boolean ledVal0 = false; // state of LED 0
boolean ledVal1 = false; // state of LED 1
boolean ledVal2 = false; // state of LED 2
boolean ledVal3 = false; // state of LED 3
void setup() {
{
nunchuck_init(); // send the initilization handshake
pinMode(ledPin0, OUTPUT);
digitalWrite(ledPin0, ledVal0);
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin1, ledVal1);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, ledVal2);
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, ledVal3);
Serial.begin(57600);
//lcd.begin(16, 2);
//lcd.print("WiiChuckServo ready"); // Print a message to the LCD.
//delay(10);*/
delay(10);
}
}
void loop()
{
{
{
{
if( loop_cnt > 100 ) { // every 100 msecs get new data
loop_cnt = 0;
nunchuck_get_data();
accx = nunchuck_accelx(); // ranges from approx 70 - 182 roll
accy = nunchuck_accely(); // ranges from approx 65 - 173 pitch
zbut = nunchuck_zbutton(); // 0 open 1 closed
cbut = nunchuck_cbutton(); // 0 open 1 closed
xraw = nunchuck_joyx(); // ranges from aprox 27 - 224 c=127-129 left - right
yraw = nunchuck_joyy(); // ranges from aprox 29 - 226 c=129-132 down - up
/*lcd.begin(16, 2);
lcd.print("X:");lcd.print((byte)valx,DEC);lcd.write(0xdf);
lcd.setCursor(6, 0);
lcd.print("Y:");lcd.print((byte)valy,DEC);lcd.write(0xdf);
lcd.setCursor(11, 0);if( cbut == HIGH){ lcd.print(" C");}
lcd.setCursor(13,0); if( zbut == HIGH){ lcd.print(" Z");}
lcd.setCursor(0, 1); if (ledVal1 == HIGH){lcd.print("Z Lights");}
lcd.setCursor(9, 1); if (ledVal2 == HIGH){lcd.print("Z Armed!");
}//End lcd out */
}
loop_cnt++;
delay(1); // delay came with WiiChuck script
}
{ //Start of X-Y mixing attempt
if( loop_cnt2 > 100 ){ //loop to prevent lag
loop_cnt2 = 0;
xjoy = map(xraw, 26, 225, 1, 180); // ranges from aprox 27 - 224 Left < c=127-129 >right
yjoy = map(yraw, 28, 227, 1, 180); //ranges from aprox 29 - 226 Down< c=129-132 >up
int rthrust = 0;
int lthrust = 0;
if(yraw > 143)//forward
{
rthrust = yjoy; // val 90-180
lthrust = yjoy;
if(xraw < 123)//left 90-1
{
lthrust = yjoy - map(xjoy, 90, 1, 20, 100);// math?
rthrust = yjoy;
}
else if(xraw > 133)// right 90-180
{
rthrust = yjoy - map(xjoy, 90, 180, 20, 100);//change to -map 1-90 or 20-100 for smaller turn increment
lthrust = yjoy;
}
}
else if(yraw < 117)//reverse
{
rthrust = yjoy;
lthrust = yjoy; //val=1-90
if(xraw < 123)//left (fwd) 90-1
{
lthrust = yjoy + map(xjoy, 90, 1, 20, 100);
rthrust = yjoy;// reverse left
}
else if(xraw > 133)// right (fwd) 90-180
{
rthrust = yjoy + map(xjoy, 90, 180, 20, 100);// math?
lthrust = yjoy;
}
}
else //in Y deadband = zero turn
{
if(xraw < 123)//left 1-90
{
lthrust = map(xjoy, 90, 1, 90, 180);// math?
rthrust = xjoy; // full left & rev right
}
else if(xraw > 133)// right 90-180
{
lthrust = map(xjoy, 90, 180, 90, 1);// math?
rthrust = xjoy;// fwd right & reverse left
}
/* else
{
rthrust = 0;
lthrust = 0;
}*/
}
Serial.print("<");
Serial.print(lthrust, DEC);
Serial.print(":");
Serial.print(rthrust, DEC);
/*Serial.print(":"); // commented out for debug!!
Serial.print(accx, DEC);
Serial.print(":");
Serial.print(accy, DEC);
Serial.print(":");
Serial.print(ledVal0, DEC); //z press
Serial.print(":");
Serial.print(ledVal1, DEC); //z hold
Serial.print(":");
Serial.print(ledVal2, DEC); //c press
Serial.print(":");
Serial.print(ledVal3, DEC); //c hold // commented out for debug!!
*/ Serial.println(">");
}
loop_cnt2++;
}
}
}
{
// Test for Cbut pressed and store the down time
if (cbut == HIGH && cbutLast == LOW && (millis() - btnUpTime) > long(debounce)) {
btnDnTime = millis();
}
cbutVal = cbut;// Read the state of the button
// Test for button release and store the up time
if (cbutVal == LOW && cbutLast == HIGH && (millis() - btnDnTime) > long(debounce)) {
if (ignoreUp == false) eventc1();
else ignoreUp = false;
btnUpTime = millis();
}
// Test for button held down for longer than the hold time
if (cbutVal == HIGH && (millis() - btnDnTime) > long(holdTime)) {
eventc2();
ignoreUp = true;
btnDnTime = millis();
}
cbutLast = cbutVal;//End of push hold C
// Test for Zbut pressed and store the down time
if (zbut == HIGH && zbutLast == LOW && (millis() - btnUpTime) > long(debounce)) {
btnDnTime = millis();
}
zbutVal = zbut;// Read the state of the button
// Test for button release and store the up time
if (zbutVal == LOW && zbutLast == HIGH && (millis() - btnDnTime) > long(debounce)) {
if (ignoreUp == false) eventz1();
else ignoreUp = false;
btnUpTime = millis();
}
// Test for button held down for longer than the hold time
if (zbutVal == HIGH && (millis() - btnDnTime) > long(holdTime)) {
eventz2();
ignoreUp = true;
btnDnTime = millis();
}
zbutLast = zbutVal;//End of push hold Z
}
}
// Events to trigger by click and press+hold Zbut
void eventz1(){ //Z press
ledVal0 = !ledVal0;
digitalWrite(ledPin0, ledVal0);
}
void eventz2(){ //Z hold
ledVal1 = !ledVal1;
digitalWrite(ledPin1, ledVal1);
}
void eventc1(){ //C press
ledVal2 = !ledVal2;
digitalWrite(ledPin2, ledVal2);
}
void eventc2(){ //C Hold
ledVal3 = !ledVal3;
digitalWrite(ledPin3, ledVal3);
}
RX Code (same some things commented out for trouble shooting or to try other things)
#include <Servo.h>
#include <LiquidCrystal.h>
#define BUFFER_MAX 10
int rthrust = 0;
int lthrust = 0;
int rthrust2;
//int ledPin2 = 8;
int ledPin3 = 13;
int loop_cnt0 = 0;
int loop_cnt1 = 0;
//int loop_cnt2 = 0;
Servo servoL;
Servo servoR;
//byte accx,accy,cbutp,cbuth,zbutp,zbuth; //Commented out for debug
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // LCD pins
byte counter = 0;
char bufferBUFFER_MAX];
void setup() {
// pinMode(13, OUTPUT);//led //Commented out, I dont know why
Serial.begin(57600);
servoL.attach(10);//left
servoR.attach(9);//right
}
void loop() {
{
if( loop_cnt0 > 10 ){ //loop to prevent lag
loop_cnt0Â =Â 0;
if (Serial.available()) {
char c = ((char)Serial.read());
if(c == '<')
{
digitalWrite(13,HIGH);
counter = 0;
memset( buffer, 0, BUFFER_MAX);
}
else if(c == '>')
{
digitalWrite(13,LOW);
processMessage();
}
else
{
if(counter == BUFFER_MAX)
counter = 0;
buffercounter++]Â =Â c;
}
}
}
loop_cnt0++;
delay(1);
}
if( loop_cnt1 > 50 ){ //loop to prevent lag
loop_cnt1Â =Â 0;
if (lthrust > 0) {
servoL.write(lthrust);Â }
else if(lthrust == 0){
servoL.write(90);Â }
if (rthrust > 0) {
rthrust2 = map(rthrust, 1, 180, 180, 1);
servoR.write(rthrust2);Â }
else if(rthrust == 0){
servoR.write(90);Â }
lcd.begin(16, 2);
lcd.setCursor(0, 0);lcd.print((byte)lthrust,DEC);lcd.write(0xdf);
lcd.setCursor(4, 0);lcd.print((byte)rthrust,DEC);lcd.write(0xdf);
//lcd.setCursor(9, 0);lcd.print((byte)loop_cnt0,DEC);
//lcd.setCursor(13,0);lcd.print((byte)counter,DEC);
//lcd.setCursor(0, 1);lcd.print(buffer);
//lcd.setCursor(9, 1);lcd.print(buffer);
}//End lcd out
loop_cnt1++;
delay(1);
}
void processMessage()//8 token- How does this section work, Is there something better?
{
//Â Serial.println(buffer);
// Serial.println(counter, DEC);
// if( loop_cnt2 > 20 ){ //loop to prevent lag
//loop_cnt2Â =Â 0;
int count = 0;
int tokenCount = 0;
char tokenValue3];
int tokenValues3];
for(int i=0; i<=counter; i++)
{
if(bufferi] == ':' || i == counter)
{
tokenValuestokenCount++]Â =Â atoi(tokenValue);
count = 0;
memset(tokenValue, 0, 3);
}
else
{
tokenValuecount++]Â =Â bufferi];
}
}
lthrust = tokenValues0];
rthrust = tokenValues1];
//accx = tokenValues[2]; //Commented out for debug
//accy = tokenValues[3];
//zbutp = tokenValues[4];
//zbuth = tokenValues[5];
//cbutp = tokenValues[6];
//cbuth = tokenValues[7];Â
//}
//loop_cnt2++;
delay(1);
}Â
WiichuckFIOv2.pde (7.04 KB)
ReceiverUno.pde (2.97 KB)