Author Topic: ATTiny 85 timers,whats the best way to implement a delay when using arduino IDE?  (Read 14704 times)

0 Members and 1 Guest are viewing this topic.

Offline tobyrobbTopic starter

  • Contributor
  • Posts: 15
Hi all,

        Haven't posted much, usually like to try everything myself first.  But this one is something i feel i need to ask.

Over the years I have been building these movement activated night lights, have recently made my first PCB that includes some surface mount!

I was using the ATmega328 cause im a bit of a noob in some ways, plus i kinda like the plain english structure of the Arduino IDE programming environment, and i can pass on my code and kits.

I have replaced the atmega328 with an Attiny85 which i am still programming with the plain language of the arduino IDE.

The problem is, I am having trouble making a nice delay while my light is on.

Not every command is supported of course for the Attiny and i guess some timers are used for some functions, leaving them unavailable for other things,  i guess i would need to check a whole bunch of stuff, the data sheet and the library that supports the attiny through arduino.

But just as a regular question,

does everyone here use timers based around interrupts?

I have of course been using the delay command, cause im a noob, but i guess a hardware timer with interrupt is the best way?

Im unsure and would like to be pointed in the right direction so i can code better.


Toby.

My board is attached ..



« Last Edit: February 25, 2014, 05:43:55 am by tobyrobb »
 

Offline Zkronk

  • Newbie
  • Posts: 6
  • Country: se
I guess that the delay()-function in the Arduino IDE is just some kind of while-loop function that you eaisly could replicate yourself :)
But if it's would be that simple, wonder why the ATTiny 85 doesn't support that?
 

Offline tobyrobbTopic starter

  • Contributor
  • Posts: 15
Umm it does, ive just been having trouble trying other types of delay, mainly because i have a watchdog timer, which means i am using a for loop for the delay so I can reset the watchdog, if i use just delay, then the times over 8 seconds would cause the watchdog to time out...

The list of supported commands for ATtiny85 are
pinMode()
digitalWrite()
digitalRead()
analogRead()
analogWrite()
shiftOut()
pulseIn()
millis()
micros()
delay()
delayMicroseconds()


 

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4695
  • Country: au
  • Question Everything... Except This Statement
micros at-least on the 328 is remarkable accurate,
millis i would imagine to be equally good and works nicer if your delays are over 50 odd minutes, (overflow)

i use this in my own code

Code: [Select]
if (micros() - NextTime >= Rate ) {         // Compare if the time has been reached
      NextTime += Rate;                     // add on the amount of microseconds for the next occurance
 

Offline tobyrobbTopic starter

  • Contributor
  • Posts: 15
Thanks, I have tried something similar. I wonder if the watchdog timer is the same timer as the millis command, I have dropped the watchdog, the delay command is working fine, I will try your version though and compare.

I just hope i don't need the watchdog on such a simple project...

Toby.
 

Offline psycho0815

  • Regular Contributor
  • *
  • Posts: 150
  • Country: de
    • H-REG Blog
nope the wdt is completely different. the point of it is to detect if your programm freezes and reset the mcu if it does. so you have to make sure you reset it before the timer runs out.
with a non-blocking delay you could do something like

Code: [Select]
loop(){
 if (micros() - NextTime >= Rate ) {         // Compare if the time has been reached
      NextTime += Rate;                     // add on the amount of microseconds for the next occurance
      doStuff();
  }
  wdt_reset();
}

of course in that case you have to make sure, that a normal execution of doStuff doesn't take longer than your watchdog timeout.
If you like, check out my blog (german):
http://h-reg.blogspot.de
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf