Created
March 13, 2017 00:52
The Arduino code for the Arduino Nano that Receives an RF signal to switch on a relay
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
/* =============================================================== | |
Project: CHAIN BLOCKS : 433MHz Receiver with Relay and Motor | |
Author: Scott C | |
Created: 26th Feb 2017 | |
Arduino IDE: 1.8.1 | |
Website: http://arduinobasics.blogspot.com.au | |
Description: Use the 433Mhz RF receiver to receive a signal. The Arduino | |
will process this signal and will trigger a relay | |
based on the result of the signal. A motor is attached to the | |
relay for extra effect. | |
================================================================== */ | |
#define relay 10 //Connect relay module signal pin to Arduino digital pin 10 | |
long mySignal=0; //This variable holds the signal value | |
int reps = 200; //Increase the number of repetitions (Reps) for greater accuracy | |
boolean trigger = false; //The relay will switch when the "trigger" is true | |
boolean lastTrigger = false; //Hold the state of the last Trigger value | |
int threshold = 325; //If mySignal is greater than this threshold, it will trigger the relay | |
/* ================================================================ | |
* SETUP () | |
================================================================== */ | |
void setup() { | |
pinMode(relay, OUTPUT); | |
Serial.begin(9600); | |
} | |
/* ================================================================ | |
* LOOP () | |
================================================================== */ | |
void loop() { | |
mySignal = 0; //Reset this variable with every loop. | |
for(int i=0; i<reps; i++){ | |
mySignal = mySignal + analogRead(A0); | |
} | |
mySignal = mySignal / reps; //Take the average analog reading. | |
Serial.println(mySignal); //Print to the serial monitor / or plotter. | |
if(mySignal>threshold){ //If mySignal is greater than the threshold, it will trigger the relay | |
trigger = true; | |
} else { | |
trigger = false; | |
} | |
if(trigger==lastTrigger){ | |
//do nothing if the current trigger is the same as the last trigger. | |
}else { | |
digitalWrite(relay, trigger); //The state of the relay will reflect that of the trigger value | |
delay(200); //The 200ms delay is necessary for stability reasons, DO NOT DELETE | |
lastTrigger=trigger; //Keep track of the state of the current trigger. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment