Hello everyone!
I'm new to arduino so I'm hoping I can get some advice.
currently i'm running a loop with a button that i've replaced with wires i can touch together to close the "button," thus allowing the led to fade.
what i want to figure out is how can i put a timer/millis (or anything that will make it work) into the loop so the first loop would be, when the wires touch, to "fade." then over time, like 5 seconds, have a second loop start with a more intense fade, then a third loop with a more intense fade and so on and so for. and by intense i mean to decrease the fade's delay.
the problem is i can't fit in what i've found regarding millis (A) into what i've programmed so far (B).
what i've found may be n/a but its what i've been working with.
hope i can get some long lasting advice.
thanks!
(A)
unsigned long startTime = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long loopTime = millis() - startTime; //Calculate the time since last time the cycle was completed
if (loopTime <= 1000) //Check if the time is less than 1000 millis, and if so, run loop 1
{
Serial.println("Loop 1");
}
else if (loopTime > 1000 && loopTime <= 2000) //If time is over 1000 millis and less than/or 2000 millis, run loop 2
{
Serial.println("Loop 2");
}
else if (loopTime > 2000) //If time is over 2000 millis, set the startTime to millis so the loop time will be reset to zero
{
startTime = millis();
}
delay(250); //Only here to prevent spamming serial monitor too much
}
(B)
int ledPin = 3;
int switchPin = 13;
int val;
int brightness = 150;
int fadeAmount = 8 ;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop(){
val = digitalRead(switchPin);
if (val == LOW) {
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness > 255) {
brightness = 0;
}
delay (150);
}
if (val == LOW) {
analogWrite(ledPin, brightness);
brightness = brightness + fadeAmount;
if (brightness > 255) {
brightness = 0;
}
delay (150);
}
}