...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
#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'.
In Terminal a list of all open COM ports can be shown via ' ls /dev/tty.* '.
' /dev/tty.HC-06-DevB ' should be shown in the list. A connection to the module can be established via ' screen /dev/tty.HC-06-DevB '.