Page tree

Versions Compared

Key

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

...

Code Block
languagec#
titleThe Arduino can detect the measured distance of a sensor and print it to the console
linenumberstrue
collapsetrue
/*
 * code from http://www.robodino.de/2011/12/ultraschall-distanz-sensor-hc-sr04.html
 */ 
 
#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#
titleThe Arduino can receive command via bluetooth and set an LED on or off
linenumberstrue
collapsetrue
#define trigPin 31
#define echoPin 33
//#define led 11
//#define led2 10char blueToothVal;           //value sent over via bluetooth
char lastValue;              //stores last state of device (on/off)
 
void setup() 
{
  Serial.println("Hello");
  Serial.begin (9600); 
  pinMode(trigPin13, OUTPUT);
}
 
 
void pinModeloop(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){
)
{
  if(Serial.available()>0)
  {//if there is data being recieved
    blueToothVal=Serial.read(); //read it
  }
  if (blueToothVal=='n')
  {//if value from bluetooth serial is n
    digitalWrite(13,HIGH);            //switch on LED
    if (lastValue!='n')
      Serial.println(F("LED is on")); //print LED is on
    lastValue=blueToothVal;
  }
  else if (blueToothVal=='f')
  {//if value from bluetooth serial is n
    digitalWrite(13,LOW);             //turn off LED
    if (lastValue!='f')
      Serial.println(F("OutLED ofis rangeoff"));
 //print }
LED is else {on
    Serial.print(distance)lastValue=blueToothVal;
    Serial.println(" cm");
  }
  delay(11000);
}
Code Block
languagec#
titleA computer can connect wirelessly to the Arduino and get the measured distance via bluetooth terminal
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);
}