Hi,
Don't come on here much as I use my Uno to write simple programmes for LED lighting kits for models, usually simple enough to load onto an Attiny85 as this has more than enough I/O for the lighting kits.
I'm building a bespoke kit for a Sci-Fi kit where I want to flash an LED for 2 seconds on then 2 seconds off and also have 2 LEDs that the first fades up stays on for 20 seconds, fades down, the second fades up, on for 20 seconds and then fades down and just keeps repeating this cycle.
Had a play around with a couple of solutions neither of which worked successfully, mainly to the use of the "delay" instruction, it stopped the LedFlash.h library from working.
The last programme I developed used a state change for the fading cycle and works exactly as I want it to on the Uno, LED flashes for 2 seconds and then out for 2 and the 2 LEDs fade in and out in sequence, happy days!
I have then loaded it into the Attiny85 but it won't run the same, the fading works though is a bit jerky and the flash rate is inconsistent.
This is the programme code I am using, is there something I have missed to stop it working in the Attiny85 or is there a better, simpler way to do what I am after.
Have I missed something to make it work with the Attiny85 or is there a simpler way to do what I am trying to achieve with the 85. (Code is for 85 using pins 0 & 1 for PWM)
Thanks in anticipation.
#include <LedFlasher.h>
// pin assignments
const byte Strobes1Pin = 4;
const int RestgreenPin = 0; // PWM
const int BattleyellowPin = 1; // PWM
// Flashers pin off-time on-time on?
LedFlasher strobes1 (Strobes1Pin, 2000, 2000, false);
// states for the state machine
typedef enum
{
initialState,
wantStrobes1, // ALWAYS ON
wantRestgreenstartup, // Startup mode
wantRestgreenoff, // Battle mode
wantBattleyellowon, // Battle mode
wantBattleyellowoff, // Rest mode
wantRestgreenon, // Rest mode
} states;
// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;
void setup ()
{
pinMode (Strobes1Pin, OUTPUT);
// set up faders, flashers
strobes1.begin ();
} // end of setup
void doStateChange ()
{
lastStateChange = millis (); // when we last changed states
timeInThisState = 1000; // default one second between states
switch (state)
{
case initialState:
state = wantStrobes1;
break;
case wantStrobes1:
strobes1.on();
state = wantRestgreenoff;
timeInThisState = 1000;
break;
//battle mode
case wantRestgreenoff:
{
float in, out;
// falling
for (in = 1.570; in < 4.712; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(RestgreenPin,out);
delay(1);
}
state = wantBattleyellowon;
break;
case wantBattleyellowon:
// rising2
for (in = 4.712; in < 7.854; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(BattleyellowPin,out);
delay(1);
}}
state = wantBattleyellowoff;
timeInThisState = 20000;
break;
//rest mode
case wantBattleyellowoff:
{
float in, out;
// falling
for (in = 1.570; in < 4.712; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(BattleyellowPin,out);
delay(1);
}
state = wantRestgreenon;
break;
case wantRestgreenon:
// rising2
for (in = 4.712; in < 7.854; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(RestgreenPin,out);
delay(1);
}}
state = wantRestgreenoff;
timeInThisState = 20000;
break;
} // end of switch on state
} // end of doStateChange
void loop ()
{
if (millis () - lastStateChange >= timeInThisState)
doStateChange ();
// update faders, flashers
strobes1.update ();
} // end of loop[/[code]
Cheers,
Warren
[/code]