Check out our code below:
const int BEEP = 9; // the pin for the beeperconst int GREEN = 8; const int SENSOR = 7; // the input pin where the pushbutton is connected int val = 0; // val will be used to store the state of the input pin void setup() {
pinMode(BEEP, OUTPUT); // tell Arduino LED is an output pinMode(SENSOR, INPUT); // and BUTTON is an input } void loop(){ val = digitalRead(SENSOR); // read input value and store it
// check whether the input is HIGH (button pressed)
if (val == HIGH) { digitalWrite(GREEN, LOW); tone(9, 2800); // turn LED ON } else {
noTone(9); if (val == LOW) { digitalWrite(GREEN, HIGH); // turn LED ON
} else {
digitalWrite(GREEN, LOW); } } }

No comments:
Post a Comment