Hello everyone. I hope you all are doing great. I ran into a problem when I was trying to write a program for a airsoft scenario. My goal is to have a program that when the blue teams button is pushed their light comes on and a buzzer starts going off for two minutes then stops and once it stops that means the team won. If the person stops pushing the button then the timer is immediately reset and they would have to start over. I got the lights to work somewhat but the buzzer doesn't work at all. I finally decided to ask the experts here on what I did wrong. Hope you can help me figure it out. I am pretty sure it is a simple mistake but I have only been coding for a little bit and can't find out where I errored. I also included a schematic that I drew up, hope you can understand it and if not I will remake it. Thanks in advance. Here is the code:
const int buttonPinblueTeam = 3; // the number of the pushbutton pin
const int buttonPinredTeam = 5;
const int ledPinblueTeam = 13; // the number of the LED pin
const int ledPinredTeam = 12;
int buzzerPin = 11;
int ledStateblueTeam = LOW; // the current state of the output pin
int ledStateredTeam = LOW;
int buttonStateblueTeam = 0; // the current reading from the input pin
int buttonStateredTeam = 0;
int buzzerPinstate = LOW;
void setup() {
pinMode(buttonPinblueTeam, INPUT);
pinMode(buttonPinredTeam, INPUT);
pinMode(ledPinblueTeam, OUTPUT);
pinMode(ledPinredTeam, OUTPUT);
pinMode(buzzerPinstate, OUTPUT);
}
void loop() {
buttonStateblueTeam = digitalRead(buttonPinblueTeam);
buttonStateredTeam = digitalRead(buttonPinredTeam);
if (buttonStateblueTeam == HIGH) {
digitalWrite(ledPinredTeam, LOW);
digitalWrite(ledPinblueTeam, HIGH);
for (int i = 0; i <= 5; i++) {
buzzerPinstate = HIGH;
delay(1000);
buzzerPinstate = LOW;
if (buzzerPinstate > 140) { // bail out on sensor detect
i = 0;
break;
}
}
if (buttonStateblueTeam == LOW) {
digitalWrite(ledPinblueTeam, LOW);
digitalWrite(buzzerPinstate, LOW);
}
}
if (buttonStateredTeam == HIGH) {
digitalWrite(ledPinblueTeam, LOW);
digitalWrite(ledPinredTeam, HIGH);
int threshold = 140;
for (int i = 0; i <= 5; i++) {
buzzerPinstate = HIGH;
delay(1000);
buzzerPinstate = LOW;
if (buzzerPinstate > 140) { // bail out on sensor detect
i = 0;
break;
}
}
if (buttonStateredTeam == LOW) {
digitalWrite(ledPinredTeam, LOW);
digitalWrite(buzzerPinstate, LOW);
}
}
// set the LED:
digitalWrite(ledPinblueTeam, ledStateblueTeam);
digitalWrite(ledPinredTeam, ledStateredTeam);
digitalWrite(buzzerPin, buzzerPinstate);
}
First, Karma for a very good first post. Nice schematic, code in code tags and good information.
I don't see pullups on your switches. Since they are wired to ground you could use the internal pullups by setting their pinModes to INPUT_PULLUP. The switches will read HIGH when not pressed and LOW when pressed.
don't gonna write such an aggressive username.
IMHO this world needs more peace not more aggression.
You may say using bruisin-guns is better than lethal weapons.
IMHO still on the wrong side.
As human race we have the capability to not do negative energy
BTW this documentation is a challenge to not fall into prejudice by what you see and your mind tries to conclude as a first impression.
Some types of buzzer work only if they are driven at a frequency in the low audio range (say 1-2 kHz) and the tone() library can be used for this.
During the delay() statement in your sketch, no button can be read.
GRuser:
on code I see "buzzerpin 11"
on schematic buzzer is connected to pin 10
have a look on that
Sorry GRuser, the buzzer was on pin 10 but I changed it to 11 and forgot to change it in the sketch. And groundFungus, I used two push buttons like the one in the picture below but I couldn't find anything on the schematic that looked like them. Sorry if that made it confusing. The buttons work because it turns the LEDs on just not the buzzer. Thanks so far everyone.
Also StefanL38, no one said you had to like a persons user name and no one said you had to say it. It is just a user name that I use when I don't want my real name blasted all over the internet. I can think of a lot worse names that people have used as user names. Please don't be rude and this topic was posted to see if I can get the codding fixed. Not for someone to talk about my user name. Please have a great rest of your day.
In your loops you set buzzerpinstate high and low with a delay. I assume that's where you're expecting the buzzer to sound. It won't though because you have missed some corresponding digitalWrite statements.
Hey everyone, thanks for your tips on why it might not be working. The problem was I forgot to add a few digitalWrites(). As I was thinking about it I also decided that I didn't need two buttons/lights since only one team will be at the "flag" and the other one will be attacking. I might make some changes to it to make it better. Here is the finished code and updated schematic:
// constants won't change. They're used here to set pin numbers:
const int buttonPinblueTeam = 3; // the number of the pushbutton pin
const int ledPinblueTeam = 13; // the number of the LED pin
int buzzerPin = 11;
// Variables will change:
int ledStateblueTeam = LOW; // the current state of the output pin
int buttonStateblueTeam = 0; // the current reading from the input pin
void setup() {
pinMode(buttonPinblueTeam, INPUT);
pinMode(ledPinblueTeam, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
buttonStateblueTeam = digitalRead(buttonPinblueTeam);
if (buttonStateblueTeam == HIGH) {
digitalWrite(ledPinblueTeam, HIGH);
for (int i = 0; i <= 5; i++) {
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
if (buzzerPin > 140) { // bail out on sensor detect
i = 0;
break;
}
}
if (buttonStateblueTeam == LOW) {
digitalWrite(ledPinblueTeam, LOW);
digitalWrite(buzzerPin, LOW);
}
}
// set the LED:
digitalWrite(ledPinblueTeam, ledStateblueTeam);
}
The switch is wired to the ground, input, and hot. I was trying to use the example code from the Break Arduino tutorial. (link below) If you don't want to go through the link it is the break tutorial in the category control structure. After I found that the buzzer kept going off after 2 minutes I played around with it and now it says:
if ( i > 120) {
the For before that now says:
for (int i = 0; i <= 1; i++) {
Did I do that right?
I am using this type of button:
How can a two terminal device be connected to three things?
In the Debounce tutorial it shows that you can.
I added code to flash the led when the 2 minutes are up. Updated code:
// constants won't change. They're used here to set pin numbers:
const int buttonPinblueTeam = 3; // the number of the pushbutton pin
const int ledPinblueTeam = 13; // the number of the LED pin
int buzzerPin = 11;
// Variables will change:
int ledStateblueTeam = LOW; // the current state of the output pin
int buttonStateblueTeam = 0; // the current reading from the input pin
void setup() {
pinMode(buttonPinblueTeam, INPUT);
pinMode(ledPinblueTeam, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
buttonStateblueTeam = digitalRead(buttonPinblueTeam);
if (buttonStateblueTeam == HIGH) {
digitalWrite(ledPinblueTeam, HIGH);
for (int i = 0; i <= 1; i++) {
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(500);
digitalWrite(ledPinblueTeam, LOW);
delay(500);
digitalWrite(ledPinblueTeam, HIGH);
if (i > 120) { // bail out on sensor detect
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPinblueTeam, LOW);
delay(250);
digitalWrite(ledPinblueTeam, HIGH);
delay(250);
digitalWrite(ledPinblueTeam, LOW);
delay(250);
digitalWrite(ledPinblueTeam, HIGH);
delay(250);
digitalWrite(ledPinblueTeam, LOW);
delay(250);
digitalWrite(ledPinblueTeam, HIGH);
delay(250);
digitalWrite(ledPinblueTeam, LOW);
delay(250);
digitalWrite(ledPinblueTeam, HIGH);
delay(250);
i = 0;
break;
}
}
if (buttonStateblueTeam == LOW) {
digitalWrite(ledPinblueTeam, LOW);
digitalWrite(buzzerPin, LOW);
}
}
// set the LED:
digitalWrite(ledPinblueTeam, ledStateblueTeam);
}
sniper02:
In the Debounce tutorial it shows that you can.
No it doesn't. I'm sure you misunderstood whatever you read there.
You have a loop where 'i' gets the values 0 to 1. Then you test to see if it's greater than 120. How can that ever work?
By the way, modifying the value of for loop variables inside the for loop, is considered a bad programming practice. If you have to do it, it usually means you didn't structure your loops efficiently. It might take a nested loop of some kind to do it, but it would be safer and easier to understand.
Sorry, I didn't know how to draw it up in the schematic. Never did anything like that before. Well that is how I have my button set up. Again I am sorry.
sniper02:
Sorry, I didn't know how to draw it up in the schematic. Never did anything like that before. Well that is how I have my button set up. Again I am sorry.
What about simply drawing it the same way the same tutorial draws it?