I Like to use the pins 4,5,6,7 for input pins at the example "ServoIn". But i don't know how to modify the example code. It is not working. Can you please help?
Here you will find the modified code:
/* ---------------------------------------------------------------------------** This software is in the public domain, furnished "as is", without technical** support, and with no warranty, express or implied, as to its usefulness for** any purpose.**** servoin_example.pde** Demonstrate Servo Signal Input functionality**** Author: Daniel van den Ouden** Project: ArduinoRCLib** Website: http://sourceforge.net/p/arduinorclib/** -------------------------------------------------------------------------*/#include<ServoIn.h>#include<Timer1.h>#define SERVOS 4uint16_tg_values[SERVOS];// output buffer for ServoInuint8_tg_workIn[SERVOIN_WORK_SIZE(SERVOS)];// we need to have a work buffer for the ServoIn classrc::ServoIng_ServoIn(g_values,g_workIn,SERVOS);voidsetup(){// Initialize timer1rc::Timer1::init();Serial.begin(9600);// We use pin 4-7 as Servo input pinspinMode(4,INPUT);pinMode(5,INPUT);pinMode(6,INPUT);pinMode(7,INPUT);// only allow pin change interrupts for PB20-23, digital pins 4-7.PCMSK2=(1<<PCINT20)|(1<<PCINT21)|(1<<PCINT22)|(1<<PCINT23);// enable pin change interrupt 0PCICR=(1<<PCIE2);// start listeningg_ServoIn.start();}voidloop(){// update incoming valuesg_ServoIn.update();// handle servo values here, stored in g_valuesfor(inti=0;i<SERVOS;i++){Serial.print(" Kanal: ");Serial.print(i);Serial.print(" = ");Serial.print(g_values[i]);}Serial.println(" ");}// Interrupt handling code below, this needs cleaningstaticuint8_tlastB=0;// Pin change port 0 interruptISR(PCINT2_vect){uint8_tnewB=PIND;uint8_tchgB=newB^lastB;// bitwise XOR will set all bits that have changedlastB=newB;if(chgB){if(chgB&_BV(0)){g_ServoIn.pinChanged(0,newB&_BV(0));}if(chgB&_BV(1)){g_ServoIn.pinChanged(1,newB&_BV(1));}if(chgB&_BV(2)){g_ServoIn.pinChanged(2,newB&_BV(2));}if(chgB&_BV(3)){g_ServoIn.pinChanged(3,newB&_BV(3));}}}
Last edit: dvdouden 2012-10-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Your problem is in the ISR, you've set up the interrupts correctly but you need to check for the pin in the interrupt handler. Currently you're checking for bits 0,1,2 and 3 on port D, which maps to pins 16,17,18 and 19. However you're using pins 20, 21, 22 and 23; those are in bits 4,5,6 and 7! It's my mistake for not using defines in the interrupt handler that you missed it.
Here's what the ISR should look like in your situation:
// Interrupt handling code below, this needs cleaning
static uint8_t lastD = 0;
// Pin change port 2 interrupt
ISR(PCINT2_vect)
{
uint8_t newD = PIND;
uint8_t chgD = newD ^ lastD; // bitwise XOR will set all bits that have changed
lastD = newD;
if (chgD)
{
if (chgD & _BV(PCINT20))
{
g_ServoIn.pinChanged(0, newD & _BV(PCINT20));
}
if (chgD & _BV(PCINT21))
{
g_ServoIn.pinChanged(1, newD & _BV(PCINT21));
}
if (chgD & _BV(PCINT22))
{
g_ServoIn.pinChanged(2, newD & _BV(PCINT22));
}
if (chgD & _BV(PCINT23))
{
g_ServoIn.pinChanged(3, newD & _BV(PCINT23));
}
}
}
Last edit: dvdouden 2012-10-29
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
Anonymous
-
2017-09-21
Post awaiting moderation.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I Like to use the pins 4,5,6,7 for input pins at the example "ServoIn". But i don't know how to modify the example code. It is not working. Can you please help?
Here you will find the modified code:
Last edit: dvdouden 2012-10-29
Sorry for the late reply, been busy :)
Your problem is in the ISR, you've set up the interrupts correctly but you need to check for the pin in the interrupt handler. Currently you're checking for bits 0,1,2 and 3 on port D, which maps to pins 16,17,18 and 19. However you're using pins 20, 21, 22 and 23; those are in bits 4,5,6 and 7! It's my mistake for not using defines in the interrupt handler that you missed it.
Here's what the ISR should look like in your situation:
Last edit: dvdouden 2012-10-29