andyb2000

Arduino xmas lights 2013

Dec 20th, 2013
144
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Christmas lights 2013 */
  2. /* Andy Brown */
  3.  
  4. /* define inputs (14 is analog0 15 is analog1) */
  5. #include <avr/wdt.h>
  6.  
  7. int pir[] = {14,15,16,17,18};
  8. int led[] = {7,8,9,10,11,12,6};
  9. long ledstate[] = {0,0,0,0,0,0,0};
  10. int curstate[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW};
  11.  
  12. int val = 0; // variable for reading the pin status
  13. int movement_trigger = 0;
  14. int normal_sequence = 0;
  15. int anymovement = 0;
  16. long loopcounter = 0;
  17. long sequence_trigger = 0;
  18.  
  19. void setup() {
  20. pinMode(led[0], OUTPUT);
  21. pinMode(led[1], OUTPUT);
  22. pinMode(led[2], OUTPUT);
  23. pinMode(led[3], OUTPUT);
  24. pinMode(led[4], OUTPUT);
  25. pinMode(led[5], OUTPUT);
  26. pinMode(led[6], OUTPUT);
  27.  
  28. pinMode(pir[0], INPUT); // declare sensor as input
  29. pinMode(pir[1], INPUT);
  30. pinMode(pir[2], INPUT);
  31. pinMode(pir[3], INPUT);
  32. pinMode(pir[4], INPUT);
  33.  
  34. curstate[0] = LOW;
  35. curstate[1] = LOW;
  36. curstate[2] = LOW;
  37. curstate[3] = LOW;
  38. curstate[4] = LOW;
  39. curstate[5] = LOW;
  40. curstate[6] = LOW;
  41.  
  42. digitalWrite(led[0], HIGH);
  43. digitalWrite(led[1], HIGH);
  44. digitalWrite(led[2], HIGH);
  45. digitalWrite(led[3], HIGH);
  46. digitalWrite(led[4], HIGH);
  47. digitalWrite(led[5], LOW);
  48. digitalWrite(led[6], LOW);
  49. Serial.begin(9600);
  50. Serial.println("Setup init");
  51. wdt_enable(WDTO_4S);
  52. }
  53.  
  54. void loop(){
  55. /* Check each PIR */
  56. for (int i=0; i <= 4; i++){
  57. val = digitalRead(pir[i]);
  58. wdt_reset();
  59. if (val == HIGH) {
  60. digitalWrite(led[i], LOW);
  61. digitalWrite(led[6], LOW);
  62. curstate[i]=HIGH;
  63. sequence_trigger=0;
  64. normal_sequence=0;
  65. ledstate[i]=loopcounter+700; // set loopcounter to when light went on
  66. Serial.println("PIR detected as on");
  67. Serial.println(i);
  68. } else {
  69. // check loopcounter, so we turn off only after a preset time
  70. if ((ledstate[i] <= loopcounter) && (curstate[i] == HIGH)) {
  71. digitalWrite(led[i], HIGH);
  72. ledstate[i]=0;
  73. curstate[i]=LOW;
  74. sequence_trigger=0;
  75. Serial.println("PIR switched off");
  76. Serial.println(i);
  77. };
  78. };
  79. };
  80. wdt_reset();
  81.  
  82. if (sequence_trigger == 0) {
  83. for (int z=0; z <= 4; z++) {
  84. if (curstate[z] == LOW) {
  85. digitalWrite(led[z], HIGH);
  86. };
  87. };
  88. };
  89. loopcounter += 1;
  90. // Just to handle overflow here - reset things
  91. if (loopcounter > 1000000000) {
  92. loopcounter=0;
  93. for (int y=0; y <= 4; y++){
  94. ledstate[y] = 0;
  95. };
  96. sequence_trigger=0;
  97. Serial.println("Resetting all loops");
  98. };
  99.  
  100. // Check to do sequencer
  101. if (ledstate[0] == 0 && ledstate[1] == 0 && ledstate[2] == 0 && ledstate[3] == 0 && ledstate[4] == 0) {
  102. if (sequence_trigger == 0) {
  103. sequence_trigger=loopcounter+3000;
  104. Serial.println("setting sequencer trigger values");
  105. };
  106. };
  107. if ((sequence_trigger < loopcounter) && (sequence_trigger != 0)) {
  108. sequencer();
  109. };
  110. wdt_reset();
  111. delay(2);
  112. wdt_reset();
  113. }
  114.  
  115. void sequencer() {
  116. /* Do the sequence */
  117.  
  118. /* normal_sequence = 0 all on
  119. 1 = 0 2 4
  120. 2 = 1 3 5
  121. 3 = 0 5 3
  122. 4 = 2 1 4
  123. 5 = 012345 flicker */
  124. wdt_reset();
  125. if (normal_sequence >= 6810 && normal_sequence <=7200) {
  126. digitalWrite(led[0], HIGH);
  127. digitalWrite(led[1], LOW);
  128. digitalWrite(led[2], LOW);
  129. digitalWrite(led[3], HIGH);
  130. digitalWrite(led[4], LOW);
  131. digitalWrite(led[5], HIGH);
  132. digitalWrite(led[6], LOW);
  133. };
  134. if (normal_sequence >=6410 && normal_sequence <=6800) {
  135. digitalWrite(led[0], LOW);
  136. digitalWrite(led[1], LOW);
  137. digitalWrite(led[2], LOW);
  138. digitalWrite(led[3], HIGH);
  139. digitalWrite(led[4], LOW);
  140. digitalWrite(led[5], LOW);
  141. digitalWrite(led[6], HIGH);
  142. };
  143. if (normal_sequence >=6010 && normal_sequence <=6400) {
  144. digitalWrite(led[0], HIGH);
  145. digitalWrite(led[1], LOW);
  146. digitalWrite(led[2], HIGH);
  147. digitalWrite(led[3], LOW);
  148. digitalWrite(led[4], HIGH);
  149. digitalWrite(led[5], LOW);
  150. digitalWrite(led[6], HIGH);
  151. };
  152. if (normal_sequence >=5610 && normal_sequence <=6000) {
  153. digitalWrite(led[0], LOW);
  154. digitalWrite(led[1], HIGH);
  155. digitalWrite(led[2], LOW);
  156. digitalWrite(led[3], HIGH);
  157. digitalWrite(led[4], LOW);
  158. digitalWrite(led[5], HIGH);
  159. digitalWrite(led[6], LOW);
  160. };
  161. if (normal_sequence >=5210 && normal_sequence <=5600) {
  162. digitalWrite(led[0], HIGH);
  163. digitalWrite(led[1], LOW);
  164. digitalWrite(led[2], HIGH);
  165. digitalWrite(led[3], LOW);
  166. digitalWrite(led[4], HIGH);
  167. digitalWrite(led[5], LOW);
  168. digitalWrite(led[6], HIGH);
  169. };
  170. if (normal_sequence >=4810 && normal_sequence <=5200) {
  171. digitalWrite(led[0], LOW);
  172. digitalWrite(led[1], LOW);
  173. digitalWrite(led[2], LOW);
  174. digitalWrite(led[3], LOW);
  175. digitalWrite(led[4], LOW);
  176. digitalWrite(led[5], LOW);
  177. digitalWrite(led[6], LOW);
  178. };
  179. if (normal_sequence >=4410 && normal_sequence <=4800) {
  180. digitalWrite(led[0], HIGH);
  181. digitalWrite(led[1], LOW);
  182. digitalWrite(led[2], LOW);
  183. digitalWrite(led[3], LOW);
  184. digitalWrite(led[4], LOW);
  185. digitalWrite(led[5], LOW);
  186. digitalWrite(led[6], LOW);
  187. };
  188. if (normal_sequence >=4010 && normal_sequence <=4400) {
  189. digitalWrite(led[0], HIGH);
  190. digitalWrite(led[1], HIGH);
  191. digitalWrite(led[2], LOW);
  192. digitalWrite(led[3], LOW);
  193. digitalWrite(led[4], LOW);
  194. digitalWrite(led[5], LOW);
  195. digitalWrite(led[6], LOW);
  196. };
  197. if (normal_sequence >=3610 && normal_sequence <=4000) {
  198. digitalWrite(led[0], HIGH);
  199. digitalWrite(led[1], HIGH);
  200. digitalWrite(led[2], HIGH);
  201. digitalWrite(led[3], LOW);
  202. digitalWrite(led[4], LOW);
  203. digitalWrite(led[5], LOW);
  204. digitalWrite(led[6], LOW);
  205. };
  206. if (normal_sequence >=3210 && normal_sequence <=3600) {
  207. digitalWrite(led[0], HIGH);
  208. digitalWrite(led[1], HIGH);
  209. digitalWrite(led[2], HIGH);
  210. digitalWrite(led[3], HIGH);
  211. digitalWrite(led[4], LOW);
  212. digitalWrite(led[5], LOW);
  213. digitalWrite(led[6], LOW);
  214. };
  215. if (normal_sequence >=2810 && normal_sequence <=3200) {
  216. digitalWrite(led[0], HIGH);
  217. digitalWrite(led[1], HIGH);
  218. digitalWrite(led[2], HIGH);
  219. digitalWrite(led[3], HIGH);
  220. digitalWrite(led[4], HIGH);
  221. digitalWrite(led[5], LOW);
  222. digitalWrite(led[6], LOW);
  223. };
  224. if (normal_sequence >=2410 && normal_sequence <=2800) {
  225. digitalWrite(led[0], HIGH);
  226. digitalWrite(led[1], HIGH);
  227. digitalWrite(led[2], HIGH);
  228. digitalWrite(led[3], HIGH);
  229. digitalWrite(led[4], HIGH);
  230. digitalWrite(led[5], HIGH);
  231. digitalWrite(led[6], LOW);
  232. };
  233. if (normal_sequence >=2010 && normal_sequence <=2400) {
  234. digitalWrite(led[0], HIGH);
  235. digitalWrite(led[1], HIGH);
  236. digitalWrite(led[2], HIGH);
  237. digitalWrite(led[3], HIGH);
  238. digitalWrite(led[4], HIGH);
  239. digitalWrite(led[5], LOW);
  240. digitalWrite(led[6], HIGH);
  241. };
  242. if (normal_sequence >=1610 && normal_sequence <=2000) {
  243. digitalWrite(led[0], HIGH);
  244. digitalWrite(led[1], HIGH);
  245. digitalWrite(led[2], HIGH);
  246. digitalWrite(led[3], HIGH);
  247. digitalWrite(led[4], LOW);
  248. digitalWrite(led[5], HIGH);
  249. digitalWrite(led[6], HIGH);
  250. };
  251. if (normal_sequence >=1210 && normal_sequence <=1600) {
  252. digitalWrite(led[0], HIGH);
  253. digitalWrite(led[1], HIGH);
  254. digitalWrite(led[2], HIGH);
  255. digitalWrite(led[3], LOW);
  256. digitalWrite(led[4], HIGH);
  257. digitalWrite(led[5], HIGH);
  258. digitalWrite(led[6], HIGH);
  259. };
  260. if (normal_sequence >=810 && normal_sequence <=1200) {
  261. digitalWrite(led[0], HIGH);
  262. digitalWrite(led[1], HIGH);
  263. digitalWrite(led[2], LOW);
  264. digitalWrite(led[3], HIGH);
  265. digitalWrite(led[4], HIGH);
  266. digitalWrite(led[5], HIGH);
  267. digitalWrite(led[6], HIGH);
  268. };
  269. if (normal_sequence >=410 && normal_sequence <=800) {
  270. digitalWrite(led[0], HIGH);
  271. digitalWrite(led[1], LOW);
  272. digitalWrite(led[2], HIGH);
  273. digitalWrite(led[3], HIGH);
  274. digitalWrite(led[4], HIGH);
  275. digitalWrite(led[5], HIGH);
  276. digitalWrite(led[6], HIGH);
  277. };
  278. if (normal_sequence >=0 && normal_sequence <=400) {
  279. digitalWrite(led[0], LOW);
  280. digitalWrite(led[1], HIGH);
  281. digitalWrite(led[2], HIGH);
  282. digitalWrite(led[3], HIGH);
  283. digitalWrite(led[4], HIGH);
  284. digitalWrite(led[5], HIGH);
  285. digitalWrite(led[6], HIGH);
  286. };
  287. normal_sequence++;
  288. if (normal_sequence > 7200) {normal_sequence=0;};
  289. wdt_reset();
  290. };
RAW Paste Data