Last active
August 29, 2015 14:02
-
-
Save thedod/ba98365c2e5984db2496 to your computer and use it in GitHub Desktop.
Stick2Cane: arduino sketch for http://www.instructables.com/id/Stick2Cane-never-lose-your-walking-cane/
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
/* Pin definitions for MilCandy boxes | |
http://www.seeedstudio.com/wiki/MilCandy | |
Notes: | |
* McGroveIn2 and McGroveOut2 aren't used in [most?] Grove components | |
but if you use a breakout-cable/screw-ternimal/etc. it's the white cable. | |
* McBattIn is battery level | |
* McLightIn is a light sensor | |
*/ | |
// analog input(s) ("If" Grove jack) | |
const int McGroveIn1 = 5; | |
const int McGroveIn2 = 4; | |
// other analog sensors | |
const int McBattIn = 3; | |
const int McLightIn = 16; | |
// Yellow "learn" button (digital) | |
// Note: button's normally HIGH, which is nothing to be ashamed off ;) | |
const int McButton = 2; | |
// output(s) ("Then" Grove jack) | |
const int McGroveOut1 = 5; | |
const int McGroveOut2 = 6; | |
// LEDs | |
const int McLEDGreen = 3; | |
const int McLEDRed = 4; | |
// const int McLED = 7; // not sure what it is - but I see no LED there |
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
// Stic2k2Cane - never lose your walking cain (433/315MHz tweak) | |
// Arduino on receiving end buzzes when receiver gets no signal. See what it's good for | |
// at http://www.instructables.com/id/Stick2Cane-never-lose-your-walking-cane/ | |
// Transmitter is simply connected to a 50KHz 555 oscillator (no microcontroller). | |
// See http://www.electronics-tutorials.ws/waveforms/555_oscillator.html | |
// Arduino determines whether it's getting a signal by building a histogram of sample | |
// values. When there's signal, most samples into a specific histogram slot (or its | |
// immediate neighbours). We're excluding the 0 value since it's common anyway. | |
// ----- begin pin definitions | |
// If you're not using a MilCandy Arduino clone | |
// Replace Mc* with the pins you connect the receiver, buzzer and LEDs to. | |
#include "MilCandy.h" | |
const int DATA_IN = McGroveIn1; | |
const int BUZZ_PIN = McGroveOut1; | |
const int LED_SUCCESS_PIN = McLEDGreen; | |
const int LED_FAIL_PIN = McLEDRed; | |
const int BUTTON_PIN = McButton; | |
const int BUTTON_NORMAL_STATE = HIGH; // true for MilCandy | |
// ----- end pin definitions | |
const int SAMPLE_PERIOD = 22; // Millis | |
const int NUM_SAMPLES = 64; | |
const unsigned int BUZZ_FREQ = 4000; // In this frequency range, piezo is loud | |
const unsigned long BUZZ_DURATION = SAMPLE_PERIOD*NUM_SAMPLES/4; | |
int histogram[16]; | |
int peak; // max histogram value (excluding 0) | |
const int MIN_REQUIRED_PEAK = 38; // From experiments, this is usually above background noise | |
boolean is_mute, button_was_pressed, is_signal; | |
void setup() { | |
pinMode(BUZZ_PIN, OUTPUT); | |
noTone(BUZZ_PIN); | |
pinMode(LED_SUCCESS_PIN, OUTPUT); | |
digitalWrite(LED_SUCCESS_PIN, LOW); | |
pinMode(LED_FAIL_PIN , OUTPUT); | |
digitalWrite(LED_FAIL_PIN, LOW); | |
// When you charge a MilCandy, it is on automatically, | |
// AND the power cable introduces RF noise, so it's | |
// "you charge, I beep". That's why it wakes up mute :) | |
is_mute = true; | |
is_signal = false; | |
button_was_pressed = false; // previous button state (needed for detecting changes) | |
} | |
void loop() { | |
// Init histogram | |
for (int i = 0; i<16; i++) { histogram[i] = 0; } | |
for (int s = 0; s<NUM_SAMPLES; s++) { // Build histogram | |
histogram[analogRead(DATA_IN)>>6] += 1; // quantize 10 bits to 4 | |
delay(SAMPLE_PERIOD); | |
updateGUI(); | |
} | |
int peak = 0,peakidx; | |
for (int i = 1; i<16; i++) { // Skip 0. Noise has lots of 0. | |
if (peak<histogram[i]) { | |
peak = histogram[i]; | |
peakidx = i; | |
} | |
} | |
// Tweak in case peak is "fuzzy" | |
if (peakidx>1) peak += histogram[peakidx-1]; | |
if (peakidx<15) peak += histogram[peakidx+1]; | |
is_signal = (peak>=MIN_REQUIRED_PEAK); | |
if (!is_signal && !is_mute) tone(BUZZ_PIN,BUZZ_FREQ,BUZZ_DURATION); | |
} | |
void updateGUI() { | |
if (digitalRead(BUTTON_PIN)!=BUTTON_NORMAL_STATE) { | |
if (!button_was_pressed) { | |
is_mute = !is_mute; | |
} | |
button_was_pressed = true; | |
} else { | |
button_was_pressed = false; | |
} | |
if (is_mute && millis()%500>450) { // blink "I'm mute! Dammit!" | |
digitalWrite(LED_FAIL_PIN,LOW); | |
digitalWrite(LED_SUCCESS_PIN,LOW); | |
} else if (is_signal) { | |
digitalWrite(LED_SUCCESS_PIN,HIGH); | |
digitalWrite(LED_FAIL_PIN,LOW); | |
} else { | |
digitalWrite(LED_FAIL_PIN,HIGH); | |
digitalWrite(LED_SUCCESS_PIN,LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment