Qduino
Jump to navigation
Jump to search
#define trigPin 1
#define echoPin 0
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(10, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
pinMode(4, INPUT_PULLUP);
}
// the loop function runs over and over again forever
void loop() {
bool HIGH_LOW = digitalRead(4);
digitalWrite(10, HIGH_LOW);
digitalWrite(2, !HIGH_LOW);
ping();
}
void ping() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println(distance);
}
void blink() {
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(900); // wait for a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
