ATTiny 85 timers,whats the best way to implement a delay when using arduino IDE? - Page 1
Forum section: electronics / projects

author: tobyrobb
author_rank: Contributor
timestamp: February 25, 2014, 05:35:58 am
content: 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 ..

author: Zkronk
author_rank: Newbie
timestamp: February 25, 2014, 05:40:22 am
content: I guess that the delay()-function in the Arduino IDE is just some kind of while-loop function that you eaisly could replicate yourselfBut if it's would be that simple, wonder why the ATTiny 85 doesn't support that?

author: tobyrobb
author_rank: Contributor
timestamp: February 25, 2014, 05:48:14 am
content: 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 arepinMode()digitalWrite()digitalRead()analogRead()analogWrite()shiftOut()pulseIn()millis()micros()delay()delayMicroseconds()

author: Rerouter
author_rank: Super Contributor
timestamp: February 25, 2014, 07:17:29 am
content: 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 codeCode:if (micros() - NextTime >= Rate ) { // Compare if the time has been reachedNextTime += Rate; // add on the amount of microseconds for the next occurance

author: tobyrobb
author_rank: Contributor
timestamp: February 25, 2014, 07:50:40 am
content: 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.

author: psycho0815
author_rank: Regular Contributor
timestamp: February 25, 2014, 10:50:14 am
content: 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 likeCode:loop(){if (micros() - NextTime >= Rate ) { // Compare if the time has been reachedNextTime += Rate; // add on the amount of microseconds for the next occurancedoStuff();}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.