Help with pulseIn() on Attiny85?

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:

  1. 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.

  2. 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?

Well, if the Blink example runs slow it means there is a mistake in your ATtiny85 core. I'd look into getting that sorted out before working on more complex sketches.

johnwasser: Well, if the Blink example runs slow it means there is a mistake in your ATtiny85 core. I'd look into getting that sorted out before working on more complex sketches.

That was exactly the issue; I had never even thought that that's where the problem would occur, given that it was otherwise executing code "correctly". Thank you!

Have you sorted the 85’s fuses, by default it will run at 1MHz. If you clear the CLKDIV8 (sp?) fuse you will get 8MHz and by using the PLL (I haven’t done this mind you) you can get 16MHz.


Rob

When I try to use pulseIn in tiny85 I am not getting exact duty cycle in microseconds but when I use the same pulseIn in the arduni uno I am getting exact duty cycle value in microseconds. To saw out put from tiny85 I used SoftwareSerial function and connected pin 2 and 3 to the arduino Tx, Rx. then I can able to see that 192 or 128 as the value when I try to use 50 percentage duty cycle but I am getting 320 on the arduino uno. Could any one help me to solve this issue.