Arduino code not working

My code isn’t working I would like it to do the following:

Sound an alarm and flash a light if a seatbelt is fastened and the ignition is not on. It begins by setting up the pins for the buzzer, light seatbelt sensor (magnetic reed switch sensor module) and ignition sensor (Hall effect`Use sensor module). The loop then checks if the seatbelt is fastened and the ignition is off. If both are true, it waits for 20 seconds before entering a for loop that alternates the buzzer and light on and off with a 500ms delay between each change. After that for loop, it enters a while loop that continues to sound the alarm and flash the light until the seatbelt is unfastened. The buzzer and light should only go on if it indicates the above.

However right now it has the light on and buzzer sounding when it’s not meant to when I plug into the computer and when I plug it into the battery it doesn’t do anything.

Here is my code
i

nt buzzerPin = 3;
int lightPin = 6;
int seatbeltPin = 2;
int ignitionPin = 4;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(lightPin, OUTPUT);
  pinMode(seatbeltPin, INPUT_PULLUP);
  pinMode(ignitionPin, INPUT_PULLUP);
  digitalWrite(buzzerPin, LOW);
  digitalWrite(lightPin, LOW);
}

void loop() {
  // Check if the seatbelt is fastened and ignition is off
  if (digitalRead(seatbeltPin) == LOW && digitalRead(ignitionPin) == HIGH) {
    // Wait for 20 seconds before sounding the alarm
    delay(20000);
    // Blink the light and sound the buzzer 10 times
    for (int i = 0; i < 10; i++) {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(lightPin, HIGH);
      delay(500);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(lightPin, LOW);
      delay(500);
    }
    // Keep sounding the alarm and flashing the light until the seatbelt is unfastened
    while (digitalRead(seatbeltPin) == LOW) {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(lightPin, HIGH);
      delay(500);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(lightPin, LOW);
      delay(500);
    }
  }
  // If the seatbelt is not fastened or the ignition is on, turn off the buzzer and light
  else {
    digitalWrite(`buzzerPin,` LOW);
    digitalWrite(lightPin, LOW);
  }
}`

`



I don’t know whether my code is wrong or the setup is wrong please let me know asap thanks.

Hi @ashac,

there is a <code/> symbol in the post editor:

image

Please format your code and post it inside the tags that appear when you press this button!

It should look like this then:

int buzzerPin = 3;
int lightPin = 6;
int seatbeltPin = 2;
int ignitionPin = 4;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(lightPin, OUTPUT);
  pinMode(seatbeltPin, INPUT_PULLUP);
  pinMode(ignitionPin, INPUT_PULLUP);
  digitalWrite(buzzerPin, LOW);
  digitalWrite(lightPin, LOW);
}

void loop() {
  // Check if the seatbelt is fastened and ignition is off
  if (digitalRead(seatbeltPin) == LOW && digitalRead(ignitionPin) == HIGH) {
    // Wait for 20 seconds before sounding the alarm
    delay(20000);
    // Blink the light and sound the buzzer 10 times
    for (int i = 0; i < 10; i++) {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(lightPin, HIGH);
      delay(500);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(lightPin, LOW);
      delay(500);
    }
    // Keep sounding the alarm and flashing the light until the seatbelt is unfastened
    while (digitalRead(seatbeltPin) == LOW) {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(lightPin, HIGH);
      delay(500);
      digitalWrite(buzzerPin, LOW);
      digitalWrite(lightPin, LOW);
      delay(500);
    }
  }
  // If the seatbelt is not fastened or the ignition is on, turn off the buzzer and light
  else {
    digitalWrite(buzzerPin, LOW);
    digitalWrite(lightPin, LOW);
  }
}

That's easier to read and handle in the forum!

Your pictures are nice and give some information but a drawing of your setup would be much more helpful ... :wink:

1 Like

I have copied your sketch to Wokwi:

You make intensive use of delay()! That blocks your code too much! It cannot react on changes.

(As you may use an active buzzer and Wokwi provides only a passive one, it will not make sound ...)

Can you explain that i don’t understand

Hello ashac

Post a schematic too

I dont have a schematic

sorry can you explain this I dont understand what you mean

How can I fix this

Use the "Blink without Delay" example to be found in the IDE to gain the knowledge.

1 Like

Where is this

Yhe example is in the IDE.

Here is a way how it works:

constexpr int buzzerPin = 3;
constexpr int lightPin = 6;
constexpr int seatbeltPin = 2;
constexpr int ignitionPin = 4;
constexpr unsigned long waitBeforeAlarm = 2000;

unsigned long lastTimeBlinked = 0;
unsigned long alarmTime       = 0;
unsigned long blinkInterval  = 500;
boolean       alarmActive = false;
boolean       doAlarm     = false;


void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(lightPin, OUTPUT);
  pinMode(seatbeltPin, INPUT_PULLUP);
  pinMode(ignitionPin, INPUT_PULLUP);
  digitalWrite(buzzerPin, LOW);
  digitalWrite(lightPin, LOW);
}

void loop() {
  // Check if the seatbelt is fastened and ignition is off
  boolean giveAlarm = (digitalRead(seatbeltPin) == LOW && digitalRead(ignitionPin) == HIGH);
  if (!alarmActive && giveAlarm) {
    alarmActive = true;
    alarmTime = millis();
  } 
  if (alarmActive && !giveAlarm) {
    digitalWrite(buzzerPin, LOW);
    digitalWrite(lightPin, LOW); 
    alarmActive = false;
    doAlarm = false;
    }
  if (alarmActive && millis()-alarmTime > waitBeforeAlarm) {
      doAlarm = true;    
    }
  if (doAlarm) {
    if (millis() - lastTimeBlinked > blinkInterval){
      lastTimeBlinked = millis();
      int buzz   = !digitalRead(buzzerPin);
      int light  = !digitalRead(lightPin);
      digitalWrite(buzzerPin, buzz);
      digitalWrite(lightPin, light); 
    }
  }
}


You can check it out on Wokwi:

The left slider is connected to the ignitionPin, the right one to seatbeltPin.

Are you sure you want to alarm using these conditions?

// Check if the seatbelt is fastened and ignition is off
// Keep sounding the alarm and flashing the light until the seatbelt is unfastened
// If the seatbelt is not fastened or the ignition is on, turn off the buzzer and light

waitBeforeAlarm is set to 2000 msec for test purposes...

How did you get to all those wires then?

Where is Arduino Pin 5 (GREEN WIRE) going? Transistor emitter?

One of the transistor legs is not connected on your breadboard.
Sketch out a rough, hand drawn, schematic showing the intended wiring so we can compare with the actual wiring.
Its hard to tell if it's right if we don't know what it's supposed to be! :smile:

Tom.. :smiley: :+1: :coffee: :australia:

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