I have successfully gotten an Arduino sketch working for reading an IR remote control, and I am working on porting it to an Attiny85. I am using the Arduino as a programmer, as documented here, and am able to get the basic "Blink" sketch working on the Attiny85.
This is the code I'm trying to run (adapted from this sample from sparkfun):
int irPin = 1; //Sensor pin 1 wired to Arduino's pin 2
int statLED = 0; //Toggle the status LED every time Power is pressed
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
void setup() {
pinMode(statLED, OUTPUT);
digitalWrite(statLED, LOW);
pinMode(irPin, INPUT);
}
void loop() {
int key = getIRKey(); //Fetch the key
}
int getIRKey() {
int data[12];
int i;
while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
digitalWrite(statLED, HIGH);
for(i = 0 ; i < 11 ; i++)
data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses
for(i = 0 ; i < 11 ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}
int result = 0;
for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
if(data[i] == 1) result |= (1<<i);
return result; //Return key number
}
At this point, all I want to do is toggle my LED to "on" once we receive the first pulse from the IR remote. This works as expected on the Duemilanove, but not on the Attiny85.
I feel like this is a timing issue with pulseIn(), for two reasons:
-
When I run the Blink sketch, the LED blinks at a much slower rate than 1 second. That is, delay(1000) delays for much longer.
-
When I manually unplug the sensor from pin and connect it to GND (thus pulling that pin to LOW), the LED does turn on, as expected. So there is something about my threshold on pulseIn() on an 8MHz Attiny85 which is not working properly with my remote control.
Does anyone have any thoughts on what would cause these timing differences, that I might have to account for in my code?