Hello, i'm looking for some direction. I just want to fade one LED at random times, somewhere between 10 - 45 sec. My issue is getting the fade loop to act like it's on it's own loop.
I know it's staring me in the face, any help would be appreciated.
Here is the code.
unsigned long interval=25; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
long randNumber;
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
randomSeed(analogRead(0));
}
// the loop routine runs over and over again forever:
void loop() {
unsigned long currentMillis = millis();
randNumber = random(30000);
if (randNumber > 29990){
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
previousMillis = millis();
}
}
else;
}
void loop() {
randNumber = random(5000,60000);
randNumber2 = random(5,30);
randNumber3 = random(2,5);
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=randNumber3) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(randNumber2);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=randNumber3) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(randNumber2);
}
analogWrite(9, 0);
delay(randNumber);
}
randNumber = random(30000);
if (randNumber > 29990){
What is the purpose of generating a random number between 0 and 29999, when you only plan to react to a value between 29991 and 29999?
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
If you subtract an unsigned long from an unsigned long, what type is the resulting value? Why do you think it is necessary to cast that type to unsigned long?
else;
Well, isn't THAT useful code.
You've posted two codes, and completely failed to describe what they actually do, or how that differs from what you want them to do. You can't get from point A to point B without knowing where points A and B are.
It's explained in the beginning, feel free to upload both to an arduino and see for yourself. It's a simple Led fade, how much explanation do you need?
What is? If "it" is what the code actually does, you did a very poor job of explaining that.
I just want to fade one LED at random times, somewhere between 10 - 45 sec.
10 seconds is an interval, NOT a time.
My issue is getting the fade loop to act like it's on it's own loop.
The issue is that that statement is completely void of meaningful content, and that you have completely failed to describe what either program actually does.
My issue is getting the fade loop to act like it's on it's own loop.
Sure thing. PM me, and I'll send you an address. Send me the Arduino, with LEDs connected, and tell me which program you want me to copy, compile, link, and upload. After I've observed what happens, I'll send the Arduino back to you. Maybe.