Jugaadu and his brother both are studying in their respective rooms. Jugaadu had noticed multiple times that his brother always forgets to switch off the light while leaving the room. Jugaadu believes in saving electricity for a sustainable environment. Let us help Jugaadu to get rid of this problem?
#define rfReceivePin A0 //RF Receiver data pin = Analog pin 0
#define rfTransmitPin 4 //RF Transmitter pin = digital pin 4
#define button 6 //The button attached to digital pin 6
#define ledPin 13 //Onboard LED = digital pin 13
const int dataSize = 500; //Arduino memory is limited (max=1700)
byte storedData[dataSize]; //Create an array to store the data
const unsigned int threshold = 100; //signal threshold value
int maxSignalLength = 255; //Set the maximum length of the signal
int dataCounter = 0; //Variable to measure the length of the signal
int buttonState = 1; //Variable to control the flow of code using button presses
int buttonVal = 0; //Variable to hold the state of the button
int timeDelay = 105; //Used to slow down the signal transmission (can be from 75 - 135)
void setup(){
Serial.begin(9600); //Initialise Serial communication - only required if you plan to print to the Serial monitor
pinMode(rfTransmitPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT);
}
void loop(){
//buttonVal = digitalRead(button);
initVariables();
//if(buttonState>0 && buttonVal==HIGH){
//Serial.println("Listening for Signal");
// }
buttonVal = digitalRead(button);
// Serial.println(buttonVal);
if( buttonVal==LOW){
//Serial.println("Send Signal");
listenForSignal();
sendSignal();
}
delay(20);
}
/* ------------------------------------------------------------------------------
Initialise the array used to store the signal
------------------------------------------------------------------------------*/
void initVariables(){
for(int i=0; ithreshold && dataCounterthreshold = lost while listening for signal
digitalWrite(ledPin, LOW);
}
/*------------------------------------------------------------------------------
Send the stored signal to the FAN/LIGHT's RF receiver. A time delay is required to synchronise
the digitalWrite timeframe with the 433MHz signal requirements. This has not been tested with different
frequencies.
------------------------------------------------------------------------------ */
void sendSignal(){
digitalWrite(ledPin, HIGH);
for(int i=0; i