Hi can any one help ?
I'm having trouble figuring this out...
I'm trying to make 2 led's switch on but the first one I want to switch on right away, and the other one with a let's say 500ms delay.
and when i relese the button both of them I want to have a delay for like 2min or so but when they turn off i want one of them to turn off like 300ms fater then the other...
// These variables store the flash pattern
// and the current state of the LED
int buttonPin = 2;
int ledPin = 8;// the number of the LED pin
int ledState = LOW; // ledState used to set the LED
int led1Pin = 7;// the number of the LED pin
int led1State = LOW; // ledState used to set the LED
int buttonState = LOW;
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 0; // milliseconds of on-time
long OffTime = 2000; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((buttonState == HIGH) && (currentMillis - previousMillis))
{
ledState = HIGH; // Turn it off
previousMillis = currentMillis >= OnTime; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
delay(500);
led1State = HIGH; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(led1Pin, led1State); // Update the actual LED
}
else if ((buttonState == LOW) && (currentMillis - previousMillis >= OffTime))
{
led1State = LOW; // turn it on
previousMillis = currentMillis >= OffTime; // Remember the time
digitalWrite(led1Pin, led1State); // Update the actual LED
delay(300);
ledState = LOW; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
I have understood the contents of your post as follows:
1. You press K1 and hold ; as a result, LED13 will be ON instantly; LED12 will be ON after 500 ms.
2. You release K1; as a result LED13 will remain for ON for (2x60x1000 - 300 = ) ms from the moment you have released K1; LED12 will be OFF 300 ms later.
3. The Modified Version of your Program codes.
Expanded Form:
// These variables store the flash pattern
// and the current state of the LED
int buttonPin = 2; //K1 is connected
int ledPin = 13;// LED138;// the number of the LED pin
//int ledState = LOW; // ledState used to set the LED
int led1Pin = 12;//LED127;// the number of the LED pin
//int led1State = LOW; // ledState used to set the LED
//int buttonState = LOW;
//unsigned long previousMillis = 0; // will store last time LED was updated
//long OnTime = 0; // milliseconds of on-time
//long OffTime = 2000; // milliseconds of off-time
unsigned long currentMillis;
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(ledPin, LOW);
digitalWrite(led1Pin, LOW);
}
void loop()
{
while(digitalRead(2) != LOW)
{
;
}
digitalWrite(ledPin, HIGH);
currentMillis = millis();
while(millis() - currentMillis != 500)
{
;
}
digitalWrite(led1Pin, HIGH);
while(digitalRead(2) != HIGH)
{
;
}
currentMillis = millis();
while(millis() - currentMillis != 119700)
{
;
}
digitalWrite(ledPin, LOW); // LEd13 OFF
currentMillis = millis();
while(millis() - currentMillis != 300)
{
;
}
digitalWrite(led1Pin, LOW);
}
Compact Form:
int buttonPin = 2; //K1 is connected
int ledPin = 13;// LED138;// the number of the LED pin
int led1Pin = 12;//LED127;// the number of the LED pin
unsigned long currentMillis;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(ledPin, LOW);
digitalWrite(led1Pin, LOW);
}
void loop()
{
while (digitalRead(2) != LOW)
{
;
}
digitalWrite(ledPin, HIGH);
callMillis(500);
digitalWrite(led1Pin, HIGH);
while (digitalRead(2) != HIGH)
{
;
}
callMillis(119700);
digitalWrite(ledPin, LOW); // LEd13 OFF
callMillis(300);
digitalWrite(led1Pin, LOW);
}
void callMillis(unsigned long x)
{
currentMillis = millis();
while (millis() - currentMillis != x)
{
;
}
}
Ok thanks very much this works well :)) did not know how to add the delay in millis for that delay led for on/off ...
and another question can i simply integrate the fade function for a third led to fade when on stays on for those 2min and fades out ???