Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino MX-FS-03V / MX-05V "am I near transmitter?" trick
- // The trasmitter is simply connected to an oscillator (no microcontroller).
- // 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 (from my experience: 0 + some other [single] value, so I guess it's pwm).
- // 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 there's a transmitter or not
- // with a very low error rate).
- // Onboard LED (pin 13) is on when we detect signal.
- // Serial shows a numeric noise level.
- // In the final project, receiver will buzz when there's *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/
- const int LED_PIN = 13; // On-board LED
- const int BUZZ_PIN = 2; // Used for a temporary "cheat". See below (or ignore :) ).
- const int ANALOG_IN = 5;
- 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 = 3; // when noise > ACCEPTABLE_NOISE, we assume no signal
- int histogram[16];
- int noise; // accumulates number of "noise samples"
- void setup() {
- pinMode(LED_PIN, OUTPUT);
- pinMode(BUZZ_PIN, OUTPUT);
- Serial.begin(9600);
- // Temporary "cheat":
- // BUZZ_PIN is wired to the transmitter until I get an oscillator for it.
- // (ground and power too, so that BUZZ_PIN has proper reference).
- tone(BUZZ_PIN,440); // frequency isn't suppose to matter.
- }
- 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
- if (histogram[i]<MIN_SIGNAL) {
- noise += histogram[i];
- }
- }
- Serial.println(noise);
- digitalWrite(LED_PIN,noise<=ACCEPTABLE_NOISE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement