Skip to content

Commit 6116328

Browse files
authored
Merge pull request #609 from srobo/arduino-update
Update arduino firmware file to support ultrasound
2 parents be1f52e + 096b9f6 commit 6116328

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

resources/kit/arduino-fw.ino

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// We communicate with the Arduino at 115200 baud.
44
#define SERIAL_BAUD 115200
55

6-
#define FW_VER 1
6+
#define FW_VER 2
77

88
void setup() {
99
Serial.begin(SERIAL_BAUD);
1010
}
1111

1212
int read_pin() {
13+
// Convert the ASCII character to a pin number.
14+
// a -> 0, b -> 1, c -> 2, etc.
1315
while (!Serial.available());
1416
int pin = Serial.read();
1517
return (int)(pin - 'a');
@@ -43,6 +45,33 @@ void command_mode(int mode) {
4345
pinMode(pin, mode);
4446
}
4547

48+
void command_ultrasound() {
49+
int pulse = read_pin();
50+
int echo = read_pin();
51+
52+
// config pins to correct modes
53+
pinMode(pulse, OUTPUT);
54+
pinMode(echo, INPUT);
55+
56+
// provide pulse to trigger reading
57+
digitalWrite(pulse, LOW);
58+
delayMicroseconds(2);
59+
digitalWrite(pulse, HIGH);
60+
delayMicroseconds(5);
61+
digitalWrite(pulse, LOW);
62+
63+
// measure the echo time on the echo pin
64+
int duration = pulseIn(echo, HIGH, 60000);
65+
Serial.print(microsecondsToMm(duration));
66+
}
67+
68+
long microsecondsToMm(long microseconds) {
69+
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
70+
// The ping travels out and back, so to find the distance we need half
71+
// 10 x (us / 29 / 2)
72+
return (5 * microseconds / 29);
73+
}
74+
4675
void loop() {
4776
// Fetch all commands that are in the buffer
4877
while (Serial.available()) {
@@ -70,6 +99,9 @@ void loop() {
7099
case 'p':
71100
command_mode(INPUT_PULLUP);
72101
break;
102+
case 'u':
103+
command_ultrasound();
104+
break;
73105
case 'v':
74106
Serial.print("SRcustom:");
75107
Serial.print(FW_VER);

0 commit comments

Comments
 (0)