...
Code Block |
---|
language | c# |
---|
title | The Arduino can detect the measured distance of a sensor and print it to the console |
---|
linenumbers | true |
---|
collapse | true |
---|
|
/*
* code partly 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;
// To trigger the digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this linesensor a 10us HIGH signal is needed.
// Create definite LOW for 2us
digitalWrite(trigPin, HIGHLOW);
// delayMicroseconds(10002); - Removed this line
delayMicroseconds(10); // AddedGive thisit line
a HIGH digitalWrite(trigPin, LOW);for 10us
duration = pulseIn(echoPindigitalWrite(trigPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) {delayMicroseconds(10);
// This is where the LED On/Off happensLOW again
//digitalWrite(ledtrigPin,HIGH LOW);
// WhenMeasure the Redpulse conditionwidth isof met, the Greenincoming LEDsignal
should turnduration off
//digitalWrite(led2,LOW= pulseIn(echoPin, HIGH);
}
// elseCalculate {
the distance with //digitalWrite(led,LOW);given function
distance = (duration//digitalWrite(led2,HIGH);
}2) / 29.1;
if (distance >= 400 || distance <= 0) {
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
// Wait a microsecond to start again
delay(1);
} |
Code Block |
---|
language | c# |
---|
title | The Arduino can receive command via bluetooth and set an LED on or off |
---|
linenumbers | true |
---|
collapse | true |
---|
|
/*
* code partly from http://www.arduino-hacks.com/adding-bluetooth-capability-project-arduino-hc-06/
*/
char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)
void setup()
{
Serial.begin(9600);
void setup() {
// Start a bluetooth connection via Serial1 ports
Serial.begin(9600);
// User the LED on pin 13
pinMode(13,OUTPUT);
}
void loop()
{ {
// If data over Serial1 are received
if(Serial.available()>0) {
{//if thereRead isthe data being recieved
blueToothVal=Serial.read();
}
//read it
} If the data equal the letter 'n'
if (blueToothVal=='n') {
{//if valueSwitch from bluetooth serial is nLED on
digitalWrite(13,HIGH); //switch on LED
if (lastValue!='n')
Serial.println(F("LED is on")); //print LED is on
lastValue=blueToothVal;
}}
// Else if the data equal the letter 'f'
else if (blueToothVal=='f') {
{ //if valueSwitch from bluetooth serial is nLED off
digitalWrite(13,LOW); //turn off LED
if (lastValue!='f')
Serial.println(F("LED is off")); //print LED is on
lastValue=blueToothVal;
}
delay(1000}
// Wait 100ms
delay(100);
} |
Code Block |
---|
language | c# |
---|
title | A computer can connect wirelessly to the Arduino and get the measured distance via bluetooth terminal |
---|
linenumbers | true |
---|
collapse | true |
---|
|
#define trigPin 31
#define echoPin 33
char blueToothVal; //value sent over via bluetooth
void setup()
{
Serial.begin(9600);
//pinMode(13,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
long getDistance() {
long duration, distance;
digitalWrite(13,HIGH); //switch on LED
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 400 || distance <= 0){
distance = getDistance();
}
return distance;
}
void loop()
{
if(Serial.available()>0)
{
blueToothVal=Serial.read(); //read it
}
if (blueToothVal=='x')
{
Serial.print(getDistance());
Serial.println(" cm");
}
// Reset blueToothVal to
any letter but }'x'
blueToothVal = 'c';
delay(1000100);
} |
The Bluetooth module is found on the MacBook Pro as HC-06 in the list of available Bluetooth devices. A connection can be established with Code '1234'.
...