Hi all I need some help and I'm fairly new to the Arduino world.
Basically, I need to detect a signal from a momentary switch and then put a LED on for 5 sec, every time a push at the button is detected. (This led is in real life a water pump that turns on and off for 5 seconds - but for simplicity, we will use an LED)
Everything works fine when the Arduino is running however I need to be able to turn the power on / off for the Arduino once in a while to save my battery usage. this is where my problem begins.
Every time I reset/boot my Arduino the LED runs for 5 sec and then stops. In other words it runs the code without the button being pushed. How can I avoid this ?
If it is because of floating pins I must be doing something wrong, because I am using pull-down resistors. (see attached picture).
Pull Down resistor: 10K ohm
Resistor before LED: 330 ohm
Here is my code:
int LEDPin=30; // Signal to LED
int ButtonPin=26; // input for reading on momentary switch
int buttonRead; // momentary switch value (0=LOW) (1=HIGH)
unsigned long timingPeriod;
unsigned long ElapsedTime;
void setup() {
Serial.begin(9600);
pinMode(ButtonPin, INPUT);
pinMode(LEDPin,INPUT);
}
void loop() {
//----------------------------------------------------------------//
buttonRead=digitalRead(ButtonPin);
Serial.println(buttonRead);
if (buttonRead==0)
digitalWrite (LEDPin,LOW);
else if (buttonRead==1)
timingPeriod = millis();
ElapsedTime = (millis() - timingPeriod);
if (ElapsedTime <= 5000)
digitalWrite (LEDPin,HIGH);
else
digitalWrite (LEDPin,LOW);
//---------------------------------------------------------------//
}
During the first five seconds, you need to know whether the button was pressed. Not whether it IS pressed, but whether it has been. When you detect a press, you set timingPeriod. You can use that as an indication. In your if that checks elapsed time, also check that timingPeriod is not zero.
Is this the only function of the Arduino?
This function can be achieved much faster, easier, and cheaper using a delay relay module with a 555 timer on it.
Should only cost a dollar or two.
int LEDPin=30; // Signal to LED
int ButtonPin=26; // input for reading on momentary switch
int buttonRead; // momentary switch value (0=LOW) (1=HIGH)
unsigned long timingPeriod;
unsigned long ElapsedTime;
void setup() {
Serial.begin(9600);
pinMode(ButtonPin, INPUT);
pinMode(LEDPin,OUTPUT); // <-- You are OUTPUTING a high, don't use INPUT
}
void loop() {
//----------------------------------------------------------------//
buttonRead=digitalRead(ButtonPin);
Serial.println(buttonRead);
if (buttonRead==0)
// While not needed for a single line, IT does make it easier to read
{
digitalWrite (LEDPin,LOW);
}
else if (buttonRead==1)
// Absolutely needed, or only the first line would be run.
{
timingPeriod = millis();
ElapsedTime = (millis() - timingPeriod);
if (ElapsedTime <= 5000)
{
digitalWrite (LEDPin,HIGH);
}
else
{
digitalWrite (LEDPin,LOW);
}
//---------------------------------------------------------------//
}
}