Transformer une ancienne radio en pupitre élève
https://forum.arduino.cc/t/liaison-decolage-bluetooth-pour-radio-commande-rc/433994
Schéma

/* CONFIGURATION */
#define sigPin 2 //Broche de l'arduino reliée au port écolage
#define chanel_number 8 //set the number of chanels
#define default_servo_value 1500 //set the default servo value
#define PPM_FrLen 22500 //set the PPM frame length in microseconds (1ms = 1000µs)
#define PPM_PulseLen 300 //set the pulse length
#define onState 1 //set polarity of the pulses: 1 is positive, 0 is negative
//////////////////////////////////////////////////////////////////
// Tableau contenant la valeur des différentes voies
int ppm[chanel_number];
// Librairie pour l'écran LCD. Voir http://www.arduino.cc/en/Tutorial/LiquidCrystal&k=2BjihEEf49EEnl2c9cXhDQ%3D%3D%0A&r=3yuNHmLmHt1hrQBr7%2FaeV9KqpNfZSTxLUDQUL57uyxA%3D%0A&m=SkLxmkhQhqEzY%2FvlispUxPuAvUk21SUcjXqzGB5sveo%3D%0A&s=587b781588977e6e878c986d2ea8915bb416cace0afa8a17fb36056dd6a03973
#include
// Configuration des broches du LCD
LiquidCrystal lcd(8, 9, 3, 5, 6, 7); // ATTENTION, elles correspondent à mon branchement qui n'est pas dans l'ordre des broches entre l'arduino et le LCD, pour vous ce sera probablement lcd(8,9,4,5,6,7); !!!
int button = 0;
// Consantes des valeurs des différents modes
const int STABILIZE = 1000; // < 1230 dans Mission Planner const int ALT_HOLD = 1300; // > 1750 "
const int LOITER = 1400; // 1250
const int RTH = 1550; // 1500
const int AUTO = 1700; // 1400
// Variable contenant la valeur du mode en cours
int modeDeVol_uS=STABILIZE;
// Constantes des boutons
const int BUTTON_SELECT = 1;
const int BUTTON_LEFT = 2;
const int BUTTON_UP = 3;
const int BUTTON_DOWN = 4;
const int BUTTON_RIGHT = 5;
// Constantes des valeur haute et basse de chaque bouton sur l'entrée analogique
const int BUTTON_SELECT_LOW = 700;
const int BUTTON_SELECT_HIGH = 800;
const int BUTTON_LEFT_LOW = 400;
const int BUTTON_LEFT_HIGH = 500;
const int BUTTON_UP_LOW = 100;
const int BUTTON_UP_HIGH = 150;
const int BUTTON_DOWN_LOW = 300;
const int BUTTON_DOWN_HIGH = 350;
const int BUTTON_RIGHT_LOW = 1;
const int BUTTON_RIGHT_HIGH = 99;
// Variables gérant l'anti-rebond des boutons. Source : http://www.instructables.com/id/How-to-access-5-buttons-through-1-Arduino-input/step5/Testing-it/&k=2BjihEEf49EEnl2c9cXhDQ%3D%3D%0A&r=3yuNHmLmHt1hrQBr7%2FaeV9KqpNfZSTxLUDQUL57uyxA%3D%0A&m=SkLxmkhQhqEzY%2FvlispUxPuAvUk21SUcjXqzGB5sveo%3D%0A&s=b76be266eab18fb9f2ab45900a5447f8746b125b07910bd94daaf544f3eaac6f
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
// Initialise les valeurs par défaut des différentes voies
for(int i=0; i<chanel_number; i++){
ppm[i]= default_servo_value;
}
// Configuration de la broche écolage en sortie
pinMode(sigPin, OUTPUT);
digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off)
// Configuration du timer d'interruption pour générer le signal PPM à interval régulier
cli();
TCCR1A = 0; // set entire TCCR1 register to 0
TCCR1B = 0;
OCR1A = 100; // compare match register, change this
TCCR1B |= (1 << WGM12); // turn on CTC mode
TCCR1B |= (1 << CS11); // 8 prescaler: 0,5 microseconds at 16mhz
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
sei();
// Définition de la résolution du LCD
//lcd.begin(16, 2); // Affiche le mode par défaut
// lcd.print("STABILZE");
}
void loop() {
// Lecture de la valeur des boutons
button = analogRead(0);
// On se positionne sur la deuxième ligne du LCD
lcd.setCursor(0, 1);
// On y affiche la valeur lue
lcd.print(button);
// Interprétation de la valeur analogique pour déterminer le bouton int tmpButtonState = LOW; if(button>BUTTON_LEFT_LOW && button<BUTTON_LEFT_HIGH){ //Read switch 5 tmpButtonState = BUTTON_LEFT; }else if(button>BUTTON_RIGHT_LOW && button<BUTTON_RIGHT_HIGH){ //Read switch 4 tmpButtonState = BUTTON_RIGHT; }else if(button>BUTTON_UP_LOW && button<BUTTON_UP_HIGH){ //Read switch 3 tmpButtonState = BUTTON_UP; }else if(button>BUTTON_DOWN_LOW && button<BUTTON_DOWN_HIGH){ //Read switch 2 tmpButtonState = BUTTON_DOWN; }else if(button>BUTTON_SELECT_LOW && button<BUTTON_SELECT_HIGH){ //Read switch 1 tmpButtonState = BUTTON_SELECT; }else{ //No button is pressed; tmpButtonState = LOW; } // Gestion du rebond if (tmpButtonState != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) {
buttonState = tmpButtonState;
}
lastButtonState = tmpButtonState;
// Maintenant on effectue les actions correspondantes
switch(buttonState){
case BUTTON_LEFT:
modeDeVol_uS = ALT_HOLD;
lcd.clear();
lcd.print("ALT_HOLD - " + String(modeDeVol_uS));
break;
case BUTTON_RIGHT:
modeDeVol_uS = AUTO;
lcd.clear(); lcd.print("AUTO - " + String(modeDeVol_uS));
break;
case BUTTON_UP:
modeDeVol_uS = LOITER;
lcd.clear(); lcd.print("LOITER - " + String(modeDeVol_uS));
break;
case BUTTON_DOWN:
modeDeVol_uS = RTH;
lcd.clear(); lcd.print("RTH - " + String(modeDeVol_uS));
break;
case BUTTON_SELECT:
modeDeVol_uS = STABILIZE;
lcd.clear(); lcd.print("STABILIZE - " + String(modeDeVol_uS));
break;
}
// On définie la valeur de la voie qui nous intéresse
// La numérotation commençant à 0. il faut écrire 4 pour la voie 5, ou 5-1 ;-)
ppm[5-1]=modeDeVol_uS;
}
/**
* Génération signal PPM
*
* Interruption pour générer le signal PPM à interval régulier
* Ne rien modifier sous cette ligne à moins de vraiment maitriser !
*/
ISR(TIMER1_COMPA_vect)
{
static boolean state = true;
TCNT1 = 0;
if(state)
{ //start pulse
digitalWrite(sigPin, onState);
OCR1A = PPM_PulseLen * 2;
state = false;
}
else
{ //end pulse and calculate when to start the next pulse
static byte cur_chan_numb;
static unsigned int calc_rest;
digitalWrite(sigPin, !onState);
state = true;
if(cur_chan_numb >= chanel_number)
{
cur_chan_numb = 0;
calc_rest = calc_rest + PPM_PulseLen;//
OCR1A = (PPM_FrLen - calc_rest) * 2;
calc_rest = 0;
}
else
{
OCR1A = (ppm[cur_chan_numb] - PPM_PulseLen) * 2;
calc_rest = calc_rest + ppm[cur_chan_numb];
cur_chan_numb++;
}
}
}