Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Range keeper: an MX-FS-03V / MX-05V "am I near the transmitter?" Arduino sketch
- // Buzzes when receiver detects no signal. I explain why I need this at
- // http://www.instructables.com/answers/Why-doesnt-my-MX-FS-03V-MX-05V-433MHz-trick-work/
- // The trasmitter is simply connected to an oscillator (no microcontroller).
- // See http://www.electronics-tutorials.ws/waveforms/555_oscillator.html
- // Arduino on the receiver end determines whether it's getting [any] signal or noise
- // by building a histogram of sample values.
- // When there's signal [no matter what data], most samples will fall into
- // very few histogram slots.
- // This means that we can regard all histogram slots that have less than MIN_SIGNAL samples as "noise".
- // Number of "noise samples" seems to be a good indicater (detects whether transmitter's within "reasonable
- // range" with a reasonable error rate).
- // To do: control MIN_SIGNAL with a potentiometer for better fine-tuning
- const int ANALOG_IN = 5;
- const int BUZZ_PIN = A0;
- const int LED_PIN = LED_BUILTIN;
- const int SAMPLE_PERIOD = 30; // Millis
- const int NUM_SAMPLES = 64;
- const int MIN_SIGNAL = 10; // histogram slot < MIN_SIGNAL is considered noise
- const int ACCEPTABLE_NOISE = 12; // To do: control this with a potentiometer
- const unsigned int BUZZ_FREQ = 440; // Schumannn's favorite ;)
- const unsigned long BUZZ_DURATION = SAMPLE_PERIOD*NUM_SAMPLES/2;
- int histogram[16];
- int noise; // accumulates number of "noise samples"
- void setup() {
- pinMode(BUZZ_PIN, OUTPUT);
- pinMode(LED_PIN, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- for (int i = 0; i<16; i++) {
- histogram[i] = 0;
- }
- for (int s = 0; s<NUM_SAMPLES; s++) { // Build histogram
- histogram[analogRead(ANALOG_IN)>>6] += 1;
- delay(SAMPLE_PERIOD);
- }
- noise = 0;
- for (int i = 0; i<16; i++) { // Measure noise
- Serial.print(histogram[i]);
- Serial.print(' ');
- if (histogram[i]<MIN_SIGNAL) {
- noise += histogram[i];
- }
- }
- Serial.print('\n');
- Serial.println(noise);
- boolean in_range = noise<=ACCEPTABLE_NOISE;
- if (noise>ACCEPTABLE_NOISE) {
- tone(BUZZ_PIN,BUZZ_FREQ,BUZZ_DURATION);
- digitalWrite(LED_PIN,false);
- } else {
- digitalWrite(LED_PIN,true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement