Untitled
unknown
plain_text
a year ago
1.1 kB
9
Indexable
#include <HCSR04.h>
#define LED_PIN 4
HCSR04 hc(5, 6); //initialisation class HCSR04 (trig pin , echo pin)
const int buttonPin = 2;
const int buzzer = 3;
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
const int buttonPin = 2;
pinMode(LED_PIN, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop()
{
delay(60); //Use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Serial.println(hc.dist()); // return curent distance in serial
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(buttonPin, HIGH);
Serial.println(buttonState);
digitalWrite(LED_PIN, HIGH);
tone(buzzer, 1000);
}
else {
// turn LED off:
digitalWrite(buttonPin, LOW);
Serial.println(buttonState);
digitalWrite(LED_PIN, LOW);
noTone(buzzer);
}
}Editor is loading...
Leave a Comment