This code turns on the led for 5 seconds then switches the led off but i have a problem...
const int ledPin = 13; // the number of the LED pin
unsigned long time;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin,LOW);
}
void loop()
{ time= millis();
if (time >= 1000 && time < 1020){digitalWrite(ledPin,HIGH);}; // turn it on when its been running for a second
if (time >= 6000 && time < 6020){digitalWrite(ledPin,LOW);}; // turn it off when its been running for 6 seconds
// write rest of code here . as written it will repeat about every 49 days when millis() 'goes round the clock'
}
When i add a button press to this it no longer works?
What am i missing people?
const int buttonPin = 2; // the number of the pushbutton pin
const int ledOdd = 13; // the number of the LED pin
const int ledPin = 50;
// variables will change:
int buttonstate = 0;
// variable for reading the pushbutton status
int buttonPress = 0;
int buttonState = 0;
int ledState = LOW;
int ledEoff = LOW;
// ledState used to set the LED
long previousMillis = 0;
long interval = 1000;
long intervalTen = 10000;
unsigned long time;
void setup() {
// initialize the LED pin as an output:
pinMode(ledOdd, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop(){
buttonState = digitalRead(buttonPin);
time= millis();
if(buttonState == HIGH) {
if (time >= 1000 && time < 1020){digitalWrite(ledPin,HIGH);}; // turn it on when its been running for a second
if (time >= 6000 && time < 6020){digitalWrite(ledPin,LOW);}; // turn it off when its been running for 6 seconds
}
}