Skip to content

Instantly share code, notes, and snippets.

@cmoz
Last active March 31, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmoz/e233d4ed3e328ae1abead9c48feadad1 to your computer and use it in GitHub Desktop.
Save cmoz/e233d4ed3e328ae1abead9c48feadad1 to your computer and use it in GitHub Desktop.
Entire code for distance hat
/**************************************************
* Ultrasonic Distance Sensor Hat with NeoPixels, vibration motor and buzzer
* based on HC-SR04 sonar sensor, NeoPixel libraries
* Author: Christine Farion
* Site: www.christinefarion.com
* Twitter: @cmoz
* Date: October 2021
* License: CC BY-NC-SAi
*
* using Adafruit NeoPixel Library
*
* Pinout for a lilypad USB board that I used...
* PINOUTS : NEOPIXELS PIN £
* : BUZZER PIN 10
* : VIBRATION PIN 9
* : ECHO A2
* : TRIGGER A3
*/
/**************************************************
* Ultrasonic Distance Warner
* based on HC-SR04 sonar sensor
* Author: Marcel Andr
* Date: 6.7.2020
* License: CC BY-NC-SA
*
* using sonar library from Martin Sosic (https://github.com/Martinsos/arduino-lib-hc-sr04)
*/
#include <HCSR04.h> // include Library - use the extension manager to add this library to your environment
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 3
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24 // Popular NeoPixel ring size
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
//Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 50 // Time (in milliseconds) to pause between pixels
// Define the hardware connections:
const int pin_buzzer = 10; // pin where the buzzer is connceted to
const int pin_vibration = 9; // pin where the buzzer is connceted to
const int pin_Echo = A2; // digital pin for HC-SR04 echo connector
const int pin_Trigger = A3; // digital pin for HC-SR04 Trigger connector
// Define the signalling
const int tone_repeat = 1; // How often a tone / smile will be repeated
const int tone_low = 800; // Frequency for tone if emergency level is low
const int time_low = 0; // Tone duration if emergency level is low
const int dist_low = 150; // distance level for low emergency level
const int tone_medium = 1600; // Frequency for tone if emergency level is medium
const int time_medium = 350; // Tone duration if emergency level is medium
const int dist_medium = 100; // distance level for medium emergency level
const int tone_high = 2400; // Frequency for tone if emergency level is high
const int time_high = 200; // Tone duration if emergency level is high
const int dist_high = 50; // distance level for high emergency level
// to store the current distance:
float distance; // distance read from ultrasonic sensor
//create an instance to control the sensor:
UltraSonicDistanceSensor distanceSensor(pin_Trigger, pin_Echo); // Initialize sensor that uses digital pins 13 and 12.
/*
* Setup steps
*/
// *************************************************** SETUP *********************************************************//
void setup () {
Serial.begin(9600); // Enabled for debug
pinMode(pin_vibration, OUTPUT);
Start(200); // Call start sequence
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.show();
pixels.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
/*
* The main programm loop:
*
* - read current distance
* - compare with distance intervals defined above
* - call signalling (emergency) routine to set smiling and beeping
*/
// *************************************************** LOOP *********************************************************//
void loop () {
pixels.clear(); // Set all pixel colors to 'off'
distance = distanceSensor.measureDistanceCm(); // read current distance
Serial.println(distance); // output for debug
// check interval:
if (distance < dist_high)
{
emergency(3);
}
else if (distance <= dist_medium)
{
emergency(2);
}
else if (distance <= dist_low)
{
emergency(1);
}
else if (distance > dist_low)
{
emergency(0);
}
delay(100); // wait a bit :-)
}
// *************************************************** FUNCTIONS ******************************************************** //
/*
* Start Sequence
* ==============
*
* Params:
* -------
* startpeed: time in milliseconds between each LED step
*
*/
void Start(int startspeed) {
delay(500);
}
void greenSmile() {
//pixelColor(0,255,0);
rainbow(10); // Flowing rainbow cycle along the whole strip
}
void redSmile() {
pixelColor(255,0,0);
}
void blueSmile() {
pixelColor(0,0,255);
}
void purpleSmile() {
pixelColor(180, 0, 255);
}
void offSmile() {
pixels.clear(); // Set all pixel colors to 'off'
}
/*
* Signal to short distance
* ========================
*
* Params:
* -------
* level: "loudness" of signaling -> 0 (off), 1 (low),2 (medium) or 3 (high)
*
*/
void emergency(int level) {
switch (level) {
case 0: // off
greenSmile();
break;
case 1: // low
for (int i = 1; i <= tone_repeat; i++) {
//tone (pin_buzzer, tone_low, time_low);
blueSmile();
//digitalWrite (pin_vibration, HIGH);
delay (time_low);
offSmile();
delay (time_low);
digitalWrite (pin_vibration, LOW);
}
break;
case 2: // medium
for (int i = 1; i <= tone_repeat; i++) {
tone (pin_buzzer, tone_medium, time_medium);
purpleSmile();
digitalWrite (pin_vibration, HIGH);
delay (time_medium);
offSmile();
delay (time_medium);
digitalWrite (pin_vibration, LOW);
}
break;
case 3: // high
for (int i = 1; i <= tone_repeat; i++) {
tone (pin_buzzer, tone_high, time_high);
redSmile();
digitalWrite (pin_vibration, HIGH);
delay (time_high);
offSmile();
delay (time_high);
digitalWrite (pin_vibration, LOW);
}
break;
default: // default = off
greenSmile();
delay (time_low);
break;
}
}
void pixelColor(int color1, int color2, int color3) {
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels.setPixelColor(i, pixels.Color(color1, color2, color3));
pixels.show(); // Send the updated pixel colors to the hardware.
delay(DELAYVAL); // Pause before next pass through loop
}
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
// Hue of first pixel runs 5 complete loops through the color wheel. EDITED To run once
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < 1*65536; firstPixelHue += 256) {
for(int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
}
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment