I am a complete newb on Arduino. I know my way around electronics and computers, did a bit of HTML and PHP programming, so I think I know the idea of programming. I bought a Arduino Starter Kit and have been trying stuff out.
And ofcourse, now someone asks me if I can make something: Basically it's making a button send a 500 miliseconds long 5 volts puls, no matter how long you hold the button. Until you release the button and press it again, then it should give a new puls for 500 miliseconds.
I used some examples, I send the puls to a LED to check if it works. I can make the LED turn on, no problem. But I don't know how to make the part where it only gives a pulse just yet.
Can anyone point me in the right direction or help me somehow? Thanks!
So I've been trying and came up with this code, but the LED is never going on. Any idea what I did wrong?
Many thanks in advance!
// constants won't change :
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const long interval = 5000; // interval, how long LED should be on (milliseconds)
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
// set timer (currentmillis), starts at start program unsigned long counter (millis)
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else
{
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
else
{
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I knocked a sketch together to do what you want, just to make sure I could do it.
Hint: I used two boolean flags, which I called lightIsOn and buttonPressedAlready, both initialised as false. Also logged the time the led was switched on as lightWentOnAt.
If you click the switch while the led is on, the period is not extended and if you hold the switch down too long it still goes off as expected.
You're welcome to the code if you like but I won't post it until you say so, in case you want to wrestle some more.
Does that mean I'm way of my sketch? In that case I would love to peek at your code. I've been wrestling some more after my post and I'm not getting further.
So shall I flash it up for a few minutes and take it away?
//http://forum.arduino.cc/index.php?topic=447149.0
byte butTon = 14;
bool lightIsOn = false;
bool buttonPressedAlready = false;
unsigned long lightWentOnAt;
unsigned long lightBeOnFor = 1000; //or whatever
void setup() {
// put your setup code here, to run once:
pinMode(butTon, INPUT_PULLUP); //button to ground, pullup makes it high normally, low when pressed
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
//if the button is newly pressed and the light is off:
if (!digitalRead(butTon) && !lightIsOn && !buttonPressedAlready) //button pressed but light not on yet
{
digitalWrite(LED_BUILTIN, HIGH);
lightIsOn = true;
lightWentOnAt = millis();
buttonPressedAlready = true;
}
//if the light is on but time has expired
if (lightIsOn && (millis() - lightWentOnAt > lightBeOnFor))
{
digitalWrite(LED_BUILTIN, LOW);
lightIsOn=false;
}
//if the button is not pressed
if (digitalRead(butTon))
{
buttonPressedAlready=false;
}
}
@keesvos - you apparently missed my advice to look at the state change detection example. You need to do something only when the switch BECOMES pressed, NOT when the switch IS pressed. There is a big difference.
You need to set previousMillis to some value, somewhere. I hate that name. An event occurred. It is the time that the event occurred that is of interest. Surely, you can come up with a name that reflects the event and the fact that the variable holds a time.
If you do that, I'm sure that you can see that checking whether a switch has become pressed, so you know whether to turn the LED on and record the time that that event happens, and checking to see if the LED has been on long enough are two completely independent activities.
OK, I'm going to figure out both possible sollutions, just te get more experience. I started with the direction PaulS pointed out.
PaulS: @keesvos - you apparently missed my advice to look at the state change detection example.
@PaulS, I thought I did, but now see I didn't. I've dove into int more and now have a new code setup. It's not working, the LED is going one when the button is pressed, but comparing is not done when the button is pressed. Somehow I'm not checking at the correct moment.
My personal time comparison shows I have to go to bed, so just post my code. I hope you can give me a hand.
I'll pick it up again tomorrow. Thanks.
And yes, I am going to do the trick for 2 LED's, so I've already put in some names for that.
// constants won't change :
const int button1 = 2; // the number of the first pushbutton pin
const int button2 = 4; // the number of the second pushbutton pin
const int led1 = 13; // the number of the first LED pin
const int led2 = 12; // the number of the second LED pin
const long interval = 500; // interval, how long LED should be on (milliseconds)
// variables will change:
int ledState1 = LOW; // ledState used to set the first LED
int ledState2 = LOW; // ledState used to set the second LED
int buttonState1 = 0; // current state of the first button
int buttonState2 = 0; // current state of the second button
int lastButtonState1 = 0; // previous state of the first button
int lastButtonState2 = 0; // previous state of the second button
unsigned long previousTime1 = 0; // previous state of the timer since first button is pressed
unsigned long previousTime2 = 0; // previous state of the timer since second button is pressed
void setup() {
// initialize the LED pins as an output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// initialize the pushbutton pins as an input:
pinMode(button1, INPUT);
pinMode(button1, INPUT);
}
void loop()
{
unsigned long currentTime = millis(); // set timer that starts as soon as Arduino is turned on
buttonState1 = digitalRead(button1); // read the state of the pushbutton value
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button wend from off to on:
if (currentTime - previousTime1 < interval) { // Compare current time minus time in previous loop run to interval
previousTime1 = currentTime; // Store current time for next compare
digitalWrite(led1, HIGH); // Put LED1 on
}
else {
digitalWrite(led1, LOW);
}
}
if (buttonState1 == LOW) {
digitalWrite(led1, LOW);
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState1 = buttonState1;
}