Hi guys, I really need some help here. Basically what I'm trying to do is fade 2 LED's at the same rate, with a push of a button. One going down and the other going up at the same time, over 90 seconds. It also pauses for 10 seconds before it starts the fade. The following sketch works. It's probably just a very ass backwards way of doing it using arrays and nothing like the blink without delay sketch.
#define FADE_TIME 32000
#define NUM_OUTPUTS 2
struct stOutput
{
int nPin;
unsigned long nFadeTime;
unsigned long nCurrentTime;
unsigned long nTimerStart;
int nStart;
int nEnd;
};
stOutput arrOutputs[NUM_OUTPUTS] = {{10, FADE_TIME, 0, 0, 255, 0}, {11, FADE_TIME, 0, 0, 0, 255} };
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
//Serial.println(map(40000, 90000, 0, 255, 0));
}
void loop() {
// put your main code here, to run repeatedly:
FadeOutputs();
if( digitalRead(2) == HIGH )
{
ResetOutputs();
}
}
void Start()
{
bool bStarted = false;
for (int i=0; i<NUM_OUTPUTS; ++i)
{
if( arrOutputs[i].nTimerStart != 0 )
{
bStarted = true;
break;
}
}
if( !bStarted )
{
for (int i=0; i<NUM_OUTPUTS; ++i)
{
analogWrite(arrOutputs[i].nPin, arrOutputs[i].nStart);
}
delay(10000);
}
}
void ResetOutputs()
{
Start();
for (int i=0; i<NUM_OUTPUTS; ++i)
{
if( arrOutputs[i].nTimerStart == 0 )
{
arrOutputs[i].nCurrentTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? 0 : arrOutputs[i].nFadeTime;
analogWrite(arrOutputs[i].nPin, arrOutputs[i].nStart);
arrOutputs[i].nTimerStart = millis();
}
}
}
void FadeOutputs()
{
for (int i=0; i<NUM_OUTPUTS; ++i)
{
if (arrOutputs[i].nTimerStart != 0)
{
unsigned long nTime = millis()-arrOutputs[i].nTimerStart;
if(nTime >= 5)
{
if( arrOutputs[i].nStart < arrOutputs[i].nEnd )
{
if((long)arrOutputs[i].nCurrentTime + nTime >= FADE_TIME)
{
arrOutputs[i].nTimerStart = 0;
arrOutputs[i].nCurrentTime = 0;
}
else
arrOutputs[i].nCurrentTime += nTime;
}
else
{
if((long)(arrOutputs[i].nCurrentTime - nTime) <= 0)
{
arrOutputs[i].nTimerStart = 0;
arrOutputs[i].nCurrentTime = 0;
}
else
arrOutputs[i].nCurrentTime -= nTime;
}
int nStartTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? 0 : arrOutputs[i].nFadeTime;
int nEndTime = (arrOutputs[i].nStart < arrOutputs[i].nEnd) ? arrOutputs[i].nFadeTime : 0;
int nValue = map(arrOutputs[i].nCurrentTime, nStartTime, nEndTime, arrOutputs[i].nStart, arrOutputs[i].nEnd);
Serial.println(nValue);
analogWrite(arrOutputs[i].nPin, nValue);
if( arrOutputs[i].nTimerStart != 0 )
arrOutputs[i].nTimerStart = millis();
}
}
}
}
The ONLY problem I have left is that it only works when my fade time is 30 seconds (not 90) if I increase it anymore than 32 seconds or so. My PWM values start doing weird crazy things like starting at 800 and not obeying the code as it was with 30 seconds and below. Its as if the map function just breaks when it has to use numbers above a certain value even though its using a unsigned long.
My apologies for the long code
I have no way to shorten to explain what I mean.
I Hope someone can help. Thanks!