Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #include <SimpleTimer.h>
  2.  
  3. SimpleTimer timer;
  4.  
  5.  
  6.  
  7. const int buttonPin = 2; // the button is connected to pin 2
  8. const int Led0 = 3; // first LED is connected to pin 3
  9. const int NumLeds = 4; // we have four LEDs (at the moment)
  10. const int Temp = A3;
  11.  
  12. // struct for buttons
  13. enum b_p {P_NONE, P_SHORT, P_LONG, P_DOUBLE};
  14. // data type takes three values.
  15. typedef enum b_p b_press;
  16. //now we can just use b_press as a type.
  17.  
  18. struct B_struct {
  19. int port; //the port on which this button operates
  20. int state = HIGH; //the state of the port (high = unpressed)
  21. unsigned long last_change = 0; //time of thelast press
  22. bool d_1;
  23. b_press response = P_NONE,P_SHORT, P_LONG, P_DOUBLE;
  24. };
  25.  
  26. typedef struct B_struct Button;
  27. Button * buttonReg[20] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  28. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
  29. void pciSetup(byte pin)
  30.  
  31. {
  32. *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
  33. PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
  34. PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
  35. }
  36.  
  37. // ISR sets up an interrupt service routine for the specified interrupt vector
  38. ISR (PCINT0_vect) // handle pin change interrupt for pins 8 to 13
  39. {
  40. // here we will loop through the required part of buttonReg
  41. // if a button is registered (non-null) we call its poll() method
  42. // that checks if button has actually changed
  43. for (int i=8; i<=13; i++) {
  44. if (buttonReg[i] != NULL) poll(buttonReg[i]);
  45. }
  46. }
  47.  
  48. ISR (PCINT1_vect) // handle pin change interrupt for pins 8 to 13
  49. {
  50. // here we will loop through the required part of buttonReg
  51. // if a button is registered (non-null) we call its poll() method
  52. // that checks if button has actually changed
  53. for (int i=14; i<=19; i++) {
  54. if (buttonReg[i] != NULL) poll(buttonReg[i]);
  55. }
  56. }
  57.  
  58. ISR (PCINT2_vect) // handle pin change interrupt for pins 8 to 13
  59. {
  60. // here we will loop through the required part of buttonReg
  61. // if a button is registered (non-null) we call its poll() method
  62. // that checks if button has actually changed
  63. for (int i=0; i<=7; i++) {
  64. if (buttonReg[i] != NULL) poll(buttonReg[i]);
  65. }
  66. }
  67.  
  68.  
  69.  
  70. void buttonInit(Button * B, int p) { //initializes a Button working on pin p
  71. B->port = p;
  72. pinMode(p, INPUT);
  73. buttonReg[p] = B;
  74. pciSetup(p);
  75. }
  76.  
  77.  
  78. // check the button, to be called each time round the loop
  79. void poll(Button * B) {
  80. //Serial.println("poll"); // Debuffing code
  81. if (millis()-B->last_change<10) {
  82. return;
  83. }
  84. int b = digitalRead(B->port);
  85. if (b!= B->state) {
  86. if (b==HIGH) {
  87. unsigned long p_time = millis()-B->last_change;
  88. if (p_time<150) {
  89. if (B->d_1) { //had one press, this is the second
  90. B->response = P_DOUBLE;
  91. B->d_1 = false;
  92. } else {
  93. B->d_1 = true;
  94. int timeout = timer.setTimeout(160, buttonTimeout);
  95. }
  96. } else { // longer than 150ms
  97. if (p_time<250) B->response = P_SHORT;
  98. else B->response = P_LONG;
  99. }
  100. B->state = HIGH;
  101. B->last_change = millis(); //to allow timing of the gap
  102. } else { //b==LOW
  103. B->last_change = millis();
  104. B->state = LOW;
  105. }
  106. } else { // no change but check for timeout in middle of double
  107. if (B->d_1) { // button up looking for double
  108. unsigned long p_time = millis()-B->last_change;
  109. if (p_time>150) { // too long, cancel double press
  110. B->d_1 =false; // no longer looking for double press
  111. B->response = P_SHORT; // declare first press a short press
  112. }
  113. }
  114. }
  115. }
  116. // called to find state of button. Post: response = P_NONE
  117. b_press ask(Button * B) {
  118. b_press bp = B->response;
  119. B->response = P_NONE;
  120. return bp;
  121. }
  122. // count button presses and return the number
  123. int buttonState = HIGH; // assume button starts off unpressed
  124. int buttonCount = 0; // start counting at zero
  125. int countButton() {
  126. int bs = digitalRead(buttonPin);
  127. if ((bs == LOW) && (bs != buttonState)) {
  128. buttonState = bs;
  129. buttonCount++;
  130. } else if ((bs == HIGH) && (bs != buttonState)) buttonState = bs;
  131. return buttonCount;
  132. }
  133.  
  134.  
  135. void buttonTimeout() {
  136. for (int a=0; a < 20; a++) {
  137. if (buttonReg[a]->d_1) poll(buttonReg[a]);
  138. }
  139. }
  140.  
  141.  
  142. // control LEDs
  143. void controlLEDs(int value) {
  144. //int bitCount = 1;
  145. // Program a loop, where:
  146. // the loop counter (call it i) runs from Led0 to Led0+Numleds-1
  147. // each time through the loop digitalWrite(i,value) is called
  148. for (int i=Led0; i<Led0+NumLeds; i++) {
  149. if (i==Led0 + value) {
  150. digitalWrite(i,HIGH); }
  151. else digitalWrite(i,LOW);
  152. }
  153. }
  154. // Hint: you already have a similar loop in the program.
  155. Button B;
  156. int LEDs[4];
  157.  
  158. void setup() {
  159. pinMode(LED_BUILTIN, OUTPUT);
  160. // set up the button as an input
  161. buttonInit(&B,buttonPin);
  162. for (int i=Led0; i<Led0+NumLeds; i++) {
  163. pinMode(i,OUTPUT);
  164. LEDs[i-Led0] = LOW;
  165. Serial.begin(9600);
  166. Serial.println("serial initialised");
  167. }
  168. analogReference(INTERNAL);
  169. }
  170.  
  171. void valuetest (bool v) {
  172. if (v) Serial.println("Recieving value");
  173. else Serial.println("Not recieving value");
  174. }
  175.  
  176. float cel(int input) {
  177. return ((float)input)*110.0/1023.0;
  178. }
  179. bool valuesend = false;
  180. float tempval = 0;
  181.  
  182. void loop() {
  183. int ctrlVal = analogRead(A0);
  184. b_press bp = ask(&B);
  185. switch (bp) {
  186. case P_DOUBLE:
  187. valuesend = false;
  188. valuetest(valuesend);
  189. //if (LEDs[2] == LOW) {
  190. //LEDs[2] = HIGH;
  191. //digitalWrite(Led0+2,HIGH);
  192. //} else {
  193. //LEDs[2] = LOW;
  194. // digitalWrite(Led0+2,LOW);
  195. // }
  196. // break;
  197. case P_LONG:
  198. valuesend = true;
  199. valuetest(valuesend);
  200. //if (LEDs[1] == LOW) {
  201. //LEDs[1] = HIGH;
  202. //digitalWrite(Led0+1,HIGH);
  203. //} else {
  204. //LEDs[1] = LOW;
  205. //digitalWrite(Led0+1,LOW);
  206. //}
  207. break;
  208. case P_SHORT:
  209. valuesend = !valuesend;
  210. valuetest(valuesend);
  211. //if (LEDs[0] == LOW) {
  212. //LEDs[0] = HIGH;
  213. //digitalWrite(Led0,HIGH);
  214. //} else {
  215. //LEDs[0] = LOW;
  216. //digitalWrite(Led0,LOW);
  217. //}
  218. break;
  219. default: ;
  220. //int Temp = analogRead(A2);
  221. //Serial.println(Temp);
  222.  
  223. tempval = cel(analogRead(A1));
  224.  
  225. if (valuesend) {
  226. Serial.println("Control = ");
  227. Serial.println(cel(ctrlVal));
  228. Serial.println("Temperture = ");
  229. Serial.println(tempval);
  230.  
  231. if (tempval < ctrlVal - 0.5) {
  232. controlLEDs(0);
  233. } else if (tempval > ctrlVal + 0.5) {
  234. controlLEDs(2);
  235. } else {
  236. controlLEDs(1);
  237. }
  238. }
  239. }
  240. delay(100);
  241. }
  242. // wait for 1/20 second before looping
  243. //timer.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement