Skip to content

Instantly share code, notes, and snippets.

@slambert
Last active February 26, 2019 04:53
Show Gist options
  • Save slambert/2991980 to your computer and use it in GitHub Desktop.
Save slambert/2991980 to your computer and use it in GitHub Desktop.
3 sonar pinging, light controller - for 100% piece
/*
Sonar 100% Program
v.0.4.1
This version has:
* 3 sonar units
* LEDs to show when triggered
* variable trigger distance set with a pot
* An error LED to show if a sonar unit is out.
* some cleaning up and comments
Steve Lambert June 25, 2012
- uses code from Alex Reben's flasher program as well.
*/
/////////////////
// SONAR STUFF //
/////////////////
#include <NewPing.h>
NewPing sonar1(2, 3, 400); // Sensor 1: trigger pin, echo pin, maximum distance in cm
NewPing sonar2(4, 5, 400); // Sensor 2: trigger pin, echo pin, maximum distance in cm
NewPing sonar3(6, 7, 400); // Sensor 3: trigger pin, echo pin, maximum distance in cm
#define pingSpeed 250 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
unsigned long pingTimer1, pingTimer2, pingTimer3;
#define LED_PIN 13 //this flashes when all sonar is triggered
#define ERROR_LED 19 //this flashes when sonar returns 0
#define SonarA 16 // LED for when 1st sonar is triggered
#define SonarB 17 // LED for when 2nd sonar is triggered
#define SonarC 18 // LED for when 3rd sonar is triggered
// Global Variables
//pot pin
#define distancePot A1
int triggerDistance = 12; // This is the short distance, in inches
int IN1 = 0; // Global inches variable 1
int IN2 = 0; // Global inches variable 2
int IN3 = 0; // Global inches variable 3
/////////////////
// RELAY STUFF //
/////////////////
#define relayAPin 8
#define relayBPin 9
#define relayCPin 10
#define relayDPin 11
#define relayEPin 12
//pot pin for changing speed
#define potPin A0
//lowest change rate in milliseconds
#define lowRate 50
//highest change rate in milliseconds
#define highRate 2000
// the length of your pattern (how many stages there are)
// Make sure this is consitant with number of BXXXX things in HGFEDCBA Pattern
#define patternLength 1280
// lenbth of pattern when off
#define patternLengthOff 16
//timing
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
//flashing
static int currentPattern = 0;
// settings for when off
byte patternOff[patternLengthOff] = {
B10000, B10000, B10000, B10000, B10000, B10000, B10000, B10000,
B00000, B00000, B00000, B00000, B00000, B00000, B00000, B00000
};
//HGFEDCBA Patterns - FUN TO CHANGE!!!
//B"XXXX" for 4 channels
//B"XXXXXXXX" for 8 channels and so on
//Make sure to change the patten length above.
byte pattern[patternLength] = {
//count across med
B11111, B11111, B00111, B00111, B00011, B00011, B00001, B00001,
B00000, B00000, B11000, B11000, B11100, B11100, B11110, B11110,
B11111, B11111, B00111, B00111, B00011, B00011, B00001, B00001,
B00000, B00000, B11000, B11000, B11100, B11100, B11110, B11110,
B11111, B11111, B00111, B00111, B00011, B00011, B00001, B00001,
B00000, B00000, B11000, B11000, B11100, B11100, B11110, B11110,
B11111, B11111, B00111, B00111, B00011, B00011, B00001, B00001,
B00000, B00000, B11000, B11000, B11100, B11100, B11110, B11110,
//64
// one moves across med
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
B11000, B11000, B00100, B00100, B00010, B00010, B00001, B00001,
//128
//one moves across fast
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
B11000, B00100, B00010, B00001, B11000, B00100, B00010, B00001,
//192
//count across fast
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
B11111, B00111, B00011, B00001, B00000, B11000, B11100, B11110,
//256
//4 slow flash
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
B11111, B11111, B11111, B11111, B00000, B00000, B00000, B00000,
// three moves across med
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
B11110, B11110, B00111, B00111, B11011, B11011, B11101, B11101,
// winky winky flip flop med
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
B11100, B11100, B11111, B11111, B00011, B00011, B00000, B00000,
// winky winky oney
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
B11111, B00111, B11111, B11011, B11111, B11101, B11111, B11110,
//512
// flash 2
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
// bongos
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
B00100, B00010, B11000, B00001, B11000, B00010, B00100, B00001,
//boom boom ta-ka ta-ka
B11111, B00000, B11111, B00000, B00111, B11110, B00111, B11110,
B11111, B00000, B11111, B00000, B11011, B11101, B11011, B11101,
B11111, B00000, B11111, B00000, B00111, B11110, B00111, B11110,
B11111, B00000, B11111, B00000, B11011, B11101, B11011, B11101,
B11111, B00000, B11111, B00000, B00111, B11110, B00111, B11110,
B11111, B00000, B11111, B00000, B11011, B11101, B11011, B11101,
B11111, B00000, B11111, B00000, B00111, B11110, B00111, B11110,
B11111, B00000, B11111, B00000, B11011, B11101, B11011, B11101,
//total random
B00100, B00010, B00001, B11000, B00010, B00100, B00001, B11000,
B00010, B00100, B11000, B00001, B00001, B11000, B00100, B00010,
B00100, B11000, B00010, B00001, B00001, B00010, B00100, B11000,
B00001, B11000, B00010, B00100, B00100, B00010, B11000, B00001,
B00100, B00010, B00001, B11000, B00010, B00100, B00001, B11000,
B00010, B00100, B11000, B00001, B00001, B11000, B00100, B00010,
B00100, B11000, B00010, B00001, B00001, B00010, B00100, B11000,
B00001, B11000, B00010, B00100, B00100, B00010, B11000, B00001,
//768
//pyramid med
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
B11000, B11100, B11110, B11111, B11110, B11100, B11000, B00000,
//reverse pyramid
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
B00001, B00011, B00111, B11111, B00111, B00011, B00001, B00000,
//creepy crawly
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
B11100, B11010, B00110, B00101, B00011, B11010, B11001, B00101,
//triangle staircase
B11000, B11100, B11110, B11111, B00001, B00010, B00100, B11000,
B11000, B00100, B00010, B00001, B11111, B11110, B11100, B11000,
B11000, B11100, B11110, B11111, B00001, B00010, B00100, B11000,
B11000, B00100, B00010, B00001, B11111, B11110, B11100, B11000,
B11000, B11100, B11110, B11111, B00001, B00010, B00100, B11000,
B11000, B00100, B00010, B00001, B11111, B11110, B11100, B11000,
B11000, B11100, B11110, B11111, B00001, B00010, B00100, B11000,
B11000, B00100, B00010, B00001, B11111, B11110, B11100, B11000,
//1024
//flash up and down
B11000, B00000, B11100, B00000, B11110, B0000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B0000, B11000, B00000,
B11000, B00000, B11100, B00000, B11110, B0000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B0000, B11000, B00000,
B11000, B00000, B11100, B00000, B11110, B0000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B0000, B11000, B00000,
B11000, B00000, B11100, B00000, B11110, B0000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B0000, B11000, B00000,
//boom boom 1234
B11111, B00000, B11111, B00000, B00111, B11011, B11101, B11110,
B11111, B00000, B11111, B00000, B11110, B11101, B11011, B00111,
B11111, B00000, B11111, B00000, B00111, B11011, B11101, B11110,
B11111, B00000, B11111, B00000, B11110, B11101, B11011, B00111,
B11111, B00000, B11111, B00000, B00111, B11011, B11101, B11110,
B11111, B00000, B11111, B00000, B11110, B11101, B11011, B00111,
B11111, B00000, B11111, B00000, B00111, B11011, B11101, B11110,
B11111, B00000, B11111, B00000, B11110, B11101, B11011, B00111,
//flash up
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B00000, B00001, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B00000, B00001, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B00000, B00001, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B00000, B00111, B00000, B00011, B00000, B00001, B00000,
//flash up and flash
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000,
B11000, B00000, B11100, B00000, B11110, B00000, B11111, B00000,
B11111, B11111, B00000, B00000, B11111, B11111, B00000, B00000
//1280
// note: leave comma off the last value
};
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 100ms (or whatever pingSpeed is)
pingTimer2 = pingTimer1 + (pingSpeed / 3); // Sensor 2 fires 33ms later
pingTimer3 = pingTimer2 + (pingSpeed / 3); // Sensor 3 fires 33ms later
pinMode(LED_PIN, OUTPUT);
pinMode(SonarA, OUTPUT);
pinMode(SonarB, OUTPUT);
pinMode(SonarC, OUTPUT);
pinMode(ERROR_LED, OUTPUT);
pinMode(relayAPin, OUTPUT);
pinMode(relayBPin, OUTPUT);
pinMode(relayCPin, OUTPUT);
pinMode(relayDPin, OUTPUT);
pinMode(relayEPin, OUTPUT);
// pinMode(relayFPin, OUTPUT);
// pinMode(relayGPin, OUTPUT);
// pinMode(relayHPin, OUTPUT);
Serial.println("Triple Sonar 100% program, by Steve Lambert");
}
void loop() {
// SPEED STUFF
currentMillis = millis();
int potValue = analogRead(potPin);
int delayPot = map(potValue, 0, 1024, lowRate, highRate);
// SONAR STUFF
int distValue = analogRead(distancePot);
triggerDistance = map(distValue, 0, 1024, 36, 84); // remap input to 3 to 7ft
if (millis() >= pingTimer1) {
pingTimer1 += pingSpeed; // Make sensor 1 fire again 100ms later (pingSpeed)
int in1 = sonar1.ping_in();
IN1 = in1;
}
if (millis() >= pingTimer2) {
pingTimer2 = pingTimer1 + (pingSpeed / 3); // Make sensor 2 fire again 50ms after sensor 1 fires
int in2 = sonar2.ping_in();
IN2 = in2;
}
if (millis() >= pingTimer3) {
pingTimer3 = pingTimer2 + (pingSpeed / 3); // Make sensor 2 fire again 50ms after sensor 1 fires
int in3 = sonar3.ping_in();
IN3 = in3;
// All sensors pinged, process results here
}
// Do other stuff here
//Uncomment these print lines for troubleshooting:
Serial.print("Ping1: ");
Serial.print(IN1);
Serial.println("in");
Serial.print(" Ping2: ");
Serial.print(IN2);
Serial.println("in");
Serial.print(" Ping3: ");
Serial.print(IN3);
Serial.println("in");
Serial.print("triggerDistance = ");
Serial.println(triggerDistance);
updateSonarLEDA();
updateSonarLEDB();
updateSonarLEDC();
sonarErrorCheck();
if (IN1 < triggerDistance && IN2 < triggerDistance && IN3 < triggerDistance && IN1 != 0 && IN2 != 0 && IN3 != 0){
/* If all 3 sonar units are triggered,
- run light up the green LED
- run the light controller patterns
*/
digitalWrite(LED_PIN, HIGH);
updateLightBlink(pattern, patternLength, delayPot);
} else {
digitalWrite(LED_PIN, LOW);
updateLightBlink(patternOff, patternLengthOff, delayPot);
}
}
void updateSonarLEDA (){
// checks 1st sonar unit and lights up LED when triggered
if (IN1 < triggerDistance && IN1 != 0){
digitalWrite(SonarA, HIGH);
} else {
digitalWrite(SonarA, LOW);
}
}
void updateSonarLEDB (){
// checks 2nd sonar unit and lights up LED when triggered
if (IN2 < triggerDistance && IN2 != 0){
digitalWrite(SonarB, HIGH);
} else {
digitalWrite(SonarB, LOW);
}
}
void updateSonarLEDC (){
// checks 3rd sonar unit and lights up LED when triggered
if (IN3 < triggerDistance && IN3 != 0){
digitalWrite(SonarC, HIGH);
} else {
digitalWrite(SonarC, LOW);
}
}
void sonarErrorCheck(){
/*
this function checks if one of the sonar units is out.
If one echos 0in as a result, it lights up the red LED
*/
if (IN1 == 0 || IN2 == 0 || IN3 == 0){
digitalWrite(ERROR_LED, HIGH); //lihgts up red LED
} else {
digitalWrite(ERROR_LED, LOW);
}
}
void updateLightBlink (byte input[], int length, int interval){
if(currentMillis - previousMillis > interval) {
digitalWrite(relayAPin, (input[currentPattern] & B00000001));
digitalWrite(relayBPin, (input[currentPattern] & B00000010));
digitalWrite(relayCPin, (input[currentPattern] & B00000100));
digitalWrite(relayDPin, (input[currentPattern] & B00001000));
digitalWrite(relayEPin, (input[currentPattern] & B00010000));
// digitalWrite(relayFPin, (input[currentPattern] & B00100000));
// digitalWrite(relayGPin, (input[currentPattern] & B01000000));
// digitalWrite(relayHPin, (input[currentPattern] & B10000000));
if(currentPattern < length - 1)
{
currentPattern++;
}
else
{
currentPattern = 0;
}
previousMillis = currentMillis;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment