safwan092

Untitled

Dec 4th, 2022
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <RCSwitch.h>
  2.  
  3. RCSwitch mySwitch = RCSwitch();
  4. String code;
  5. int flag = 0;
  6.  
  7. const int LED1pin = 8;
  8. const int green1 = 9;
  9. const long LED1onDuration = 2000 ;
  10. const long LED1offDuration = 2000;
  11. const int LED2pin = 11;
  12. const int red2 = 10;
  13. const long LED2onDuration = 2000;
  14. const long LED2offDuration = 2000;
  15.  
  16. int LED1State = LOW;
  17. int LED2State = LOW;
  18.  
  19. unsigned long rememberTime1 = 0;
  20. unsigned long rememberTime2 = 0;
  21. unsigned long previousMillis = 0;
  22.  
  23. void setup() {
  24. Serial.begin(9600);
  25. mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
  26. pinMode(LED1pin, OUTPUT);
  27. pinMode(green1, OUTPUT);
  28. pinMode(LED2pin, OUTPUT);
  29. pinMode(red2, OUTPUT);
  30. digitalWrite(LED1pin, 1);
  31. digitalWrite(green1, 1);
  32. digitalWrite(LED2pin, 1);
  33. digitalWrite(red2, 1);
  34. delay(2000);
  35. }
  36.  
  37. void loop() {
  38. if (mySwitch.available()) {
  39. code = mySwitch.getReceivedValue();
  40. Serial.print("Received ");
  41. Serial.println( code );
  42. if (code == "5121218") {
  43. Serial.println("Open Green for 10 Seconds");
  44. flag = 1;
  45. }
  46. mySwitch.resetAvailable();
  47. }
  48. if (flag == 0) {
  49. normalOperation();
  50. //only1Runing();
  51. }
  52. else if (flag == 1) {
  53. digitalWrite(LED1pin, 1);
  54. digitalWrite(green1, 0);
  55. digitalWrite(LED2pin, 1);
  56. digitalWrite(red2, 0);
  57. delay(10000);
  58. flag = 0;
  59. }
  60.  
  61. }//end of LOOP
  62.  
  63. void only1Runing() {
  64. digitalWrite(LED1pin, 0);
  65. digitalWrite(green1, 1);
  66. digitalWrite(LED2pin, 0);
  67. digitalWrite(red2, 1);
  68. }
  69. void normalOperation() {
  70. if ( LED1State == HIGH )
  71. {
  72. if ( (millis() - rememberTime1) >= LED1onDuration) {
  73. LED1State = LOW;
  74. rememberTime1 = millis();
  75. }
  76. }
  77. else
  78. {
  79. if ( (millis() - rememberTime1) >= LED1offDuration) {
  80. LED1State = HIGH;
  81.  
  82. rememberTime1 = millis();
  83. }
  84. }
  85. digitalWrite(LED1pin, LED1State);
  86. digitalWrite(green1, !LED1State);
  87.  
  88. if ( LED2State == HIGH )
  89. {
  90. if ( (millis() - rememberTime2) >= LED2onDuration) {
  91. LED2State = LOW;
  92. rememberTime2 = millis();
  93. }
  94. }
  95. else
  96. {
  97. if ( (millis() - rememberTime2) >= LED2offDuration) {
  98. LED2State = HIGH;
  99.  
  100. rememberTime2 = millis();
  101. }
  102. }
  103.  
  104. digitalWrite(LED2pin, LED2State);
  105. digitalWrite(red2, !LED2State);
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment