Hola, ya casi he terminado mi código, pero me resulta difícil conseguir que funcione el último bit.
Hasta ahora tengo dos códigos, uno para tomar el envío de datos nunchuck de wii a través de la radio y otro para recogerlo y transferirlo a dos servos.
Mi pregunta es: ¿alguien sabe cómo editar el código de modo que cuando se presiona uno de los botones (es decir, el botón Z en el wii nunchuck) los servos pueden moverse pero cuando no se presiona el botón son de ayuda? Posición, como un dedo muerto.
Aquí está el receptor:
// 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, '// 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, '// 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, '// 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, '%pre%', sizeof(bufferP));
memset(numP, '%pre%', sizeof(numP));
memset(bufferR, '%pre%', sizeof(bufferR));
memset(numR, '%pre%', 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);
}
', 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
}
', sizeof(bufferP));
memset(numP, '%pre%', sizeof(numP));
memset(bufferR, '%pre%', sizeof(bufferR));
memset(numR, '%pre%', 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);
}
', 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
}
Aquí está la transmisión:
%pre%