Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
titleArduino can detect the measured distance of a sensor and print it to the console
linenumberstrue
collapsetrue
#define trigPin 31
#define echoPin 33
//#define led 11
//#define led2 10
 
void setup() {
  Serial.println("Hello");
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
//  pinMode(led, OUTPUT);
//  pinMode(led2, OUTPUT);
}
 
void loop() {
  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;
  if (distance < 4) {  // This is where the LED On/Off happens
  //digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  //digitalWrite(led2,LOW);
}
  else {
    //digitalWrite(led,LOW);
    //digitalWrite(led2,HIGH);
  }
  if (distance >= 400 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(1);
}
Code Block
languagec#
titleFirst ultrasonic Arduino program
linenumberstrue
collapsetrue
#define trigPin 31
#define echoPin 33
//#define led 11
//#define led2 10
 
void setup() {
  Serial.println("Hello");
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
//  pinMode(led, OUTPUT);
//  pinMode(led2, OUTPUT);
}
 
void loop() {
  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;
  if (distance < 4) {  // This is where the LED On/Off happens
  //digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  //digitalWrite(led2,LOW);
}
  else {
    //digitalWrite(led,LOW);
    //digitalWrite(led2,HIGH);
  }
  if (distance >= 400 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(1);
}
Code Block
languagec#
titleFirst ultrasonic Arduino program
linenumberstrue
collapsetrue
#define trigPin 31
#define echoPin 33
//#define led 11
//#define led2 10
 
void setup() {
  Serial.println("Hello");
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
//  pinMode(led, OUTPUT);
//  pinMode(led2, OUTPUT);
}
 
void loop() {
  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;
  if (distance < 4) {  // This is where the LED On/Off happens
  //digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
  //digitalWrite(led2,LOW);
}
  else {
    //digitalWrite(led,LOW);
    //digitalWrite(led2,HIGH);
  }
  if (distance >= 400 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(1);
}