I am trying to create breakbeam sensors for a stair project. However I have encountered a strange gremlin. When I use a separate arduino to power the IRLED, it all acts as it should. The blue and yellow LEDs light up when I shine the IRLED at the sensors, and turn off when I block it or shine it elsewhere. The code on the IRLED powering arduino is as follows:
#include <Arduino.h>
//interrupt for turning the 38Hz IR signal on/off
ISR (TIMER1_COMPA_vect)
{
TCCR2A ^= _BV (COM2A0) ; // Toggle OC2A on Compare Match
if ((TCCR2A & _BV (COM2A0)) == 0)
digitalWrite (11, LOW); // ensure off
} // end of TIMER1_COMPA_vect
void setup() {
// put your setup code here, to run once:
pinMode(11, OUTPUT); //OC2A pin
TCCR1A = _BV(COM1A0); //CTC
TCCR1B = _BV(WGM12) | _BV(CS12); //CTC, prescaler 256
OCR1A = 1875; //This value probably doesn't matter that much, but it works so why change it?
TIMSK1 = _BV (OCIE1A); // enable Timer1 Interrupt
//Set up timer 2 for 38kHz PWM. toggles on OCR1A value
TCCR2A = _BV(WGM21) | _BV (COM2A0); //CTC
TCCR2B = _BV (CS20); // No prescaler
OCR2A = 209; //frequency 38kHz
}
void loop() {
// put your main code here, to run repeatedly:
}
Using the same timer interrupt code, but adding in the logic for the IR sensors and indicator LEDs I get this:
#include <Arduino.h>
#define LED_Y 4
#define LED_B 5
//outputs from VS838 IR sensors
#define SEN_Y 6
#define SEN_B 7
unsigned long t;
unsigned long blueCount;
unsigned long yellowCount;
unsigned long timeCount;
//interrupt for turning the 38Hz IR signal on/off
ISR (TIMER1_COMPA_vect)
{
TCCR2A ^= _BV (COM2A0) ; // Toggle OC2A on Compare Match
if ((TCCR2A & _BV (COM2A0)) == 0)
digitalWrite (11, LOW); // ensure off
} // end of TIMER1_COMPA_vect
void setup() {
// put your setup code here, to run once:
// Serial.begin(9600);
pinMode(LED_Y, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(SEN_Y, INPUT_PULLUP);
pinMode(SEN_B, INPUT_PULLUP);
t = millis();
blueCount = 0;
yellowCount = 0;
timeCount = 0;
// create 38kHz signal on pin 11 with 50% duty cycle
pinMode(11, OUTPUT); //OC2A pin
TCCR1A = _BV(COM1A0); //CTC
TCCR1B = _BV(WGM12) | _BV(CS12); //CTC, prescaler 256
OCR1A = 1875; //This value probably doesn't matter that much, but it works so why change it?
TIMSK1 = _BV (OCIE1A); // enable Timer1 Interrupt
//Set up timer 2 for 38kHz PWM. toggles on OCR1A value
TCCR2A = _BV(WGM21) | _BV (COM2A0); //CTC
TCCR2B = _BV (CS20); // No prescaler
OCR2A = 209; //frequency 38kHz
}
void loop() {
// put your main code here, to run repeatedly:
while(millis() - t < 100){
timeCount++;
if(!digitalRead(SEN_B)){
blueCount++;
}
if(!digitalRead(SEN_Y)){
yellowCount++;
}
}
// Serial.println(timeCount);
// Serial.println(blueCount);
// Serial.println(yellowCount);
// Serial.println("");
// Serial.println(timeCount / blueCount);
// Serial.println(timeCount / yellowCount);
// Serial.println("");
if(timeCount / blueCount < 3){
digitalWrite(LED_B, LOW);
}
else{
digitalWrite(LED_B, HIGH);
}
if(timeCount / yellowCount < 3){
digitalWrite(LED_Y, LOW);
}
else{
digitalWrite(LED_Y, HIGH);
}
t = millis();
blueCount = 0;
yellowCount = 0;
timeCount = 0;
}
However, whenever I power the IRLED from the same arduino that I have the sensors plugged into, it doesn't work at all. When I have the serial monitor stuff uncommented it tells me that the blueCount & yellowCount are half of the timeCount which is what I would expect if we connected pin 11 directly to these inputs. Where I point the IRLED is irrelevant, and the same thing happens if I use a non IRLED (green for example).
So it seems there is some sort of interference going on when I plug the IRLED into pin 11, but I am damned if I know how to stop it from happening. I would like to get the project done on one arduino, but failing that I guess I will just use another arduino (or some other circuit) to generate my 38kHz signal. I was wondering if it was to do with the timer interrupt pins being associated with the timers, but even if I use the pins A0-A5 as the input pins I get the same result. Another reason I don't think the timer interrupt is the problem is because the circuit works if there is no load on pin 11.
I have tried both connecting the IRLED directly from pin 11 to ground, and connecting pin 11 to a transistor to drive the LED. There is no difference in the result.
Also, reason I have the 38kHz switching on and off is because if you send a constant 38kHz at the sensor, they saturate (is this the correct terminology?) and can't give a constant output. Hence why I am dividing the timeCount by the blueCount & yellowCount.
I have attached the circuit diagram (oh actually I realise I forgot the current limiting resistors (220 ohm) for the blue and yellow LEDs in the diagram) plus it is also here: Imgur: The magic of the Internet