Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #define rfReceivePin A0 //RF Receiver data pin = Analog pin 0
  2. #define rfTransmitPin 4 //RF Transmitter pin = digital pin 4
  3. #define button 6 //The button attached to digital pin 6
  4. #define ledPin 13 //Onboard LED = digital pin 13
  5.  
  6. const int dataSize = 1700; //Arduino memory is limited (max=1700)
  7. byte storedData[dataSize]; //Create an array to store the data
  8. const unsigned int threshold = 100; //signal threshold value
  9. int maxSignalLength = 255; //Set the maximum length of the signal
  10. int dataCounter = 0; //Variable to measure the length of the signal
  11. int buttonState = 1; //Variable to control the flow of code using button presses
  12. int buttonVal = 0; //Variable to hold the state of the button
  13. int timeDelay = 105; //Used to slow down the signal transmission (can be from 75 - 135)
  14.  
  15. void setup(){
  16. Serial.begin(9600); //Initialise Serial communication - only required if you plan to print to the Serial monitor
  17. pinMode(rfTransmitPin, OUTPUT);
  18. pinMode(ledPin, OUTPUT);
  19. pinMode(button, INPUT);
  20. }
  21.  
  22. void loop(){
  23. buttonVal = digitalRead(button);
  24.  
  25. if(buttonState>0 && buttonVal==HIGH){
  26. //Serial.println("Listening for Signal");
  27. initVariables();
  28. listenForSignal();
  29. }
  30.  
  31. buttonVal = digitalRead(button);
  32.  
  33. if(buttonState<1 && buttonVal==HIGH){
  34. //Serial.println("Send Signal");
  35. sendSignal();
  36. }
  37.  
  38. delay(20);
  39. }
  40.  
  41.  
  42. /* ------------------------------------------------------------------------------
  43. Initialise the array used to store the signal
  44. ------------------------------------------------------------------------------*/
  45. void initVariables(){
  46. for(int i=0; i<dataSize; i++){
  47. storedData[i]=0;
  48. }
  49. buttonState=0;
  50. }
  51.  
  52.  
  53. /* ------------------------------------------------------------------------------
  54. Listen for the signal from the RF remote. Blink the RED LED at the beginning to help visualise the process
  55. And also turn RED LED on when receiving the RF signal
  56. ------------------------------------------------------------------------------ */
  57. void listenForSignal(){
  58. digitalWrite(ledPin, HIGH);
  59. delay(1000);
  60. digitalWrite(ledPin,LOW);
  61. while(analogRead(rfReceivePin)<threshold){
  62. //Wait here until an RF signal is received
  63. }
  64. digitalWrite(ledPin, HIGH);
  65.  
  66. //Read and store the rest of the signal into the storedData array
  67. for(int i=0; i<dataSize; i=i+2){
  68.  
  69. //Identify the length of the HIGH signal---------------HIGH
  70. dataCounter=0; //reset the counter
  71. while(analogRead(rfReceivePin)>threshold && dataCounter<maxSignalLength){
  72. dataCounter++;
  73. }
  74. storedData[i]=dataCounter; //Store the length of the HIGH signal
  75.  
  76.  
  77. //Identify the length of the LOW signal---------------LOW
  78. dataCounter=0;//reset the counter
  79. while(analogRead(rfReceivePin)<threshold && dataCounter<maxSignalLength){
  80. dataCounter++;
  81. }
  82. storedData[i+1]=dataCounter; //Store the length of the LOW signal
  83. }
  84.  
  85. storedData[0]++; //Account for the first AnalogRead>threshold = lost while listening for signal
  86. digitalWrite(ledPin, LOW);
  87. }
  88.  
  89.  
  90. /*------------------------------------------------------------------------------
  91. Send the stored signal to the FAN/LIGHT's RF receiver. A time delay is required to synchronise
  92. the digitalWrite timeframe with the 433MHz signal requirements. This has not been tested with different
  93. frequencies.
  94. ------------------------------------------------------------------------------ */
  95. void sendSignal(){
  96. digitalWrite(ledPin, HIGH);
  97. for(int i=0; i<dataSize; i=i+2){
  98. //Send HIGH signal
  99. digitalWrite(rfTransmitPin, HIGH);
  100. delayMicroseconds(storedData[i]*timeDelay);
  101. //Send LOW signal
  102. digitalWrite(rfTransmitPin, LOW);
  103. delayMicroseconds(storedData[i+1]*timeDelay);
  104. }
  105. digitalWrite(ledPin, LOW);
  106. delay(1000);
  107.  
  108.  
  109.  
  110. for(int i=0; i<dataSize; i=i+2){
  111. Serial.print(storedData[i]);
  112. Serial.print(",");
  113. Serial.println(storedData[i+1]);
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement