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
}
}
How have you wired the switch? You are not using the internal pull-up resistor, so you need an external resistor. Do you have one? How is it wired?
How quickly did you press the switch after restarting the Arduino? That code will only turn the LED on it the switch is pressed within the 5 second window. After the 5 second window ends, the switch has nothing to do.
The switch is wired up using a pull up resistor. Tested on arduino button example.
Even if i press the button immediately i still get nothing. Ive it with a 15 second window too..
So you have the button pulled up - reads as High unless the button is pushed?
The buttonpush brings the signal low and prevents the time comparing from happening the way I read it.
A little "thinko" slipped in your code when adding the button press detection code: you give it a /very/ tight window for it to turn on / off: only if it's pressed exactly within 1.000 - 1.020s and off at exactly 6.000s to 6.020s. So, it is highly probable you are simply missing that window of opportunity.
You are on track now, but it is important to remember the state of the LED as well -- because you want to be able to check if it needs to turn off.
Let's unroll that pseudo code .. As it can only be on or off, you could use a boolean for the LED status, so at the top of the code, where the off_time is defined, also throw in this one:
unsigned long off_time;
boolean ledState = false;
Then, in your loop() code, there should be something like this;
if ((ledState) && (millis()>=off_time)) /* is it on and is it later or equal to off_time /
{
digitalWrite(ledPin,LOW);
ledState = false;
}
else if (!ledState) / is it off? */
{
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
ledState = true;
off_time = millis() + 5000;
}
}
This may behave more like you want it to.
To understand more of coding in general, you may find it interesting to observe the difference in behaviour if you change the
"else if (!ledState)"
to just
"else"
hugo75:
A little "thinko" slipped in your code when adding the button press detection code: you give it a /very/ tight window for it to turn on / off: only if it's pressed exactly within 1.000 - 1.020s and off at exactly 6.000s to 6.020s. So, it is highly probable you are simply missing that window of opportunity.
You are on track now, but it is important to remember the state of the LED as well -- because you want to be able to check if it needs to turn off.
Let's unroll that pseudo code .. As it can only be on or off, you could use a boolean for the LED status, so at the top of the code, where the off_time is defined, also throw in this one:
unsigned long off_time;
boolean ledState = false;
Then, in your loop() code, there should be something like this;
if ((ledState) && (millis()>=off_time)) /* is it on and is it later or equal to off_time /
{
digitalWrite(ledPin,LOW);
ledState = false;
}
else if (!ledState) / is it off? */
{
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
ledState = true;
off_time = millis() + 5000;
}
}
This may behave more like you want it to.
To understand more of coding in general, you may find it interesting to observe the difference in behaviour if you change the
"else if (!ledState)"
to just
"else"
Hope this helps!
Thanks so much dude. This is exactly what i was looking for and the code is simple enough to understand! Your the man!