Skip to content

Instantly share code, notes, and snippets.

@t413
Created August 7, 2014 02:31
Show Gist options
  • Save t413/36974b8490b5e53683e5 to your computer and use it in GitHub Desktop.
Save t413/36974b8490b5e53683e5 to your computer and use it in GitHub Desktop.
Arduino XBee/Wii Receiver
/* Author: Tim O'Brien
* Site: t413.com
* Lisence: Creative Commons Attribution-Noncommercial-Share Alike 3.
* this file updated: 12/23/09
*
* Let me know if you're using or adapting this,
* Leave a comment, send an email, I'd like to hear about it.
*/
#define M1 1
#define M2 0
const int m1pwm = 6, m1c1 = 3, m1c2 = 4;
const int m2pwm = 11, m2c1 = 12, m2c2 = 13;
int dataArray[7];
void setup() {
Serial.begin(115200);
pinMode(m1pwm, OUTPUT);
pinMode(m1c1, OUTPUT);
pinMode(m1c2, OUTPUT);
pinMode(m2pwm, OUTPUT);
pinMode(m2c1, OUTPUT);
pinMode(m2c2, OUTPUT);
}
void loop(){
if (Serial.available()>8){
getcommand();
//brake
int inx,iny;
if (dataArray[1]){ //use accelorometer control
inx = dataArray[4];
iny = dataArray[5];
} //m_stop(); -- stop
else { //use joystick
inx = dataArray[2];
iny = dataArray[3];
}
int x,y;
if (inx > 140)
x = map( (inx-140) ,0,255-140,0,255);
else if (inx < 120)
x = map( (120-inx) ,0,120,0,-255);
else x = 0;
// 255/2 = 127.5 129 -- 126
if (iny > 140)
y = map( (iny-140) ,0,255-140,0,255);
else if (iny <= 120)
y = map( (120-iny),0,120,0,-255);
else y = 0;
motor(M1,y-x);
motor(M2,y+x);
}
}
void m_stop(){
digitalWrite(m1c1, LOW);
digitalWrite(m1c2, LOW);
digitalWrite(m2c1, LOW);
digitalWrite(m2c2, LOW);
}
void motor(int which_motor,int this_speed){
//limiting
if (this_speed< -255)
this_speed = -255;
if (this_speed > 255)
this_speed = 255;
if (this_speed > 0){
if (which_motor == M1){
digitalWrite(m1c1, HIGH);
digitalWrite(m1c2, LOW);
analogWrite(m1pwm, this_speed);
}
else {
digitalWrite(m2c1, HIGH);
digitalWrite(m2c2, LOW);
analogWrite(m2pwm, this_speed);
}
}
else { //this_speed < 0
if (which_motor == M1){
digitalWrite(m1c1, LOW);
digitalWrite(m1c2, HIGH);
analogWrite(m1pwm, -this_speed);
}
else {
digitalWrite(m2c1, LOW);
digitalWrite(m2c2, HIGH);
analogWrite(m2pwm, -this_speed);
}
}
}
void getcommand(){
char c = Serial.read();
char c2 = Serial.read();
if ((c == 'd')&&(c2 == '=')){
dataArray[0] = Serial.read();
dataArray[1] = Serial.read();
dataArray[2] = Serial.read();
dataArray[3] = Serial.read();
dataArray[4] = Serial.read();
dataArray[5] = Serial.read();
Serial.read(); //line return
}
else {
while (Serial.read() != '\n'){
}
}
}
/* Author: Tim O'Brien
* Site: t413.com
* Lisence: Creative Commons Attribution-Noncommercial-Share Alike 3.
* this file updated: 12/23/09
*
* Let me know if you're using or adapting this,
* Leave a comment, send an email, I'd like to hear about it.
*/
#include <Wire.h>
uint8_t nunchuck_buf[6]; // array to store nunchuck data,
int i,loop_c=0,reading;
int ledPin = 13;
void setup(){
Serial.begin(115200);
nunchuck_init();
pinMode(ledPin, OUTPUT);
//print inital battery charge
Serial.print("b=");
Serial.print(update_batt_status());
Serial.println("%");
}
void loop() {
nunchuck_get_data();
if (((nunchuck_buf[5] >> 0) & 1) ? 0 : 1){
digitalWrite(ledPin, HIGH);
send_packet();
}
else digitalWrite(ledPin, LOW);
// Print battery state
if ( Serial.available() > 0) {
char inchar = Serial.read();
if ( inchar == 'b') {
Serial.print("\nb=");
Serial.print(update_batt_status());
Serial.println("%");
}
else // Print battery voltage
if ( inchar == 'v') {
Serial.print("\nv=");
Serial.print( (float)analogRead(0)/155 );
Serial.println("");
}
else // poll sensor data.
if ( inchar == 'd') {
send_packet();
}
}
if (loop_c++ > 1000*10){
update_batt_status();
loop_c = 0;
}
delay(1);
}
/* --- my functions --- */
int update_batt_status(){
//float voltage;
int percentage;
reading = analogRead(0);
//voltage = (float)reading/155 ;
percentage = map(reading,465,651,0,100);
if ((percentage<5)&&(percentage>-50)){
Serial.print("LOW BATTERY! (");
Serial.print(percentage);
Serial.print("%, ");
Serial.print(reading);
Serial.print("). Looping.\n");
while(1){
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
return percentage;
}
void send_packet(){
Serial.print("d=");
Serial.print( ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1,BYTE);
Serial.print( ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1,BYTE);
Serial.print( mapLimit(nunchuck_buf[0], 29, 222),BYTE);
Serial.print( mapLimit(nunchuck_buf[1], 38, 226),BYTE);
Serial.print( mapLimit(nunchuck_buf[2], 77, 178),BYTE);
Serial.print( mapLimit(nunchuck_buf[3], 72, 173),BYTE);
Serial.println("");
}
int mapLimit(int x,int li,int ui){
int out = map(x, li, ui, 0,255 );
if (out<1)
out = 0;
if (out>254)
out = 255;
return out;
}
/* --- wii functions --- */
/* Code mostly from Robot Overlord,
* http://letsmakerobots.com/node/5684
*/
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init() {
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52);// transmit to device 0x52
Wire.send(0x40);// sends memory address
Wire.send(0x00);// sends sent a zero.
Wire.endTransmission();// stop transmitting
}
// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request() {
Wire.beginTransmission(0x52);// transmit to device 0x52
Wire.send(0x00);// sends one byte
Wire.endTransmission();// stop transmitting
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x) {
x = (x ^ 0x17) + 0x17;
return x;
}
// Receive data back from the nunchuck,
// returns 1 on successful read. returns 0 on failure
int nunchuck_get_data() {
int cnt=0;
Wire.requestFrom (0x52, 6);// request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.receive());
cnt++;
}
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment