Help me please im stuck from 5 days

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

Im using Arudiono Nano

Welcome to the forum

Why did you post in the Remote Learning section of the Emergency Response category of the forum ?

Your topic has been moved to the Programming Questions category

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

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.

Why do you think that there is something wrong with your sketch ? No errors or warnings are issued when it is compiled

Does the sketch not do what you want ?

Sir You Mean Code Is Ok Right ? its Stored On Board I can Start The wiring

The code compiles so has no syntax errors but you have not described what it should do so it is impossible to say whether it will do what you want

Sir Actually I got One requriment

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

ok Sir Thanks I try One Time Emulator

Please stop using random capital letters in your posts. It makes them more difficult to read

It may take you more than One Time. :wink:

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.

Who wrote the code you have presented?

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.