lesto:
allora il segnale dalla radio è un PPM
un impulso (che porta tutti i canali) dura circa 20millisec, e il segnale alto va da un minimo di 800microsec a un massimo di 2600.
con arduino puoi leggere sia il segnale "sorgente", ovvero il segnale che contiene tutti i canali, oppure un singolo canale alla volta.
purtroppo data la durata "effimera" del segnale, bisogna per forza usare gli interrupt. Arduino mette a disposizione 2 interrupt facili da usare, ma in realtà ogni pin può essere controllato.
il codice che uso io:
InputPin.cpp
/*
Let You read digital pin 2,4,5,6
modified FROM BIRONPILOT V59, tnx to ciskje
*/
#include "InputPin.h"
#define MASKPCINT0 (1<<2)
#define MASKPCINT1 (1<<4)
#define MASKPCINT2 (1<<5)
#define MASKPCINT3 (1<<6)
InputPin::InputPin(){
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
// interrupt on pin change FROM PCINT16 to PCINT23
PCICR |= (1 << PCIE2);
PCMSK2 = (1 << PCINT18) | // digital pin2
(1 << PCINT20) | // digital pin4
(1 << PCINT21) | // digital pin5
(1 << PCINT22); // digital pin6
}
//putted here because interrupt dosn't want it in .h
unsigned long _startIn[4];
unsigned long _rawIn[4];
unsigned long _time;
byte _oldbit=0, _newbit, _changed;
volatile boolean _hasChanged=false;
ISR(PCINT2_vect) {
_time=micros();
_newbit=PIND;
_changed=_newbit^_oldbit;
if (_changed&MASKPCINT0){//if digital PIN2 has changed
if (_newbit&MASKPCINT0) { //if PIN2 now is high
_startIn[0]=_time;
}else{
_rawIn[0]=_time-_startIn[0];
}
_hasChanged=true;
}
if (_changed&MASKPCINT1){//if digital PIN4 has changed
if (_newbit&MASKPCINT1)
_startIn[1]=_time;
else
_rawIn[1]=_time-_startIn[1];
_hasChanged=true;
}
if (_changed&MASKPCINT2){//if digital PIN5 has changed
if (_newbit&MASKPCINT2)
_startIn[2]=_time;
else
_rawIn[2]=_time-_startIn[2];
_hasChanged=true;
}
if (_changed&MASKPCINT3){//if digital PIN6 has changed
if (_newbit&MASKPCINT3)
_startIn[3]=_time;
else
_rawIn[3]=_time-_startIn[3];
_hasChanged=true;
}
_oldbit=_newbit;
}
bool InputPin::getAndSetFalseHasChanged(){
bool temp = _hasChanged;
_hasChanged = false;
return temp;
}
int InputPin::getDuration(int i){
return _rawIn[i];
}
inputPin.h
#ifndef InputPin_h
#define InputPin_h
#include "WProgram.h"
class InputPin{
public:
InputPin();
bool getAndSetFalseHasChanged(); // send the initilization handshake
int getDuration(int); //return the raw duration of high signal, input: channel
private:
};
#endif
Ciao Lesto,
è passato un po di tempo dal tuo post,
volevo sapere se hai perfezionato il tuo codice, e se è possibile aumentare il numero di canali in ingresso, portandolo fino ad 8.
Ho preso un Pro Trinket che monta un ATmega328 con 18 GPIO, 28K of flash, and 2K of RAM.
Appena mi arriverà proverò subito il tuo codice...
Se hai altre informazioni utilissime ti ringrazio.
Ciao