Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update arduino firmware file to support ultrasound #609

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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