Skip to content

Commit

Permalink
Merge pull request #609 from srobo/arduino-update
Browse files Browse the repository at this point in the history
Update arduino firmware file to support ultrasound
  • Loading branch information
WillB97 authored Sep 1, 2024
2 parents be1f52e + 096b9f6 commit 6116328
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion resources/kit/arduino-fw.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// We communicate with the Arduino at 115200 baud.
#define SERIAL_BAUD 115200

#define FW_VER 1
#define FW_VER 2

void setup() {
Serial.begin(SERIAL_BAUD);
}

int read_pin() {
// Convert the ASCII character to a pin number.
// a -> 0, b -> 1, c -> 2, etc.
while (!Serial.available());
int pin = Serial.read();
return (int)(pin - 'a');
Expand Down Expand Up @@ -43,6 +45,33 @@ void command_mode(int mode) {
pinMode(pin, mode);
}

void command_ultrasound() {
int pulse = read_pin();
int echo = read_pin();

// config pins to correct modes
pinMode(pulse, OUTPUT);
pinMode(echo, INPUT);

// provide pulse to trigger reading
digitalWrite(pulse, LOW);
delayMicroseconds(2);
digitalWrite(pulse, HIGH);
delayMicroseconds(5);
digitalWrite(pulse, LOW);

// measure the echo time on the echo pin
int duration = pulseIn(echo, HIGH, 60000);
Serial.print(microsecondsToMm(duration));
}

long microsecondsToMm(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance we need half
// 10 x (us / 29 / 2)
return (5 * microseconds / 29);
}

void loop() {
// Fetch all commands that are in the buffer
while (Serial.available()) {
Expand Down Expand Up @@ -70,6 +99,9 @@ void loop() {
case 'p':
command_mode(INPUT_PULLUP);
break;
case 'u':
command_ultrasound();
break;
case 'v':
Serial.print("SRcustom:");
Serial.print(FW_VER);
Expand Down

0 comments on commit 6116328

Please sign in to comment.