const int ignitionPin = 2; // Pin connected to the ignition switch
const int relayPin = 3; // Pin connected to the relay module
const int resetPin = 4; // Pin connected to the reset button
unsigned long timer = 0;
bool timerStarted = false;
void setup() {
pinMode(ignitionPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(resetPin, INPUT_PULLUP);
digitalWrite(relayPin, LOW); // Ensure the buzzer is off initially
}
void loop() {
// Detect if the ignition is turned off
if (digitalRead(ignitionPin) == LOW && !timerStarted) {
timer = millis();
timerStarted = true;
}
// Check if 3 minutes have passed
if (timerStarted && millis() - timer >= 180000) { // 3 minutes = 180000 milliseconds
digitalWrite(relayPin, HIGH); // Activate the relay (turn on the buzzer)
}
// Check if the reset button is pressed
if (digitalRead(resetPin) == LOW) {
digitalWrite(relayPin, LOW); // Deactivate the relay (turn off the buzzer)
timerStarted = false; // Reset the timer
}
}
My Code When Im verify
Sketch uses 1088 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 14 bytes (0%) of dynamic memory, leaving 2034 bytes for local variables. Maximum is 2048 bytes.
what is the issue Of This any one can reply with perfect code please
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
Once Driver Will Of The Ignition in car or Bus After Three Minutes Need Buzzer Sound Loudly untill driver press the reset button Buzzer Sounds is active
and It repeats Daily
i Bring All Componets
Just Programing pending Can you give a solution To Clear this one
If you add some Serial.print() statements to the sketch you will be able to see what it is doing and what the state of the input and output pins are even if you do not add any hardware to the Nano
As an alternative you could copy the sketch to the Wokwi emulator website and "build" the project there to test it
Your sketch works but has a flaw. Because when you press the button
timerStarted = false; // Reset the timer
the ignition is still off, but somehow the system is powered (you are hearing the relay controlled alarm!), so when your loop hits this test again three minutes later, it turns on the alarm.
if (digitalRead(ignitionPin) == LOW && !timerStarted) {
You must write code that starts the alarm but once, and figure out if it is power down (reset) that starts things anew, or add more code to figure out when it would be appropriate to alarm again, say require that the ignition input be seen as HIGH before it being LOW can alarm you.