So, this is the code that I am working on for my project. The IDE said the program is running well, but I see no result in my actual project. I am a beginner btw, so bear with me, guys, and I'd be grateful for your help.
const int ledGreen = 11;
const int ledRed = 10;
const int pButton = 12;
int greenState = HIGH;
int redState = 0;
int buttonCurrent = 0;
int buttonPrev = LOW;
const int buzzer = 3;
const int soundPin = A1;
const int thresh = 200;
void setup() {
// put your setup code here, to run once:
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(pButton, INPUT);
pinMode(soundPin, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonCurrent = digitalRead(pButton);
int soundSens = analogRead(soundPin);
if (buttonCurrent == HIGH && buttonPrev == LOW){
if (greenState == HIGH && redState == LOW){
digitalRead(greenState = LOW);
digitalRead(redState = HIGH);
if (soundSens>=thresh){
tone(buzzer,100);
delay (5000);
noTone(buzzer);
delay(200);
}
else{
noTone(buzzer);
}
}
else{
greenState = HIGH;
redState = LOW;
}
}
digitalWrite(ledGreen, greenState);
digitalWrite(ledRed, redState);
buttonPrev = buttonCurrent;
delay (100);
}
const int ledGreen = 11;
const int ledRed = 10;
const int pButton = 12;
int greenState = HIGH;
int redState = 0;
int buttonCurrent = 0;
int buttonPrev = LOW;
const int buzzer = 3;
const int soundPin = A1;
const int thresh = 200;
void setup() {
// put your setup code here, to run once:
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
pinMode(pButton, INPUT);
pinMode(soundPin, INPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
buttonCurrent = digitalRead(pButton);
int soundSens = analogRead(soundPin);
if (buttonCurrent == HIGH && buttonPrev == LOW){
if (greenState == HIGH && redState == LOW){
digitalRead(greenState = LOW);
digitalRead(redState = HIGH);
if (soundSens>=thresh){
tone(buzzer,100);
delay (5000);
noTone(buzzer);
delay(200);
}
else{
noTone(buzzer);
}
}
else{
greenState = HIGH;
redState = LOW;
}
}
digitalWrite(ledGreen, greenState);
digitalWrite(ledRed, redState);
buttonPrev = buttonCurrent;
delay (100);
}