PaulS:
const int ledPin = 13; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
{
ledState = HIGH;
interval = 30;
}
else
{
ledState = LOW;
interval = 1000;
}
digitalWrite(ledPin, ledState);
}
}
When the pin is to be turned on, assign interval one value. When it is to be turned off, assign it another value. There is nothing in the example or philosophy that says that interval needs to be a constant.
Thanks a lot Pauls..
Now I have a problem..
The main reason why I wanted blink without delay..
I am showing you the code with the delay .
void setup(){
pinMode(13, OUTPUT);
pinMode(2, INPUT);
digitalWrite(2, HIGH);
}
void loop(){
if(digitalRead(2) == LOW){
digitalWrite(13, HIGH);
delay(20);
digitalWrite(13, LOW);
delay(20);
}
else{
digitalWrite(13, HIGH);
delay(30);
digitalWrite(13, LOW);
delay(1000);
}
}
In the code above I want the led to blink rapidly when PIN 2 is LOW.. else blink for 30 sec at 1 sec interval..
The problem comes in here that when the led just glew and went off and I make PIN2 LOW.. The led does not start to blink rapidly instantly..
It waits for the delay of 1 second to complete and then starts the rapid blinking if() loop..
How do I eliminate this..??