Go Down

Topic: verifica modifica sketch (Read 74 times) previous topic - next topic

mikiti

ciao a tutti.

modificare il seguente sketch
Code: [Select]
// Include the BMC_SBUS library
#include "BMC_SBUS.h"

//Declare BMC_SBUS Object
BMC_SBUS mySBUS;

//Include Wiichuck Library
#include <WiiChuck.h>

//Declare Wiichuck Object
Nunchuck nunchuck;
// Sbus delay value
const int sbusWAIT        = 7;      //frame timing delay in msecs

// Declare sbus control channels
int panChannel = 1;
int tiltChannel = 2;
int rollChannel = 4;

void setup() {

  // Start Wiichuck Object
  nunchuck.begin();
  // Start BMC_SBUS object
  mySBUS.begin();

}
void loop() {
    nunchuck.readData();    // Read inputs from wiichuck

    // Get wiichuck data
    int Yvalue = nunchuck.getJoyY();
    int Xvalue = nunchuck.getJoyX();
    int Zbutton = nunchuck.getButtonZ();

    //Map wiichuck values which go from 0-255 to Sbus values 0-2047
    // For no move send 1023
    int sendValueY = map(Yvalue,0,255,0,2047);
    int sendValueX = map(Xvalue,0,255,0,2047);

    // Set sbus tilt
    mySBUS.Servo(tiltChannel,sendValueY);

    // Check if trigger(button Z) is pressed, if so send the X value to roll channel
    if (Zbutton == 1) {
       mySBUS.Servo(rollChannel,sendValueX);         
    } else {
       mySBUS.Servo(panChannel,sendValueX);
    }
    // Update SBUS object and send data
    mySBUS.Update();
    mySBUS.Send();
    // Delay for SBUS
    delay(sbusWAIT);

}


Vorrei poter inviare gli stessi comandi di output non con il dispositivo di input "Wiichuck" (che è un joystick proprietario) ma con un normale joystick analogico a 2 assi X,Y (più il pulsante integrato Z).

Ho modificato il codice in questo modo:
Code: [Select]
// Include the BMC_SBUS library
#include "BMC_SBUS.h"

//Declare BMC_SBUS Object
BMC_SBUS mySBUS;

/*
//Include Wiichuck Library
#include <WiiChuck.h>

//Declare Wiichuck Object
Nunchuck nunchuck;
*/

// Sbus delay value
const int sbusWAIT        = 7;      //frame timing delay in msecs

//********************************************** modifica **********************************************
#define X_pin A0
#define Y_pin A1
#define SW_pin 8
//********************************************** fine modifica **********************************************

// Declare sbus control channels
int panChannel = 1;
int tiltChannel = 2;
int rollChannel = 4;

void setup() {
pinMode(SW_pin, INPUT_PULLUP);
/*
  // Start Wiichuck Object
  nunchuck.begin();
*/

  // Start BMC_SBUS object
  mySBUS.begin();

}
void loop() {

//    nunchuck.readData();    // Read inputs from wiichuck

/*
    // Get wiichuck data
    int Yvalue = nunchuck.getJoyY();
    int Xvalue = nunchuck.getJoyX();
    int Zbutton = nunchuck.getButtonZ();
*/

//********************************************** modifica **********************************************
int Xvalue = analogRead(X_pin);
int Yvalue = analogRead(Y_pin);
int Zbutton = !digitalRead(Zbutton);  //premendo il pulsante il valore è HIGH
//********************************************** fine modifica **********************************************

/*
    //Map wiichuck values which go from 0-255 to Sbus values 0-2047
    // For no move send 1023
    int sendValueY = map(Yvalue,0,255,0,2047);
    int sendValueX = map(Xvalue,0,255,0,2047);
*/

//********************************************** modifica **********************************************
    int sendValueY = map(Yvalue,0,1023,0,2047);
    int sendValueX = map(Xvalue,0,1023,0,2047);
//********************************************** fine modifica **********************************************

   
    // Set sbus tilt
    mySBUS.Servo(tiltChannel,sendValueY);

    // Check if trigger(button Z) is pressed, if so send the X value to roll channel
    if (Zbutton == 1) {
       mySBUS.Servo(rollChannel,sendValueX);         
    } else {
       mySBUS.Servo(panChannel,sendValueX);
    }
    // Update SBUS object and send data
    mySBUS.Update();
    mySBUS.Send();
    // Delay for SBUS
    delay(sbusWAIT);

}



Dite che può funzionare?

Go Up