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);
}
}`
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 ...
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...
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!