Is this the place to post code? Who cares, here it is, respectfully stolen of the net and tweaked to suit my own fiendish desires, a lot of the old stuff is in there from the original code and lots of test code slashed out, man alive i cut corners! My coding is nearly as messy as my workshop :) It should compile on an arduino.
Here is the Transmitter code;
#include <Servo.h>
#include <JeeLib.h> //from jeelabs.org
#include <Wire.h>
//#include "nunchuck_funcs.h"
#define myNodeID 10 //node ID of tx (range 0-30)
#define network 210 //network group (can be in the range 1-250).
#define freq RF12_868MHZ //Freq of RF12B can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. Match freq to module
byte byteRead;
typedef struct // create structure - a neat way of packaging data for RF comms
{
int jx, jy, acy, acx, zButton, cButton; // data from the controller. these have to match.
}
PayloadTX;
PayloadTX wiichuck;
byte accx,accy,zbut,cbut,joyx,joyy; // names of the nunchuck data
// constants won't change. They're used here to
// set pin numbers:
const int right = 4; // the number of the pushbutton pin
const int backward = 5;
const int forward = 6;
const int left = 7;
int p = 0;
int t = 0;
int r = 0;
int potpin0 = 5;
int potpin1 = 4;
int potpin2 = 3;
int val0;
int val1;
int val3;
//const int ledPin = 13; // the number of the LED pin
// variables will change:
//int buttonState = 0; // variable for reading the pushbutton status
int Right = 0;
int Backward = 0;
int Forward = 0;
int Left = 0;
//int cbut = 0;
void setup() {
// initialize the LED pin as an output:
// pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
rf12_initialize(myNodeID,freq,network);
Serial.begin(9600);
pinMode(right, INPUT);
pinMode(backward, INPUT);
pinMode(forward, INPUT);
pinMode(left, INPUT);
// accx = 90;
// accy = 90;
// zbut = 90;
//Stuff here for reading the three pots.
}
void loop(){
Serial.print(" 1pan ");
Serial.print(accx);
// Serial.print("\n");
Serial.print(" 1tilt ");
Serial.print(accy);
// Serial.print("\n");
Serial.print(" 1raise ");
Serial.print(zbut);
Serial.print("\n");
p = analogRead(potpin0); // reads the value of the potentiometer (value between 0 and 1023)
accx = map(p, 0, 1023, 0, 179); //
t = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
accy = map(t, 0, 1023, 0, 179); //
r = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
zbut = map(r, 0, 1023, 0, 179); //
// read the state of the pushbutton value:
Right = digitalRead(right);
Backward = digitalRead(backward);
Forward = digitalRead(forward);
Left = digitalRead(left);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
//LEFT
//Serial.print(cbut);
//Serial.print("cbut val\n");
Serial.print(" pan ");
Serial.print(accx);
// Serial.print("\n");
Serial.print(" tilt ");
Serial.print(accy);
// Serial.print("\n");
Serial.print(" raise ");
Serial.print(zbut);
Serial.print("\n");
if (Left == HIGH) {
// turn LED on:
Serial.print("left\n");
cbut = 4;
//Serial.print(cbut);
//digitalWrite(ledPin, HIGH);
}
else if (Forward == HIGH) {
// turn LED on:
Serial.print("forward\n");
cbut = 1;
//Serial.print(cbut);
//digitalWrite(ledPin, HIGH);
}
//BACKWARD
else if (Backward == HIGH) {
// turn LED on:
Serial.print("Back\n");
cbut = 2;
//Serial.print(cbut);
//digitalWrite(ledPin, HIGH);
}
//BACKWARD
//RIGHT
else if (Right == HIGH) {
// turn LED on:
Serial.print("right\n");
cbut = 3;
//Serial.print(cbut);
//digitalWrite(ledPin, HIGH);
}
else {
cbut=0;
// turn LED off:
//Serial.print("right off\n");
//digitalWrite(ledPin, LOW);
}
//RIGHT
//cbut = 1;// nunchuck_cbutton();
//accx = 71; //nunchuck_accelx(); // ranges from approx 70 - 182
//accy = 65;// nunchuck_accely(); // ranges from approx 65 - 173
//zbut = 2;//nunchuck_zbutton();
joyx = 3;//nunchuck_joyx();
joyy = 4;//nunchuck_joyy();
wiichuck.jx = joyx; // read wii angle and store value
//wiichuck.jx = map(wiichuck.jx, 30, 220, 0, 179); // map the joystick data for servo use
wiichuck.jy = joyy;
//wiichuck.jy = map(wiichuck.jy, 30, 220, 0, 179);
wiichuck.acy = accy;
wiichuck.acx = accx;
//wiichuck.acx = map(wiichuck.acx, 65, 173, 135, 45); // mapp the accelerometer data to drive the car
wiichuck.cButton = cbut;
wiichuck.zButton = zbut;
int i = 0;
while (!rf12_canSend() && i<10)
{
rf12_recvDone();
i++;
}
rf12_sendStart(0, &wiichuck, sizeof wiichuck);
//Servo control
//Pan number = reading from pot and so on.
//transmit pan number.
delay(20);
}
Here is the code for the receiver, pumping out commands to two drive motors and three servos.
#include <Servo.h>
//credit for RFM12B code from
//Glyn Hudson openenergymonitor.org GNU GPL V3 12/4/12
//Credit to JCW from Jeelabs.org for RFM12
#include <JeeLib.h>
#define myNodeID 30 //node ID of Rx (range 0-30)
#define network 210 //network group (can be in the range 1-250).
#define freq RF12_868MHZ //Freq of RF12B can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. Match freq to module
typedef struct
{
int jx, jy, acy, acx, zButton, cButton; // this is the data from the controller. they must match.
}
PayloadTX;
PayloadTX wiichuck; // the payload, named wiichuck
Servo CamRaise;
Servo CamTilt;
Servo CamPan;
const int wiichuck_NodeID=10; //emonTx node
//Servo steerServo;
//int servopin1=9;
int steer = 90;
int joyx; // Joystick X axis
int joyy; // joystick Y axis
int accy; // Accelerometer Y axis
int acyf; // Forward
int acyr; // Reverse
int accx; // Accelerometer X axis
int M2; // Accelerometer X axis
int M1; // Brake function
int whatever; // i never used this for anything, but it's here
int motor1L=6; // output to motor driver
int motor1R=7; // output to motor driver
int motor2L=8; // output to motor driver
int motor2R=9; // output to motor driver
int counter=0; // counter for brake function
// maybe......
int pan;
int tilt;
int raise;
int one;
void setup() {
rf12_initialize(myNodeID,freq,network); //Initialize RFM12 with settings defined above
Serial.begin(9600);
// Serial.println("RF12B demo Receiver - Simple demo"); //pointless memory waste
// Serial.print("Node: "); // but i keep it all here
// Serial.print(myNodeID); // just in case
// Serial.print(" Freq: "); // something screws up
// if (freq == RF12_433MHZ) Serial.print("433Mhz");
// Serial.print(" Network: ");
// Serial.println(network);
//steerServo.attach(servopin1);
//steerServo.write(steer);
CamRaise.attach(3);
CamTilt.attach(4);
CamPan.attach(5);
pinMode(motor1L, OUTPUT); // drive forward output pin 5
pinMode(motor1R, OUTPUT); // drive forward output pin 6
pinMode(motor2L, OUTPUT); // drive reverse output pin 7
pinMode(motor2R, OUTPUT); // drive forward output pin 5
M1 = 0;
M2 = 0;
digitalWrite(motor1L, LOW);
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, LOW);
digitalWrite(motor2R, LOW);
}
void loop() {
if (rf12_recvDone()){
if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0) // i have no idea what this does but i guess its important
{
int node_id = (rf12_hdr & 0x1F); //extract nodeID from payload
if (node_id == wiichuck_NodeID) { //check data is coming from node with the corrct ID
wiichuck=*(PayloadTX*) rf12_data; // Extract the data from the payload
//joyx = wiichuck.jx;
//joyy = wiichuck.jy;
//accy = wiichuck.acy;
// pan = wiichuck.jx;
//tilt = wiichuck.jy;
//raise = wiichuck.acy;
//accx = wiichuck.acx;
M2 = wiichuck.acx;
M1 = wiichuck.cButton;
int jx, jy, acy, acx, zButton, cButton; // this is the data from the controller. they must match.
one = wiichuck.jx;
whatever = wiichuck.jy;
tilt = wiichuck.acy;
pan = wiichuck.acx;
raise = wiichuck.zButton;
//set motor outputs to zero
//Serial.print("C button ");
//Serial.print(M1);
// Serial.print(" jx incoming ");
// Serial.print(pan);
// Serial.print("..... ");
//Serial.print(" jy incoming ");
//Serial.print(tilt);
//Serial.print("..... ");
Serial.print(" tilt acy ");
Serial.print(tilt);
Serial.print("..... ");
Serial.print(" pan acx ");
Serial.print(pan);
Serial.print("..... ");
Serial.print(" raise zbutton ");
Serial.print(raise);
Serial.print(".....\n ");
CamRaise.write(raise); // sets the servo position according to the scaled value
//delay(15);
CamPan.write(pan); // sets the servo position according to the scaled value
//delay(15);
CamTilt.write(tilt); // sets the servo position according to the scaled value
delay(15);
if(M1 == 1) // Forward
{
Serial.print(" Forward \n ");
digitalWrite(motor1L, HIGH);
digitalWrite(motor1R, HIGH);
digitalWrite(motor2L, HIGH);
digitalWrite(motor2R, HIGH);
}
else if(M1 == 2) // Backward
{
Serial.print(" Backward \n ");
digitalWrite(motor1L, HIGH);
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, HIGH);
digitalWrite(motor2R, LOW);
}
else if(M1 == 3) // rotate CW
{
Serial.print(" CW \n ");
digitalWrite(motor1L, HIGH);//m1 forward
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, HIGH);//m2 backward
digitalWrite(motor2R, HIGH);
}
else if(M1 == 4) // Rotate ccw
{
Serial.print(" CCW \n ");
digitalWrite(motor1L, HIGH);//m1 backward
digitalWrite(motor1R, HIGH);
digitalWrite(motor2L, HIGH);//m2 forward
digitalWrite(motor2R, LOW);
}
else if(M1 == 5) // m1 forward
{
Serial.print(" M1Forward \n ");
digitalWrite(motor1L, LOW);
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, LOW);
digitalWrite(motor2R, LOW);
}
else if(M1 == 6) // m1 backward
{
Serial.print(" M1Backward \n ");
digitalWrite(motor1L, LOW);
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, LOW);
digitalWrite(motor2R, LOW);
}
else // stop
{
//Serial.print(" stop \n ");
digitalWrite(motor1L, LOW);
digitalWrite(motor1R, LOW);
digitalWrite(motor2L, LOW);
digitalWrite(motor2R, LOW);
}
if(counter == 0 || counter == 2) // turn on the brake
{
//digitalWrite(motorpos, HIGH);
//digitalWrite(motorneg, HIGH);
//steerServo.write(90); //centers steering
}
else
{
acyf = map(accy, 110, 177, 30, 255); // remaps the accelerometer readings for driving forward
acyr = map(accy, 100, 70, 30, 200); // reverse
if(accy > 110 && accy <182) // Drive forward!
{
//forward
//digitalWrite(motorpos, HIGH);
//digitalWrite(motorneg, LOW);
analogWrite(3, acyf); // speed
}
if(accy < 100) // Reverse!
{
//backward
//digitalWrite(motorpos, LOW);
//digitalWrite(motorneg, HIGH);
analogWrite(3, acyr); //speed
}
// if(accy > 100 && accy < 110 || brake == 1) //if the accelerometer is inbetween the forward and reverse, dont do anything.
// {
// //digitalWrite(motorpos, LOW);
// //digitalWrite(motorneg, LOW);
// steerServo.write(90); //centers steering
// }
// else
// {
// steerServo.write(accx); //steering control
// }
}
if(counter==2) //resets counter
{
counter=0;
}
}
}
}
// used for testing
//Serial.print("JX Axis: "); //not necessary
//Serial.println(wiichuck.jx);
//Serial.print("JY Axis: "); //not necessary
//Serial.println(wiichuck.jy);
//Serial.print("YF Speed: "); //not necessary
//Serial.println(acyf);
//Serial.print("YR Speed: "); //not necessary
//Serial.println(acyr);
//Serial.print("X Axis: "); //not necessary
//Serial.println(wiichuck.acx);
//Serial.print("C Button: "); //not necessary
//Serial.println(wiichuck.cButton);
//Serial.print("Z Button: "); //not necessary
//Serial.println(wiichuck.zButton);
}