Hello –
I am having mixed success with pin change interrupts for some Sharp 10Z0F digital IR sensors in an obstacle avoiding robot sketch. The MCU is a 1284P-PU @ 16MHz programmed with the Arduino IDE. I can get the following tester sketch to run OK, best I can tell.
//Pin defines on 1284P-PU
//PORTA pins
#define LEFTIR A5 //PA5, PCINT5
#define RIGHTIR A4 //PA4, PCINT4
void setup(){
Serial.begin(115200);
Serial.println("**** Sharp IR pin change ints tests ****");
pinMode(LEFTIR, INPUT);
pinMode(RIGHTIR, INPUT);
//enable pin change interrupts, mask for specific pins
PCICR |= (1<<PCIE0); //enable group interrupts on PORTA PCINT[7:0]
PCMSK0 |= (1<<PCINT5); //mask to enable interrupt on A5 for LEFTIR
PCMSK0 |= (1<<PCINT4); //mask to enable interrupt on A4 for RIGHTIR
}
void loop(){
Serial.println("HIGH");
delay(100);
}
ISR (PCINT0_vect) //handle pin change interrupt for group A0-A7, triggered by A5 & A4
{
if ( bitRead (PINA,5) == LOW)
{
veerRight();
}
if ( bitRead (PINA,4) == LOW)
{
veerLeft();
}
if ( bitRead (PINA,5) == LOW && bitRead (PINA,4) == LOW)
{
backUp();
}
}
void veerRight(){
Serial.println("Veer Right");
}
void veerLeft(){
Serial.println("Veer Left");
}
void backUp(){
Serial.println("Back Up");
}
This is my first foray into pin change interrupts, referencing some recent posts on forum.pololu and forum.arduino. Those posts regarded wheel encoders, which was the original focus of my research. I modified some found code to address the Sharp IRs, and the above seemed to work.
I dropped the above code into the bot’s sketch (attached file) and encountered some problems. First, in the future I am wanting to switch from motor control durations with delay()'s to encoder wheel counts, so there was a conflict with the PCINT0_vect (and it’s aliases) already used in the PololuWheelEncoders library. I commented the encoder library and init out. This will need to be resolved later.
The second problem is in the ISR and it’s function calls. In the attached sketch, course changes of very brief durations are had when the IRs detect, though not to the desired degree. I think my problem relates to the IR sensor going from HIGH to LOW, and then LOW to HIGH, which is two changes on the pin, the second change cutting the ISR short.
If forum members might offer some guidance in regard my second problem that would be great. In regard my first problem, of conflicts with the PCINT vectors in the wheel encoder library, advice for resolving that is appreciated. I am unsure which of these two problems should be tackled fist.
Thank you!
Mark
New6x2problem.ino (8.15 KB)