daily pastebin goal
50%
SHARE
TWEET

Range keeper: "am I near the transmitter" Arduino sketch

a guest May 26th, 2014 389 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Range keeper: an MX-FS-03V / MX-05V "am I near the transmitter?" Arduino sketch
  2.  
  3. // Buzzes when receiver detects no signal. I explain why I need this at
  4. // http://www.instructables.com/answers/Why-doesnt-my-MX-FS-03V-MX-05V-433MHz-trick-work/
  5.  
  6. // The trasmitter is simply connected to an oscillator (no microcontroller).
  7. // See http://www.electronics-tutorials.ws/waveforms/555_oscillator.html
  8.  
  9. // Arduino on the receiver end determines whether it's getting [any] signal or noise
  10. // by building a histogram of sample values.
  11. // When there's signal [no matter what data], most samples will fall into
  12. // very few histogram slots.
  13. // This means that we can regard all histogram slots that have less than MIN_SIGNAL samples as "noise".
  14. // Number of "noise samples" seems to be a good indicater (detects whether transmitter's within "reasonable
  15. // range" with a reasonable error rate).
  16. // To do: control MIN_SIGNAL with a potentiometer for better fine-tuning
  17.  
  18. const int ANALOG_IN = 5;
  19. const int BUZZ_PIN = A0;
  20. const int LED_PIN = LED_BUILTIN;
  21.  
  22. const int SAMPLE_PERIOD = 30; // Millis
  23. const int NUM_SAMPLES = 64;
  24. const int MIN_SIGNAL = 10; // histogram slot < MIN_SIGNAL is considered noise
  25. const int ACCEPTABLE_NOISE = 12; // To do: control this with a potentiometer
  26.  
  27. const unsigned int BUZZ_FREQ = 440; // Schumannn's favorite ;)
  28. const unsigned long BUZZ_DURATION = SAMPLE_PERIOD*NUM_SAMPLES/2;
  29.  
  30. int histogram[16];
  31. int noise; // accumulates number of "noise samples"
  32.  
  33. void setup() {
  34.   pinMode(BUZZ_PIN, OUTPUT);
  35.   pinMode(LED_PIN, OUTPUT);
  36.   Serial.begin(9600);
  37. }
  38.  
  39. void loop() {
  40.   for (int i = 0; i<16; i++) {
  41.     histogram[i] = 0;
  42.   }
  43.   for (int s = 0; s<NUM_SAMPLES; s++) { // Build histogram
  44.     histogram[analogRead(ANALOG_IN)>>6] += 1;
  45.     delay(SAMPLE_PERIOD);
  46.   }
  47.  
  48.   noise = 0;
  49.   for (int i = 0; i<16; i++) { // Measure noise
  50.     Serial.print(histogram[i]);
  51.     Serial.print(' ');
  52.     if (histogram[i]<MIN_SIGNAL) {
  53.       noise += histogram[i];
  54.     }
  55.   }
  56.   Serial.print('\n');
  57.   Serial.println(noise);
  58.   boolean in_range = noise<=ACCEPTABLE_NOISE;
  59.   if (noise>ACCEPTABLE_NOISE) {
  60.     tone(BUZZ_PIN,BUZZ_FREQ,BUZZ_DURATION);
  61.     digitalWrite(LED_PIN,false);
  62.   } else {
  63.     digitalWrite(LED_PIN,true);
  64.   }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top