/** * Copyright (c) 2009 Andrew Rapp. All rights reserved. * * This file is part of XBee-Arduino. * * XBee-Arduino is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * XBee-Arduino is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with XBee-Arduino. If not, see . Analog inputs from Joystick, serial output, digital output Detect change voltage from Joystick and convert to : Up-Down --> VRy1 : Left-Right --> VRx1 : Wide-Tele --> VRx2 created april 2015 Pierre Marie GAYRAL F5XG This example code is in the public domain. */ // ****************** // * Series2_Tx.ino * // ****************** #include // Joystick pins used : #define VRx_1Pin A0 // Analog input pin VRx1 #define VRy_1Pin A1 // Analog input pin VRy1 #define VRx_2Pin A6 // Analog input pin VRx2 #define VRy_2Pin A7 // Analog input pin VRy2 // Joystick 1 int VRx_1Value = 513; // value read from the pot Up-Down int Old_VRx_1Value = 513; // previous value read from the pot Up-Down int VRy_1Value = 513; // value read from the pot Left-Right int Old_VRy_1Value = 513; // previous value read from the pot Left-Right int sens_Tilt = 0; int sens_Pan = 0; int sens_Left = 0; int sens_Right = 0; int UP = 0; int DOWN = 0; int LEFT = 0; int RIGHT = 0; // Joystick 2 int VRx_2Value = 513; // value read from the pot Wide-Tele int Old_VRx_2Value = 513; // previous value read from the pot Wide-Tele int VRy_2Value = 513; // value read from the pot Speed int Old_VRy_2Value = 513; // previous value read from the pot Speed int sens_Zoom = 0; int sens_Tele = 0; int TELE = 0; int WIDE = 0; int RES_1 = 0; int RES_2 = 0; // // ____________________ LEDs de contrôle ______________ // int statusLed = 13; int errorLed = 9; int successLed = 8; // numéros des boutons int pin5 = 0; int Bton = 0; int Bton_Tilt = 0; int Bton_Pan = 0; int Tele; /* Explication des masques Loupe Pan Tilt ____ ____ _____ Bit 76 543 210 WT RSM RSM W = Wide T = Télé R = Réserve S = Sens M = Marche */ byte Tilt = B11111110; // masque Tilt Marche Off byte Pan = B11110111; // masque Pan Marche Off byte Loupe = B00111111; // masque Zoom Marche Off pour les deux // Wide et Tele byte Tilt_Pan; //byte New_Pan; byte New_Loupe; //byte Remote; /* This example is for Series 2 XBee Sends a ZB TX request with the value of analogRead(pin5) and checks the status response for success */ // create the XBee object XBee xbee = XBee(); uint8_t payload[] = { 0, 0, 0 }; // SH + SL Address of receiving XBee XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x40AF59F4); // 0013A200 40AF59F4 ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload)); ZBTxStatusResponse txStatus = ZBTxStatusResponse(); void flashLed(int pin, int times, int wait) { for (int i = 0; i < times; i++) { digitalWrite(pin, HIGH); delay(wait); digitalWrite(pin, LOW); if (i + 1 < times) { delay(wait); } } } void setup() { pinMode(statusLed, OUTPUT); pinMode(errorLed, OUTPUT); pinMode(successLed, OUTPUT); Serial.begin(9600); xbee.setSerial(Serial); } void loop() { // break down 10-bit reading into two bytes and place in payload pin5 = analogRead(VRy_2Pin); // pin5 = analogRead(5); payload[0] = pin5 >> 8 & 0xff; payload[1] = pin5 & 0xff; read_Joystick_1(); // function read_Joystick_2(); // function // TestBoutons(); // Zoom(); payload[2] = Bton_Tilt + Bton_Pan + Tele; Serial_Monitor(); xbee.send(zbTx); // flash TX indicator // flashLed(statusLed, 1, 100); // after sending a tx request, we expect a status response // wait up to half second for the status response if (xbee.readPacket(100)) { // got a response! // should be a znet tx status if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) { xbee.getResponse().getZBTxStatusResponse(txStatus); // get the delivery status, the fifth byte if (txStatus.getDeliveryStatus() == SUCCESS) { // success. time to celebrate // flashLed(successLed, 5, 50); digitalWrite(successLed, HIGH); } else { // the remote XBee did not receive our packet. is it powered on? digitalWrite(successLed, LOW); flashLed(errorLed, 5, 5); } } } else if (xbee.getResponse().isError()) { //nss.print("Error reading packet. Error code: "); //nss.println(xbee.getResponse().getErrorCode()); } else { // local XBee did not provide a timely TX Status Response -- should not happen flashLed(errorLed, 2, 50); } // delay(1000); } //void TestBoutons(){ // int x =analogRead (0); // //// ************ bouton DROIT 4 --> déplace vers la droite // if (x < 30) { // Bton = 4; // Tilt_Pan = B11101110; // } // //// ************ bouton HAUT 1 --> déplace vers le haut // else if(x <200) { // Bton = 1; // Tilt_Pan = B11110111; // } // //// ************ bouton BAS 2 --> déplace vers le bas // else if(x <400) { // Bton = 2; // Tilt_Pan = B11110101; // } // //// ************ bouton GAUCHE 3 --> déplace vers la gauche // else if(x <600) { // Bton = 3; // Tilt_Pan = B11111110; // } // // // bouton SELECT 5 // else if(x <800){ // // Bton = 5; // } // // Pas de bouton appuyé = 5V // else if(x >1000){ // Tilt_Pan = B11110110; // Bton = 192; // } // delay(10); // on attend un peu pour laisser le temps a l'utilisateur de relacher le bouton // //} //void Zoom(){ // int x =analogRead (1); ///* //// ************ bouton TÉLÉ //*/ // if (x < 60) { // Tele = 127; // New_Loupe = B01111111; // } ///* //// ************ bouton WIDE //*/ // else if(x <550) { // Tele = 191; // New_Loupe = B10111111; // } // else{ // Tele = 63; // New_Loupe = Loupe; //// digitalWrite(Out_Wide, LOW); // } // // delay(10); // on attend un peu pour laisser le temps a l'utilisateur de relacher le bouton // //} void read_Joystick_1(){ // read the analog VRx1 value --> up or down VRx_1Value = analogRead(VRx_1Pin); sens_Tilt = min(Old_VRx_1Value,VRx_1Value); Old_VRx_1Value = VRx_1Value; // test value of pot VRx if (sens_Tilt < 470) { UP = 1; DOWN = 0; Bton_Tilt = 1; Tilt_Pan = B11110111; // Serial_Monitor(); } else if(sens_Tilt > 560){ UP = 0; DOWN = 1; Bton_Tilt = 2; Tilt_Pan = B11110101; // Serial_Monitor(); } else{ UP = 0; DOWN = 0; Tilt_Pan = B11110110; Bton_Tilt = 0; // Serial_Monitor(); } // read the analog VRy1 value --> left or right VRy_1Value = analogRead(VRy_1Pin); sens_Pan = min(Old_VRy_1Value,VRy_1Value); Old_VRy_1Value = VRy_1Value; // test value of pot VRy if (sens_Pan < 470) { LEFT = 0; RIGHT = 1; Bton_Pan = 4; Tilt_Pan = B11101110; // Serial_Monitor(); } else if(sens_Pan > 560){ LEFT = 1; RIGHT = 0; Bton_Pan = 3; Tilt_Pan = B11111110; // Serial_Monitor(); } else{ LEFT = 0; RIGHT = 0; Tilt_Pan = B11110110; Bton_Pan = 0; // Serial_Monitor(); } // delay before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); } void read_Joystick_2(){ // read the analog VRx2 value --> Wide or Tele VRx_2Value = analogRead(VRx_2Pin); sens_Tele = min(Old_VRx_2Value,VRx_2Value); Old_VRx_2Value = VRx_2Value; // test value of pot VRx2 if (sens_Tele < 470) { TELE = 1; WIDE = 0; Tele = 64; New_Loupe = B01000000; // Serial_Monitor(); } else if(sens_Tele > 560){ TELE = 0; WIDE = 1; Tele = 128; New_Loupe = B10000000; // Serial_Monitor(); } else{ TELE = 0; WIDE = 0; Tele = 0; New_Loupe = Loupe; // Serial_Monitor(); } // delay before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); } void Serial_Monitor(void){ // print the results to the serial monitor: // digitalWrite(Pan_LEFT,LEFT); // digitalWrite(Pan_RIGHT,RIGHT); // digitalWrite(Tilt_UP,UP); // digitalWrite(Tilt_DOWN,DOWN); // digitalWrite(Zoom_WIDE,WIDE); // digitalWrite(Zoom_TELE,TELE); // Serial.print("LEFT = " ); // Serial.println(LEFT); // Serial.print("RIGHT = "); // Serial.println(RIGHT); // Serial.print("WIDE = "); // Serial.println(WIDE); // Serial.print("TELE = "); // Serial.println(TELE); Serial.print("data = "); Serial.println(payload[2],DEC); // delay(500); }