salve a tutti,
sto cercando di "mergiare" due codici che usano interrupt, il primo si occupa di controllare due encoder e l'altro di inviare stringhe ppm..
quello che vorrei è mandare in ppm i due valori degli encoder, ma mi chiedo:
il ppm è, se ho capito bene, una serie di valori con una pausa di una determinata lunghezza che determina fine del treno precedente ed inizio di quello successivo.
credo che il ppm sia gestito da un interrupt tipo timer, cosi da garantire il treno in orario, mentre gli encoder con interrupt esterni.
ho letto molte cose negli ultimi giorni e comincio ad essere confuso
quello che mi chiedo è:
come faccio a dire al mega di dare priorità al timer interrupt rispetto agli interrupt legati ai pin?
oppure potrei sganciare i pin degli interrupt quando sono all'interno della isr che gestisce il ppm?
mumble mumble
encoder
float pulsesX, XA_SIG=0, XB_SIG=1, pulsesY, YA_SIG=0, YB_SIG=1;
void setup(){
attachInterrupt(0, XA_RISE, RISING); // Pin 2
attachInterrupt(1, XB_RISE, RISING); // Pin 3
attachInterrupt(4, YA_RISE, RISING); // Pin 19
attachInterrupt(5, YB_RISE, RISING); // Pin 18
Serial.begin(115200);
delay(100);
Serial.println("Connected");
}//setup
void loop(){
Serial.print("x");
Serial.print(pulsesX);
Serial.print("y");
Serial.print(pulsesY);
Serial.println("end");
delay(20);
}
//X-Axis
void XA_RISE(){
detachInterrupt(0);
//delay(1);
XA_SIG=1;
if(XB_SIG==0){
pulsesX++;//moving forward
}
if(XB_SIG==1){
pulsesX--;//moving reverse
}
attachInterrupt(0, XA_FALL, FALLING);
}
void XA_FALL(){
detachInterrupt(0);
//delay(1);
XA_SIG=0;
if(XB_SIG==1){
pulsesX++;//moving forward
}
if(XB_SIG==0){
pulsesX--;//moving reverse
}
attachInterrupt(0, XA_RISE, RISING);
}
void XB_RISE(){
detachInterrupt(1);
//delay(1);
XB_SIG=1;
if(XA_SIG==1){
pulsesX++;//moving forward
}
if(XA_SIG==0){
pulsesX--;//moving reverse
}
attachInterrupt(1, XB_FALL, FALLING);
}
void XB_FALL(){
detachInterrupt(1);
//delay(1);
XB_SIG=0;
if(XA_SIG==0){
pulsesX++;//moving forwar
}
if(XA_SIG==1){
pulsesX--;//moving reverse
}
attachInterrupt(1, XB_RISE, RISING);
}
//Y-Axis
void YA_RISE(){
detachInterrupt(4);
//delay(1);
YA_SIG=1;
if(YB_SIG==0){
pulsesY++;//moving forward
}
if(YB_SIG==1){
pulsesY--;//moving reverse
}
attachInterrupt(4, YA_FALL, FALLING);
}
void YA_FALL(){
detachInterrupt(4);
//delay(1);
YA_SIG=0;
if(YB_SIG==1){
pulsesY++;//moving forward
}
if(YB_SIG==0){
pulsesY--;//moving reverse
}
attachInterrupt(4, YA_RISE, RISING);
}
void YB_RISE(){
detachInterrupt(5);
//delay(1);
YB_SIG=1;
if(YA_SIG==1){
pulsesY++;//moving forward
}
if(YA_SIG==0){
pulsesY--;//moving reverse
}
attachInterrupt(5, YB_FALL, FALLING);
}
void YB_FALL(){
detachInterrupt(5);
//delay(1);
YB_SIG=0;
if(YA_SIG==0){
pulsesY++;//moving forward
}
if(YA_SIG==1){
pulsesY--;//moving reverse
}
attachInterrupt(5, YB_RISE, RISING);
}
ppm
// Data variables
unsigned int ppm[16]; // the array of values for the timer1 interrupt timings
unsigned int potVal; // value read from potemtiometer
int curPpm = 0; // the position in the array of timings
byte curPpmMax; // the actual number of timer steps
char pulsePin = 7; // the digital pin for sending pulses to the transmitter
char debugPin = 13; // something to watch to check things are working
char potPin = 2; // the analog pin for the potentiometer
byte loopCount = 0; // a crude substitute for delay()
int analogIn; // for reading potentiometer position
boolean testing = true;
void setup() {
if (testing) {
Serial.begin(9600);
Serial.println("Starting TrainRadioPot");
}
// set the pin directions
pinMode(pulsePin, OUTPUT);
// pinMode(debugPin, OUTPUT);
// set the timing values
ppm[0] = 1475; //3775; // long pulse - see notes above
ppm[1] = 75; // short dividing pulse
ppm[2] = 305; // loco1 ch1
ppm[3] = 75; // short
ppm[4] = 305; // loco2 ch2
ppm[5] = 75; // short
ppm[6] = 305; // loco3 ch3
ppm[7] = 75; // short
ppm[8] = 305; // loco4 ch4
ppm[9] = 75; // short
ppm[10] = 305; // loco5 ch5
ppm[11] = 75; // short
ppm[12] = 305; // loco6 ch6
ppm[13] = 75; // short
ppm[14] = 305; // loco7 ch7
ppm[15] = 75; // short
curPpmMax = 16; // the number of timer values
curPpm = 0; // the starting position in the array of timings
// setup and start timer interrupts
// ppm is achieved by sending different delays to the timer interrupt
noInterrupts();
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 1000; // It works by causing an interrupt when TCNT2 counts up to this number
// This number will be set from the ppm array when the program is running
digitalWrite(pulsePin,0); // start with pin low
// turn on CTC mode: Clear Timer on Compare
bitSet(TCCR1B, WGM12);
// Set CS10 and CS11 bits for 64 prescaler: 10, 12 = 1024
bitSet(TCCR1B, CS10);
bitSet(TCCR1B, CS11);
bitClear(TCCR1B, CS12);
// enable timer compare A interrupt:
bitSet(TIMSK1, OCIE1A);
interrupts();
}
void loop() {
if (loopCount == 1) {
analogIn = analogRead(potPin);
// the reading must be converted from the range 0 - 1023 to the range 250 - 500
// which means dividing by 4 and adding 180 to give 305 at the centre
potVal = (analogIn >> 2) + 180;
if (potVal > 302 && potVal < 308) {
potVal = 305; // to create an off space without jitter
}
ppm[2] = potVal;
}
// loopCount = 0; // for testing - so analog read is ignored
loopCount = loopCount + 1; // it will roll over to zero automatically
// the purpose of loopCount is so that the pot isn't read every loop
}
// interrupt routine that is triggered when the timer counts up to the preset value
ISR(TIMER1_COMPA_vect) {
noInterrupts();
digitalWrite(pulsePin, !digitalRead(pulsePin)); // change the state of the pin
OCR1A = ppm[curPpm]; // set the value for the next time interval
curPpm = ((curPpm + 1) % curPpmMax); // move the index on
// the modulus operator makes the index roll over to the start
interrupts();
}
grazie tante