Bonjour ou bonsoir tout le monde !
J'écris ce post pour vous faire part d'un problème qui m'est venu en voulant tester le joytstick "HF series" de Apem. Ce joystick possède 3 axes (X, Y et Z) et 2 boutons sur le dessus.
J'ai premièrement testé les 3 différents axes, aucun problèmes là dessus, ils fonctionnent parfaitement. Ensuite, j'ai commencé à tester les 2 boutons du joystick. Mais petit problème, l'Arduino ne détecte pas l'appui des boutons ! Au lieu de ça, il me renvoi des 1 et des 0 aléatoirement sans que je touche les sois disant boutons ! (enfin pas vraiment aléatoirement, il me renvoi des 1 uniquement quand quand j'approche ma main du joystick, sans non plus appuyer sur les boutons, faisant de lui une sorte de détecteur de présence, pas vraiment ce que je recherche je dois avouer )
Je fais donc appelle à vous les internautes, pour potentiellement m'aider dans mon problème de boutons ! (en espérant que les boutons ne soient pas cassés )
Voici donc le code que j'ai utilisé pour tester mon joystick :
/*
* PPM generator originally written by David Hasko
* on https://code.google.com/p/generate-ppm-signal/
*/
//////////////////////CONFIGURATION///////////////////////////////
#define CHANNEL_NUMBER 3 //set the number of chanels
#define CHANNEL_DEFAULT_VALUE 1500 //set the default servo value
#define FRAME_LENGTH 20000 //set the PPM frame length in microseconds (1ms = 1000µs)
#define PULSE_LENGTH 300 //set the pulse length
#define onState 0 //set polarity of the pulses: 1 is positive, 0 is negative
#define sigPin 6 //set PPM signal output pin on the arduino
const int axeX = A0;
const int axeY = A1;
const int axeZ = A2;
//#define boutCom 6
#define bout1 5
#define bout2 4
int valX = 0;
int valY = 0;
int valZ = 0;
//int valBoutCom = 0;
int valBout1 = 0;
int valBout2 = 0;
/*this array holds the servo values for the ppm signal
change theese values in your code (usually servo values move between 1000 and 2000)*/
int ppm[CHANNEL_NUMBER];
void setup(){
Serial.begin(9600);
//pinMode(boutCom, INPUT);
pinMode(bout1, INPUT);
pinMode(bout2, INPUT);
//initiallize default ppm values
for(int i=0; i<CHANNEL_NUMBER; i++){
ppm[i]= CHANNEL_DEFAULT_VALUE;
}
pinMode(sigPin, OUTPUT);
digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off)
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();
}
void loop(){
/*
Here modify ppm array and set any channel to value between 1000 and 2000.
Timer running in the background will take care of the rest and automatically
generate PPM signal on output pin using values in ppm array
*/
valX = analogRead(axeX);
valY = analogRead(axeY);
valZ = analogRead(axeZ);
//valBoutCom = digitalRead(boutCom);
valBout1 = digitalRead(bout1);
valBout2 = digitalRead(bout2);
//Serial.print(F("boutCom = ")); Serial.print(digitalRead(boutCom));
Serial.print(F(" bout1 = ")); Serial.print(digitalRead(bout1));
Serial.print(F(" bout2 = ")); Serial.println(digitalRead(bout2));
ppm[0] = map(valX, 0, 1020, 1000, 2000);
ppm[1] = map(valY, 0, 1020, 1000, 2000);
ppm[2] = map(valZ, 0, 1020, 1000, 2000);
Serial.print(F("ppm[0] = "));Serial.print(ppm[0]);
Serial.print(F(" ppm[1] = "));Serial.print(ppm[1]);
Serial.print(F(" ppm[2] = "));Serial.println(ppm[2]);
delay(500);
}
ISR(TIMER1_COMPA_vect){ //leave this alone
static boolean state = true;
TCNT1 = 0;
if (state) { //start pulse
digitalWrite(sigPin, onState);
OCR1A = PULSE_LENGTH * 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 >= CHANNEL_NUMBER){
cur_chan_numb = 0;
calc_rest = calc_rest + PULSE_LENGTH;//
OCR1A = (FRAME_LENGTH - calc_rest) * 2;
calc_rest = 0;
}
else{
OCR1A = (ppm[cur_chan_numb] - PULSE_LENGTH) * 2;
calc_rest = calc_rest + ppm[cur_chan_numb];
cur_chan_numb++;
}
}
}
Le code ci-dessus, est un code qui permet de créer des signaux CPPM pour un projet, je vous passe les détails, le plus important est ce qu'il se passe dans la loop (et un peu dans le setup aussi) !
Quelques détails que j'aimerais rajouter : pour le pinMode des pin digitaux reliant les fils connectés aux 2 boutons, j'ai essayé de les mettre en INPUT_PULLUP, ce qui a également été un échec, car le programme me renvoi sans interruption des 1 alors même que j'appui sur les boutons.
En espérant que vous pourrez m'apporter de l'aide !