Last active
June 17, 2020 02:34
-
-
Save IdrisCytron/b71e46a02bfa87d71a050a46d37fe022 to your computer and use it in GitHub Desktop.
Social Distancing Alert Wristband Using Arduino (Maker Nano)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Project: Social Distancing Alert Wristband Using Arduino (Maker Nano) | |
Board: Arduino Nano (Maker Nano) | |
Connections: | |
Nano | VL53L0X | |
5V - VIN | |
GND - GND | |
A4 - SDA | |
A5 - SCL | |
External libraries: | |
- Adafruit VL53L0X V1.0.9 (Manager) | |
*/ | |
#include "Adafruit_VL53L0X.h" | |
Adafruit_VL53L0X lox = Adafruit_VL53L0X(); | |
#define BUZZER 8 | |
#define NOTE_E4 330 | |
#define NOTE_G4 392 | |
#define NOTE_B4 494 | |
#define NOTE_D5 587 | |
int pin, ledOn, ledOff; | |
int note = 0; | |
int delayMs = 100; | |
int distance = 0; | |
void setup() | |
{ | |
for (pin = 2; pin < 13; pin++) { | |
pinMode(pin, OUTPUT); | |
} | |
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. | |
Serial.println("Adafruit VL53L0X test"); | |
if (!lox.begin()) { | |
Serial.println(F("Failed to boot VL53L0X")); | |
while(1); | |
} | |
// power | |
Serial.println(F("VL53L0X API Simple Ranging example\n\n")); | |
} | |
void loop() | |
{ | |
VL53L0X_RangingMeasurementData_t measure; | |
Serial.print("Reading a measurement... "); | |
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout! | |
if (measure.RangeStatus != 4) { // phase failures have incorrect data | |
distance = measure.RangeMilliMeter; | |
Serial.print("Distance (mm): "); | |
Serial.println(distance); | |
if (distance < 300) { | |
ledOn = 13; | |
} | |
else if (distance < 400) { | |
ledOn = 12; | |
} | |
else if (distance < 500) { | |
ledOn = 11; | |
} | |
else if (distance < 600) { | |
ledOn = 10; | |
} | |
else if (distance < 700) { | |
ledOn = 8; | |
} | |
else if (distance < 800) { | |
ledOn = 7; | |
} | |
else if (distance < 900) { | |
ledOn = 6; | |
} | |
else if (distance < 1000) { | |
ledOn = 5; | |
} | |
else if (distance < 1100) { | |
ledOn = 4; | |
} | |
else if (distance < 1200) { | |
ledOn = 3; | |
} | |
ledOff = ledOn; | |
for (pin = 2; pin < ledOn; pin++) { | |
if (pin != 8) { | |
digitalWrite(pin, HIGH); | |
} | |
} | |
for (pin = ledOff; pin < 13; pin++) { | |
if (pin != 8) { | |
digitalWrite(pin, LOW); | |
} | |
} | |
delayMs = 100; | |
if (distance < 500) { | |
tone(BUZZER, NOTE_D5, 50); | |
delay(50); | |
delayMs = 30; | |
} | |
else if (distance < 600) { | |
tone(BUZZER, NOTE_B4, 50); | |
delay(50); | |
delayMs = 70; | |
} | |
else if (distance < 800) { | |
tone(BUZZER, NOTE_G4, 50); | |
delay(50); | |
delayMs = 110; | |
} | |
else if (distance < 1000) { | |
tone(BUZZER, NOTE_E4, 70); | |
delay(50); | |
delayMs = 150; | |
} | |
} | |
else { | |
Serial.println(" out of range "); | |
for (pin = 2; pin < 13; pin++) { | |
if (pin != 8) { | |
digitalWrite(pin, LOW); | |
} | |
} | |
} | |
delay(delayMs); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment