Good evening. I'm new to Arduino and working on my first project. It is a button that makes a doorbell buzz. I've copied the code from another sheet and have gone over it a few times. Had another person double check me. I believe I have an Arduino 101 board. Could someone see if my code is off? Thanks in advance!
Do you mean the code doesn't work as you expect, or do you just want someone to look it over before you try it?
edit: assuming a 101 (which I don't have) works like an Uno (which I do), that code should be fine provided you have a pulldown resistor on the input pin, and you need to be sure the i/o pin can handle the current the buzzer will draw.
It's easier to use the builtin pullUP (again, assuming a 101 works like an Uno), and rewire the pin to ground not to 5V. Then change the code to look for a low when pressed, see lines marked <<<<< below. (I also change the output pin to 13 just to "see" the buzzer as the Uno's led.)
int buttonPin = 3;
int buzzerPin = 13; //<<<<<<<<<<<<<<<<< led as pseudo-buzzer
void setup()
{
pinMode(buttonPin, INPUT_PULLUP); //set button as digital input <<<<<<<<<<<<<<<<<<< does 101 have these?
pinMode(buzzerPin, OUTPUT); //as buzzer as digital output
}
void loop()
{
if (digitalRead(buttonPin) == 0) //check button is pressed or not <<<<<<<<<<<<<<<<<< (look for a low)
{ digitalWrite(buzzerPin, HIGH); //pressed, then buzzer buzzes. }
}
else
{ digitalWrite(buzzerPin, LOW); //notpressed, then buzzer remains silent
}
}