I am doing a project with alarm
if Rightbutton is press
In 15 sec led1 will light 1
after 15sec and led1 light up
user can press Left button to reset everything
side note I used 1k resistor and solder it physically so I didn't use any pullup resistor
but I got this problem led1 light up immediately when I power on and didn't even press any button
int pRightButton = 7;
int pLeftButton = 8;
int ledPin1 = 2;
int ledPin2 = 3;
bool status = false;
unsigned long startTime;
unsigned long beginTime;
unsigned long duration;
int RightbuttonStatus = 0;
int LeftbuttonStatus = 0;
void setup() {
pinMode(pRightButton,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(pLeftButton,INPUT);
pinMode(ledPin2,OUTPUT);
Serial.begin(9600);
}
void loop()
{
startTime = millis();
int RightbuttonStatus = digitalRead(pRightButton);
int LeftbuttonStatus = digitalRead(pLeftButton);
if (RightbuttonStatus == HIGH)
{
status = true;
}
if (status == true) //I do like so that if user release the button the led1 will still light up
{
alarm();
}
}
void alarm()
{
beginTime = millis();
duration = (startTime - beginTime);
if (duration == 15000) // after 15ec led1 ON
{
digitalWrite(ledPin1, HIGH);
}
if (duration == 15000 && LeftbuttonStatus == HIGH )// after 15sec and left button press to reset
{
status = false;
}
}
You should not have int at the start of lines 26 and 27 because you have already defined those variables before setup(). Having int at the start of those lines creates two new local variables with the same names which only exist within loop()
Robin2:
You should not have int at the start of lines 26 and 27 because you have already defined those variables before setup(). Having int at the start of those lines creates two new local variables with the same names which only exist within loop()
...R
ok I tried to remove those int but it didn't solve the problem
Alextak:
but I got this problem led1 light up immediately when I power on and didn't even press any button
That may be unrelated to your code. When you set the pin to output mode, it will default to LOW. Depending on how you have the LED connected, a LOW may turn the LED ON, and a HIGH turn it OFF.
As for the code, the timer needs to start when the button input changes from LOW to HIGH, not when it is HIGH. See the tutorial here: State Change Detection
When you detect the button has been pressed, then record the current time and set a flag to show that the timer is running. Then you can test if the timer is running AND if the 15 seconds have elapsed, and if so turn the LED on and check to see if it needs to be turned back off.
boylesg:
Immediately I can spot 'startTime = millis();' in your loop() function as a problem.
If you are using this to determine when your alarm is triggered then it is reset every time loop() is called.
And loop() is called repeatedly regardless of your alarm.
You need to do something like this (ignoring all the rest of your code)
uint32_t nStartMillis = 0;
const uint32_t nTriggerMillis = 100;
void setup()
{
// Set the timer
nStartMillis = millis();
}
void loop()
{
if ((millis() - nStartMillis) >= nTriggerMillis)
{
// Do timer stuff
// Reset the timer
nStartMillis = millis();
}
}
I.E. You should only reset a timer once the timer is triggered and you have done what you need to do in response to it.
You are resetting your timer at the top of your loop() function, meaning that __*if ((millis() - nStartMillis) >= nTriggerMillis)*__ will never be true.
I tried to edit according to your code and remove the resetting part just for now
however, the problem is still same
//updated code Ver2
int pRightButton = 7;
int pLeftButton = 8;
int ledPin1 = 2;
int ledPin2 = 3;
bool status = false;
uint32_t nStartMillis = 0;
const uint32_t nTriggerMillis = 15000;
int RightbuttonStatus = 0;
int LeftbuttonStatus = 0;
void setup() {
pinMode(pRightButton,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(pLeftButton,INPUT);
pinMode(ledPin2,OUTPUT);
Serial.begin(9600);
nStartMillis = millis();
}
void loop()
{
int RightbuttonStatus = digitalRead(pRightButton);
int LeftbuttonStatus = digitalRead(pLeftButton);
if (RightbuttonStatus == HIGH)
{
status = true;
}
if (status = true)
{
alarm();
}
}
void alarm()
{
if ((millis() - nStartMillis) >= nTriggerMillis)
{
digitalWrite(ledPin1, HIGH);
}
}
Alextak:
I tried to edit according to your code and remove the resetting part just for now
however, the problem is still same
The revised program in Reply #5 is still creating local variables with the same names as the global variables.
If the problem is that the LED1 is ON when you don't want it try putting digitalWrite(ledPin1, LOW); immediately after setting the mode to OUTPUT.
If that does not work then you need to make a simple pencil drawing showing how everything is connected and post a photo of the drawing. See this Simple Image Guide