You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 17
Next »
Mon. 11.05.15 - 14:43 (fma)
Ultrasonic sensors and bluetooth bridge have been delivered. Communication with Arduino is working.
Code:
/*
* 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 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);
pinMode(13,OUTPUT);
}
void loop()
{
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("LED is off")); //print LED is on
lastValue=blueToothVal;
}
delay(1000);
}
#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");
}
blueToothVal = 'c';
delay(1000);
}
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'.
Click here to show monitor approach...
In Terminal a list of all open serial ports can be shown via ' ls /dev/tty.* '.
' /dev/tty.HC-06-DevB ' should be shown in the list. A serial console to the module can be established via ' screen /dev/tty.HC-06-DevB '.
Update:
To close a running screen, press ctrl-a, afterwards crtl-\ (Yes: on German keyboard: ctrl-alt-shift-7), afterwards y.
Unfortunately the MacBook does not reconnect automatically after reset of the Arduino. The Bluetooth module has to be unpaired and paired again to be usable again. Hopefully this will can be fixed somehow...
Update:
A much easier way of connecting via Bluetooth is to use the Arduino Serial Monitor (Tools -> Serial Monitor).