Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #include <Arduino.h>
  3. #include <avr/wdt.h>
  4.  
  5. int pins[]={14,16,17,18,19}; //TODO: Set Switch Configuration
  6. int no_of_switches=5;
  7.  
  8. String names, req, s;
  9.  
  10. void setup() {
  11.  
  12. //Setting up for switches
  13.  
  14. // initialize the pushbutton pin as an input:
  15. for(int i=0;i<6;i++)
  16. pinMode(i+3, INPUT);
  17.  
  18. //Common pin pull up resistor
  19. pinMode(2,OUTPUT);
  20. digitalWrite(2,HIGH);
  21.  
  22. //Setting up for relays
  23. Serial.begin(115200);
  24.  
  25. for(int i=0;i<6;i++)
  26. {
  27. pinMode(i+14, OUTPUT);
  28. digitalWrite(14, 0);
  29. }
  30.  
  31. Serial.setTimeout(4);
  32.  
  33. names="";
  34. int i=0;
  35. char data;
  36. EEPROM.get(i, data);
  37.  
  38. while(data!='^')
  39. {
  40. names+=data;
  41. i++;
  42. EEPROM.get(i, data);
  43. }
  44. names+='^';
  45.  
  46. wdt_enable(WDTO_4S);
  47. }
  48.  
  49. void loop() {
  50. wdt_reset();
  51. for(int i=0;i<=no_of_switches;i++){
  52. int b = checkButton(i);
  53. if (b == 1) clickEvent(i);
  54. if (b == 2) doubleClickEvent(i);
  55. if (b == 3) holdEvent(i);
  56. if (b == 4) longHoldEvent(i);
  57. }
  58.  
  59. if (Serial.available()) {
  60. req = Serial.readString();
  61. s = "";
  62.  
  63. if (req.indexOf("gpio") != -1)
  64. {
  65. String val = req.substring(req.lastIndexOf("gpio")+4);
  66. int index = val.toInt()-1;
  67.  
  68. digitalWrite(pins[index], !digitalRead(pins[index]));
  69.  
  70. s += pins[index];
  71. s += " is ";
  72. s += (digitalRead(pins[index]) == HIGH) ? "ON " : "OFF ";
  73. }
  74. else if (req.indexOf("status") != -1){
  75. String response_code="";
  76. for(int i=0;i<no_of_switches;i++){
  77. response_code=response_code+(digitalRead(pins[i])==HIGH?1:0);
  78. }
  79. s += response_code;
  80. }
  81. else if (req.indexOf("setstate") != -1){
  82. String val = req.substring(req.lastIndexOf("setstate")+8);
  83. for(int i=0;i<no_of_switches;i++){
  84. if(val.charAt(i)=='1')
  85. digitalWrite(pins[i],HIGH);
  86. else
  87. digitalWrite(pins[i],LOW);
  88. }
  89. s += "saved";
  90. }
  91. else if (req.indexOf("setname") != -1)
  92. {
  93. String val = req.substring(req.lastIndexOf("setname")+7);
  94. for(int i=0;i<val.length();i++)
  95. EEPROM.put(i, val.charAt(i));
  96. names=val;
  97. s="saved";
  98.  
  99. }
  100. else if (req.indexOf("names") != -1)
  101. {
  102. s=names;
  103. }
  104. else
  105. {
  106. s="invalid request";
  107. }
  108. Serial.println(s);
  109. }
  110. }
  111. //=================================================
  112. // Events to trigger
  113.  
  114. void clickEvent(int pos) {
  115. digitalWrite(pins[pos],!digitalRead(pins[pos]));
  116.  
  117. }
  118. void doubleClickEvent(int pos) {
  119. boolean bol=!digitalRead(pins[pos]);
  120. for(int i=14;i<=19;i++){
  121. digitalWrite(i,bol);
  122. }
  123. }
  124. void holdEvent(int pin) {
  125. }
  126. void longHoldEvent(int pin) {
  127. }
  128.  
  129. //=================================================
  130. // MULTI-CLICK: One Button, Multiple Events
  131.  
  132. // Button timing variables
  133. int debounce = 20; // ms debounce period to prevent flickering when pressing or releasing the button
  134. int DCgap = 250; // max ms between clicks for a double click event
  135. int holdTime = 1000; // ms hold period: how long to wait for press+hold event
  136. int longHoldTime = 3000; // ms long hold period: how long to wait for press+hold event
  137.  
  138. // Button variables
  139. boolean buttonVal[] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH}; // value read from button
  140. boolean buttonLast[] = {HIGH,HIGH,HIGH,HIGH,HIGH,HIGH}; // buffered value of the button's previous state
  141. boolean DCwaiting[] = {false,false,false,false,false,false}; // whether we're waiting for a double click (down)
  142. boolean DConUp[] = {false,false,false,false,false,false}; // whether to register a double click on next release, or whether to wait and click
  143. boolean singleOK[] = {true,true,true,true,true,true}; // whether it's OK to do a single click
  144. long downTime[] = {-1,-1,-1,-1,-1,-1}; // time the button was pressed down
  145. long upTime[] = {-1,-1,-1,-1,-1,-1}; // time the button was released
  146. boolean ignoreUp[] = {false,false,false,false,false,false}; // whether to ignore the button release because the click+hold was triggered
  147. boolean waitForUp[] = {false,false,false,false,false,false}; // when held, whether to wait for the up event
  148. boolean holdEventPast[] = {false,false,false,false,false,false}; // whether or not the hold event happened already
  149. boolean longHoldEventPast[] = {false,false,false,false,false,false};// whether or not the long hold event happened already
  150.  
  151. int checkButton(int buttonPin) {
  152. int event = 0;
  153. buttonVal[buttonPin] = digitalRead(buttonPin+3);
  154. // Button pressed down
  155. if (buttonVal[buttonPin] == LOW && buttonLast[buttonPin] == HIGH && (millis() - upTime[buttonPin]) > debounce)
  156. {
  157. downTime[buttonPin] = millis();
  158. ignoreUp[buttonPin] = false;
  159. waitForUp[buttonPin] = false;
  160. singleOK[buttonPin] = true;
  161. holdEventPast[buttonPin] = false;
  162. longHoldEventPast[buttonPin] = false;
  163. if ((millis()-upTime[buttonPin]) < DCgap && DConUp[buttonPin] == false && DCwaiting[buttonPin] == true) DConUp[buttonPin] = true;
  164. else DConUp[buttonPin] = false;
  165. DCwaiting[buttonPin] = false;
  166. }
  167. // Button released
  168. else if (buttonVal[buttonPin] == HIGH && buttonLast[buttonPin] == LOW && (millis() - downTime[buttonPin]) > debounce)
  169. {
  170. if (not ignoreUp[buttonPin])
  171. {
  172. upTime[buttonPin] = millis();
  173. if (DConUp[buttonPin] == false) DCwaiting[buttonPin] = true;
  174. else
  175. {
  176. event = 2;
  177. DConUp[buttonPin] = false;
  178. DCwaiting[buttonPin] = false;
  179. singleOK[buttonPin] = false;
  180. }
  181. }
  182. }
  183. // Test for normal click event: DCgap expired
  184. if ( buttonVal[buttonPin] == HIGH && (millis()-upTime[buttonPin]) >= DCgap && DCwaiting[buttonPin] == true && DConUp[buttonPin] == false && singleOK[buttonPin] == true && event != 2)
  185. {
  186. event = 1;
  187. DCwaiting[buttonPin] = false;
  188. }
  189. // Test for hold
  190. if (buttonVal[buttonPin] == LOW && (millis() - downTime[buttonPin]) >= holdTime) {
  191. // Trigger "normal" hold
  192. if (not holdEventPast[buttonPin])
  193. {
  194. event = 3;
  195. waitForUp[buttonPin] = true;
  196. ignoreUp[buttonPin] = true;
  197. DConUp[buttonPin] = false;
  198. DCwaiting[buttonPin] = false;
  199. //downTime[buttonPin] = millis();
  200. holdEventPast[buttonPin] = true;
  201. }
  202. // Trigger "long" hold
  203. if ((millis() - downTime[buttonPin]) >= longHoldTime)
  204. {
  205. if (not longHoldEventPast[buttonPin])
  206. {
  207. event = 4;
  208. longHoldEventPast[buttonPin] = true;
  209. }
  210. }
  211. }
  212. buttonLast[buttonPin] = buttonVal[buttonPin];
  213. return event;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement