Advertisement
Guest User

Detect signal

a guest
May 25th, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. // Arduino MX-FS-03V / MX-05V "am I near transmitter?" trick
  2. // The trasmitter is simply connected to an oscillator (no microcontroller).
  3.  
  4. // Arduino on the Receiver end determines whether it's getting [any] signal or noise
  5. // by building a histogram of sample values.
  6. // When there's signal [no matter what data], most samples will fall into
  7. // very few histogram slots (from my experience: 0 + some other [single] value, so I guess it's pwm).
  8. // This means that we can regard all histogram slots that have less than MIN_SIGNAL samples as "noise".
  9. // Number of "noise samples" seems to be a good indicater (detects whether there's a transmitter or not
  10. // with a very low error rate).
  11.  
  12. // Onboard LED (pin 13) is on when we detect signal.
  13. // Serial shows a numeric noise level.
  14.  
  15. // In the final project, receiver will buzz when there's *no* signal. I explain why I need this at
  16. // http://www.instructables.com/answers/Why-doesnt-my-MX-FS-03V-MX-05V-433MHz-trick-work/
  17.  
  18. const int LED_PIN = 13; // On-board LED
  19. const int BUZZ_PIN = 2; // Used for a temporary "cheat". See below (or ignore :) ).
  20.  
  21. const int ANALOG_IN = 5;
  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 = 3; // when noise > ACCEPTABLE_NOISE, we assume no signal
  26. int histogram[16];
  27. int noise; // accumulates number of "noise samples"
  28.  
  29. void setup() {
  30.   pinMode(LED_PIN, OUTPUT);
  31.   pinMode(BUZZ_PIN, OUTPUT);
  32.   Serial.begin(9600);
  33.  
  34.   // Temporary "cheat":
  35.   // BUZZ_PIN is wired to the transmitter until I get an oscillator for it.
  36.   // (ground and power too, so that BUZZ_PIN has proper reference).
  37.   tone(BUZZ_PIN,440); // frequency isn't suppose to matter.
  38. }
  39.  
  40. void loop() {
  41.   for (int i=0; i<16; i++) {
  42.     histogram[i] = 0;
  43.   }
  44.   for (int s=0; s<NUM_SAMPLES; s++) { // Build histogram
  45.     histogram[analogRead(ANALOG_IN)>>6] += 1;
  46.     delay(SAMPLE_PERIOD);
  47.   }
  48.   noise = 0;
  49.   for (int i=0; i<16; i++) { // Measure noise
  50.     if (histogram[i]<MIN_SIGNAL) {
  51.       noise += histogram[i];
  52.     }
  53.   }
  54.   Serial.println(noise);
  55.   digitalWrite(LED_PIN,noise<=ACCEPTABLE_NOISE);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement