3
\$\begingroup\$

hey i have just about finished my code but im finding it dificult to get the last bit working.

So far i have two codes one for taking wii nunchuck data sending through radio then another for picking that up and transfering to two servos.

My question is does anyone know how to edit the code so when one of the buttons (ie the Z button on the wii nunchuck) is pushed the servos are able to move but when the button is not being pushed they are help in position, like a deadmans finger.

Here is the reciever:

// Receiver Code

#include <ServoTimer2.h>

ServoTimer2 servosteer;  // create servo object to control a servo 
ServoTimer2 servospeed;  // create servo object to control a servo 
//MEGA pin 23 receive pin - displays characters sent by RF

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
servosteer.attach(7);  // attaches the servo on pin 25 to the servo object 
servospeed.attach(8);  // attaches the servo on pin 27 to the servo object 
 // lcd.init();
    Serial.begin(9600); // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(4000);  // Bits per sec
    vw_set_rx_pin(5);
    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
     uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      //invalid message length must be S/B/F/L/R + number (max 3 digits)
      if (buflen < 3 || buflen > 5)
        return;
 digitalWrite(13, true); // Flash a light to show transmitting

      char val[buflen]; //Same as buf, last char will be used for null terminator
      memset(val, '\0', sizeof(val));

      //Copy value from string i.e. 213 from R213 into separate string
      strncpy(val, (char *)buf + 1, buflen - 1);

      //convert string containing value to integer e.g. "213" to 213.
      int VAL = atoi ( val );

      switch (buf[0]) {
                case 'X': //Deadmans finger stop all
                    Serial.print("Deadmans finger");
                    servospeed.write(1500);

                    break;
        case 'P':
            Serial.print("Pitch ");
            Serial.println(VAL);
                    servospeed.write(544+VAL*10);

            break;
        case 'R':
            Serial.print("Roll ");
            Serial.println(VAL);
                    servosteer.write(544+VAL*10);

            break;
        default:
            break;
      }
    }
     digitalWrite(13, false); // Flash a light to show transmitting
} 

Here is the transmit:

// Transmitter Code
int speedo=96;
int steero=90; //limit 70-110
int ii=0;
int jj=0;
int roll=0;
int pitch=0;

#include "Wire.h"
#include "WiiChuck.h"
WiiChuck chuck = WiiChuck();
int zPress = 0;
int XPress =-130;
int YPress =-130;

#include <VirtualWire.h> // library for RF RX/TX
#undef int
#undef abs
#undef double
#undef float
#undef round
#undef round

char charnum[10];

int bz=0;
void setup() {
  Serial.begin(115200);
  chuck.begin();
  chuck.update();
  chuck.calibrateJoy();
   // Initialise the IO and ISR
  vw_set_tx_pin(6);
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(4000);    // Bits per sec
}

void loop() {
 chuck.update();

char bufferP[5];char numP[5];
char bufferR[5];char numR[5];
  memset(bufferP, '\0', sizeof(bufferP));
  memset(numP, '\0', sizeof(numP));
  memset(bufferR, '\0', sizeof(bufferR));
  memset(numR, '\0', sizeof(numR));
  roll= (int)chuck.readRoll();
  pitch = (int)chuck.readPitch();

   digitalWrite(13, true); // Flash a light to show transmitting
   XPress=chuck.readJoyX();
   YPress=chuck.readJoyY();

 strcat(bufferR,"R");
  //if (chuck.zPressed() == 1) {strcat(bufferR,"R");} else {strcat(bufferR,"X");}
  ii=byte(roll-160); 
  if (ii<70) { ii=70;} if (ii>110) { ii=110;}
   itoa(ii,numR,10);
    strcat(bufferR,numR);
  Serial.println(bufferR);
  vw_send((uint8_t *)bufferR, strlen(bufferR));
  vw_wait_tx(); // Wait until the whole message is gone
  delay(20);

  //strcat(bufferP,"P");
  if ((XPress>-127) or (XPress<-133)or (YPress>-127) or (YPress<-133)) { strcat(bufferP,"P");} else {strcat(bufferP,"X");}

  jj=byte(pitch); 
   itoa(jj,numP,10);
    strcat(bufferP,numP);
  Serial.println(bufferP);
  vw_send((uint8_t *)bufferP, strlen(bufferP));
  vw_wait_tx(); // Wait until the whole message is gone
  delay(20);
  digitalWrite(13, false);
 }
\$\endgroup\$
1
  • \$\begingroup\$ i think a better method than pasting your code directly in your post (when you have a lot of it) is to put it somewhere else and provide a link to it. I recommend pastebin, which also provides line numbers so people can refer to them in responses. \$\endgroup\$
    – vicatcu
    Jul 27, 2010 at 15:55

2 Answers 2

2
\$\begingroup\$

In the transmitter put in an IF "button address" True then chuck.update()

\$\endgroup\$
1
  • \$\begingroup\$ agreed, just prevent the transmitter from sending if the button is not pressed. \$\endgroup\$
    – vicatcu
    Jul 27, 2010 at 15:53
0
\$\begingroup\$

In conjunction with Denominators answer, depending on the data your'e sending across, you could do the same on the other side, where it reads the data sent to it, and if that variable (or rather bit) isn't set/true, then don't move the motors.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.