attiny85 if statement not working

Hey guys :slight_smile: help!
i have an attiny85 with ATTinyCore by Space Konde that does not respect the if statement.
here is the code:

volatile int i = 0;
void setup() {
  pinMode(3, OUTPUT);

}

void loop() {
  i++;
  if (i > 147)
  {
    digitalWrite(3, HIGH);
    delay(1050);
    digitalWrite(3, LOW);
    delay(1050);
    i = 0;
  }
}

i first tried with i == 0, did not work. i checked a lot if those are 2 equal signs or just one. no matter what i do the tiny does not want to respect that if statement.
i tried changing the delay time, the attiny starts toggling the output as soon as i give it power. and it does not reset the value.
i burned the bootloader that comes with the core. i changed the core because it did the same with the previous one.

klayus:
i tried changing the delay time, the attiny starts toggling the output as soon as i give it power.

That loop() is very small, and since you're incrementint i on every loop() then it is going to hit 147 so fast that you won't notice the small delay that the if() statement gives you.

klayus:
i burned the bootloader that comes with the core. i changed the core because it did the same with the previous one.

What core were you using, and what core are you using now? Which core's bootloader did you burn?

I am with BJHenry.

I bet it is working fine. Add a small delay right after the i++, just before the if, and see what happens. If you add delay(35) it should take about 5 seconds before executing the if part.

Willem.

Why do you expect there to be a significant delay?

When the condition isn't met, the loop runs very quickly - all you're doing is incrementing a variable, testing if it's greater than 147, and then going back and doing it all over again. That will take something on the order of low-single-digit microseconds if it's running at 8 or 16mhz. So doing it 147 times will take less than a millisecond. Then the condition in the if statement will be true and it will start toggling the pin.

It sounds to me like you think that there should be a delay happening between iterations of loop() if the condition in the if statement is not true - but the only delay() statements you have are in the block that is only executed when the if condition is true.

Also, my first name is Spence, not Space :wink:

DrAzzy:
Also, my first name is Spence, not Space :wink:

I think Space Konde would be your 31st century superhero name.

the tiny toggles the output without the 1 second delay. it does not keep it high for one second and then low for 1 second. it just toggles it from high to low very fast. i'll post a screen shot from the serial monitor tomorrow. i don't remember what core was on it before.
so here is what i'mt trying to do:
i need to log the fuel consumption from a truck, send it to a tablet that uploads it to a server. i'm using 2 flow meters - 5880 tics per liter, log the ticks from both of them and the difference is the fuel consumed. i tried using an arduino nano, atmega328, using the interrupt pins, one increments the value, the other decrements it and what is left was the fuel consumed but i noticed most of the impulses from the flow meters happened simultaneous and the value resulted was far from what was supposed to be. so i setup 2 attiny85 that count the impulses from the flow meters, and once every 147 ticks they send one digital high to the arduino nano interrupt pins. i noticed that the code i wrote first did not act as was supposed to so i wrote that test code from my first post. there i noticed that the tiny acts weird in that if statement.

ps - sorry for changing your name :smiley:

What clock speed is selected? I will attempt to reproduce on my T85 when I get home from work.

I tried it with both 8 and 16 mhz

When I upload that code to my ATTiny85 (8MHz internal ATTinyCore, github latest - but nothing around this has been touched since last board manager release)...

I get exactly what I would expect:
The LED immediately starts blinking with ~1 second on and off periods

Is that not what you see?