Question about timers ATtiny85 code and question -Arduino

djsfantasi

Joined Apr 11, 2010
9,237
"using it to blink two LEDs at different intervals would only work if one rate is a multiple of the other."
Using delay() would require that the delay interval be the least common denominator of the two intervals. Otherwise, using delay wouldn’t work.

Consider flashing at 30 and 40 seconds. If you flashed at 30 seconds, then 40 seconds, with delays, one cycle would take 70 seconds. You’d miss flashing at 60 seconds if you delayed for a total of 70 seconds. If the times were 20 seconds and 60 seconds, work out this example and see for yourself that you wouldn’t miss any times.

Using the millis() function, you could define the start and end times for the two LEDs, regardless of how the two times are related. Start one. Continue checking and end at 30 seconds later. Start the second at n seconds. Continue checking and end at 40 seconds later, regardless of the first delay times.

Which I am pretty sure at this point the battery operated device would continue to run unnecessarily all day until it "woke up again" and resumed it's "on" "off" program.
I don’t see how the device would be powered all day if you are sleeping for 20 second intervals. It sounds like at most it might be powered on at most an extra 20 seconds?

When scheduling, once the second LDR says it’s time to go to sleep, your program must do what it has to. That is, turn off the device and flash the LED (I think that’s what you said).

To clarify my suggestion, you have six events…
1) Turn the first LED on and calculate the time in milliseconds when you need to shut it off. Turn the device on
2) When it’s time, turn the first LED off
3) When it’s time, turn the second LED on and calculate when to turn it off.
4) When it’s time (based on the LDR input?) , turn the second LED off. Turn the device off.

As far as sleeping, at the beginning of these tests, go to sleep (5).

When you wake, check to see if it’s time to flash the LEDs and check the LDR. At the end of these tests, go to sleep for a time (20 seconds?). When the program awakes, check to see if you need to change the states of the LEDs, turn the device on or check the LDR.

The link I gave you shows how to check for one set of states. You can add more if statements for the other conditions.

Think in terms of discrete states not sequential operations. You know what you want to do when better than I. Adjust the states to match your requirements.
 
Last edited:

Thread Starter

fredric58

Joined Nov 28, 2014
252
Thank you, after a couple hours of research/studying it appears I do NOT need a latching switch but a high side bjt would work sufficiently to power my board. I can use the torchlight to power the base with only (1) output for however long I need without complicating the code. eliminating the need for a second LED or (high output) to turn the processor on and off with a latching switch. which means, regardless of what time of day or night, if the torch light goes to sleep and there is no power going to the base of the bjt, nothing is going to happen because current will not flow to my project board unless the torch light is sending a high signal to the base of the bjt transistor. Now it is simply a case of determining the available base current and the appropriate transistor to move enough current to the board I want to power. This just gets more fun every day. Thank you all, I seem to have a tendency to OVER complicate things. But! your input is priceless, in that it makes me THINK!

Perhaps I should move the next question to a new post? I have an Adafruit sound board, when I apply power to it the speakers POP. Is there a way to "soft" power it up? It seems to have something to do with magnetic fields. or it could be something else. I just need to eliminate it. Thanks all, Fred
 

djsfantasi

Joined Apr 11, 2010
9,237
It seems to have something to do with magnetic fields. or it could be something else.
It’s because the amplifier has a capacitor in the output. The pop is from the initial current flow into the capacitor at power on. The fix is to use a time delay relay to only connect the speaker a couple of seconds after power is supplied. This can be done with a special relay or a timer (555?) circuit.
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
I'm familiar with the 555's. neat concept. what would happen if I put a capacitor across the power in line. wouldn't some of the power go to the board "softly" (only way I can explain it) as the capacitor is charging? then as the cap is charging it would slide up gently to the full 3-5v. I know the abrupt 5v is causing the problem. actually..... the audio is triggered when 1 of the 10 pins go to ground, there's no POP there. so maybe delay the pin to ground a few mS after it is powered up? is the "special relay" mentioned a mechanical relay? or how about a small capacitor between the audio trigger pin and ground? wouldn't the current have to charge the cap before making it's way to ground giving me a short delay? then when the audio is finished it would just discharge back to empty? not sure because it is on the ground side???
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
I mulled it over. OK, the 555, since it's on the low side/going to ground. how would it work? read the audio pin 1, if 0, wait 2mS then send to ground? the fact that it is on the ground side is confusing.
 

djsfantasi

Joined Apr 11, 2010
9,237
I mulled it over. OK, the 555, since it's on the low side/going to ground. how would it work? read the audio pin 1, if 0, wait 2mS then send to ground? the fact that it is on the ground side is confusing.
Wire the output of the 555 to a relay coil. Make sure you have a diode reverse biased across the coils terminals. Then the audio output is connected to the relay’s COM terminal and it’s NO terminal is wired to the speaker.
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
Hello, I think I am getting there. Can't use a relay because it uses too much power, same with the 555. It's battery operated and every bit of the battery is needed. So what I came up with was I added pin 6 as an output, just using LED's right now to see if it works. So READ LDR says it dark. The original pin 5 turns on LED, it waits, and then pin 6 lights up the other LED. The LED's will be replaced by transistors as switches eventually. Keith had made mention of timing issues and I believe I have discovered that. What I thought would be a (1) sec delay delay (1000); turns out to be about 8 seconds. Also what I thought would be a 15 second delay delay (15000); turns out to be around (2 (+-)) minutes. It works in that it turns on at dark, delays between the (2) LED's and shuts down in the light. Please point me in the direction of why the timing is so off. Thank you, Fred


Code:
   {
    power_timer0_enable ();
    delay (1);  // let timer reach a known point
    digitalWrite (LED1, HIGH);    //turns on my audio board using a bjt with NO POP in the speakers
    delay (1000);                      //8 Seconds actually
    digitalWrite (LED2, HIGH);    //sends audio trigger to ground using a bjt and activates audio
    delay (15000);                //Plays audio for 15 seconds 2+- Minutes
    digitalWrite (LED1, LOW);
    digitalWrite (LED2, LOW);
    delay (15000);                //pauses for 15 seconds and repeats 15 second of audio till LDR reads daytime then the thing goes to sleep
    
    power_timer0_disable ();
    }
 

JWHassler

Joined Sep 25, 2013
308
What I thought would be a (1) sec delay delay (1000); turns out to be about 8 seconds. Also what I thought would be a 15 second delay delay (15000); turns out to be around (2 (+-)) minutes. It works in that it turns on at dark, delays between the (2)
Your Arduino setup may be expecting a different clock-source for the microprocessor.
The factor-of-eight error is telling
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
Hey JW, thanks for dropping in on the conversation, hope your weekend is going well. check out post 18 by Keith. Post 1 has the original code. all I did was add another LED to pin 6 and up loaded it to the Tiny 85 with the same settings ,ISP Arduino, Tiny 85 w/8MHz clock.

Original with (1) LED
Code:
const byte LED = 0;          // pin 5
const byte LDR_ENABLE = 3;   // pin 2
const byte LDR_READ = 1;     // Ain1 (PB2) pin 7
const int LIGHT_THRESHOLD = 200;  // Flash LED when darker than this
When I changed it to:
Code:
const byte LED1 = 0;          // pin 5
const byte LED2 = 1;           //pin 6
const byte LDR_ENABLE = 3;   // pin 2
const byte LDR_READ = 1;     // Ain1 (PB2) pin 7
const int LIGHT_THRESHOLD = 200;  // Flash LED when darker than this
that's when I ended up with the 8x, I didn't think it would make a difference just adding (1) LED. Thanks, Fred
 

JWHassler

Joined Sep 25, 2013
308
Hey JW, thanks for dropping in on the conversation, hope your weekend is going well. check out post 18 by Keith. Post 1 has the original code. all I did was add another LED to pin 6 and up loaded it to the Tiny 85 with the same settings ,ISP Arduino, Tiny 85 w/8MHz clock.

Original with (1) LED
Code:
const byte LED = 0;          // pin 5
const byte LDR_ENABLE = 3;   // pin 2
const byte LDR_READ = 1;     // Ain1 (PB2) pin 7
const int LIGHT_THRESHOLD = 200;  // Flash LED when darker than this
When I changed it to:
Code:
const byte LED1 = 0;          // pin 5
const byte LED2 = 1;           //pin 6
const byte LDR_ENABLE = 3;   // pin 2
const byte LDR_READ = 1;     // Ain1 (PB2) pin 7
const int LIGHT_THRESHOLD = 200;  // Flash LED when darker than this
that's when I ended up with the 8x, I didn't think it would make a difference just adding (1) LED. Thanks, Fred
Huh. It LOOKS like your micro is running on 1 MHz even if programmed for 8MHz.
My ignorance is darn near perfect past this point, but I did find a fragment of info that may help:
https://forum.arduino.cc/t/why-does-millis-not-work-on-attiny85/911726/17
... it's down on post #18
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
Today will be a study day of millis. after reading why it won't work. Well Well, might this be the cause of it being 8x slower? It says the ATtiny85 comes from the factory set to 1MHz. I have lots of 85's, this one I may not have burned the bootloader. I shall now and see what happens. THANKS for the link.

Another question. Since delay blocks the execution of any other code, if I CAN use millis would the tiny also go to sleep when the LED's/signals are in the low state/not doing anything?


Events:
1) LDR reads/looks for day or night all the time
2) When the LDR determines it is dark it begins blinking LED's. (These are simply signals just using them for visual right now)
3) LED 1 will be the signal to the base of a BJT that turns on the audio sound board from its power supply.
4) LED 2 turns on a 1/2 second later which is also a signal to the base of a BJT and sends the trigger pin to ground causing the board to play audio. Both LED's will stay on for 10 - 15 seconds, both powering the sound board and holding the trigger pin to ground. shut off for 20 -30 seconds and repeat that process until the LDR determines it is day time wherein it will stop sending signals and go back to sleep and wait for night again.

I did have the trigger pin simply set to ground. But when the board powered up the speakers would POP. The second LED/signal delay (trigger pin to gnd) is required so the speakers don't POP when the board is powered up. DJ explained why on post 23.

Thanks to all ya'll, Fred
 

djsfantasi

Joined Apr 11, 2010
9,237
If the ATTiny85 doesn’t work as desired, consider using an Arduino Nano instead. It’s larger but still small. And you wouldn’t have to jump through hoops to make the timings work.
 

Thread Starter

fredric58

Joined Nov 28, 2014
252
PROBLEM SOLVED! It was set to the factory at 1 MHz, I bootloaded it and it WORKS. Now running at 8MHz. As far as other components and code goes. This is my study project. I find the entire thing fascinating. I intend to learn what every line of code is for, why it's there, why it does what it does.
 
Last edited:

Thread Starter

fredric58

Joined Nov 28, 2014
252
Hey guys, I think I discovered, as Edison did, another way something WON'T work. PWM, is relatively simple, on ,off, on, off...... works fine for things like LED's, but It apparently is NOT the way to drive the base of a BJT (as a switch). I think it needs a continuous current to operate in the correct manner? any comments? Thanks, Fred
 
Top