1. /*
  2. Author: voidcast
  3. Created on: 1/7/2012
  4. URL: http://voidcast.wordpress.com
  5.  
  6. ********************************
  7. * Simple Arduino LED flasher *
  8. ********************************
  9.  
  10. Licensed under Creative Commons - Attribution (http://creativecommons.org/licenses/by/3.0/)
  11. You are free to share, modify and use this code for commercial purposes as long as you attribute the
  12. original author
  13.  
  14. */
  15.  
  16. /*
  17. All the LEDs need to be connected to the arduino pins in sequence.
  18. The pin where they start from and where they end need to be put in below.
  19. Everything else remains unchanged
  20.  
  21. I have used 1K resistors in series with most of the LEDs except for a few which weren't bright enough.
  22. For those I've used 220 Ohm resistors. Arduino allows a max of 40mA of current through each of its pins,
  23. but the recommended amount is not to exceed 20mA. Assuming a worst-case drop of 1V (it's normally 2V) across the LEDs
  24. and given the source to be 5V on the arduino, the current that flows through is (5-1)/1K = 4mA for most of them
  25. and (5-1)/220 = 18mA for the others with lower resistance. These are well within the limits.
  26.  
  27. Pin numbers can be from 0-13 but keep in mind that pin 13 already has a 1K resistance to it.
  28. */
  29.  
  30. int startPin = 1;
  31. int endPin = 12;
  32.  
  33. void setup()
  34. {
  35. //Setup all the pins for OUTPUT as given below
  36. for(int i=startPin; i<=endPin; i++)
  37. {
  38. pinMode(i, OUTPUT);
  39. }
  40. //Serial.begin(9600);
  41.  
  42. }
  43.  
  44. //Turn on the led at the specified pin
  45. void ledOn(int pin)
  46. {
  47. digitalWrite(pin, HIGH);
  48. }
  49.  
  50.  
  51. //Turn off the led at the specified pin
  52. void ledOff(int pin)
  53. {
  54. digitalWrite(pin, LOW);
  55. }
  56.  
  57.  
  58. /*
  59. Have a single light that traverses the length of the series.
  60. Can be either forward or backward.
  61. */
  62. void ledSerial(int time, int count, boolean forward)
  63. {
  64. for(int j=1; j<=count; j++)
  65. {
  66. if(forward)
  67. {
  68. for(int i = startPin; i <= endPin; i++)
  69. {
  70. ledOn(i);
  71. delay(time);
  72. ledOff(i);
  73. }
  74. }
  75. else
  76. {
  77. for(int i = endPin; i >= startPin; i--)
  78. {
  79. ledOn(i);
  80. delay(time);
  81. ledOff(i);
  82. }
  83. }
  84. }
  85. }
  86.  
  87. /*
  88. All the LEDs blink together the specified (count) number of times
  89. */
  90. void ledBlinkAll(int time, int count)
  91. {
  92. ledReset();
  93. for(int j = 1; j <= count; j++)
  94. {
  95. for(int i = startPin; i <= endPin; i++)
  96. {
  97. ledOn(i);
  98. }
  99. delay(time);
  100. for(int i = startPin; i <= endPin; i++)
  101. {
  102. ledOff(i);
  103. }
  104. delay(time);
  105. }
  106. }
  107.  
  108. /*
  109. Have the LEDs blink alternately
  110. */
  111. void ledBlinkAlternate(int time, int count)
  112. {
  113. ledReset();
  114. for(int j = 1; j <= count; j++)
  115. {
  116. for(int i = startPin; i <= endPin; i+=2)
  117. {
  118. ledOn(i);
  119. }
  120. delay(time);
  121. for(int i = startPin; i <= endPin; i+=2)
  122. {
  123. ledOff(i);
  124. }
  125. for(int i = startPin+1; i <= endPin; i+=2)
  126. {
  127. ledOn(i);
  128. }
  129. delay(time);
  130. for(int i = startPin+1; i <= endPin; i+=2)
  131. {
  132. ledOff(i);
  133. }
  134. }
  135. }
  136.  
  137.  
  138. //Turn off all the LEDs
  139. void ledReset()
  140. {
  141. for(int i = startPin; i <= endPin; i++)
  142. {
  143. ledOff(i);
  144. }
  145. }
  146.  
  147. /*
  148. The LEDs light up in order and turn off in order, like a snake that grows and shrinks.
  149. This one can be modified to accept another argument so that you can specify whether you want the "snake"
  150. to grow or shrink. But I figured this is what I would use the most.
  151. */
  152.  
  153. void ledStretch(int time, int count, boolean forward)
  154. {
  155. for(int j=1; j<=count; j++)
  156. {
  157. if(forward)
  158. {
  159. for(int i=startPin; i<=endPin; i++)
  160. {
  161. ledOn(i);
  162. delay(time);
  163. }
  164. for(int i=startPin; i<=endPin; i++)
  165. {
  166. ledOff(i);
  167. delay(time);
  168. }
  169. }
  170. else
  171. {
  172. for(int i=endPin; i>=startPin; i--)
  173. {
  174. ledOn(i);
  175. delay(time);
  176. }
  177. for(int i=endPin; i>=startPin; i--)
  178. {
  179. ledOff(i);
  180. delay(time);
  181. }
  182. }
  183. }
  184. }
  185.  
  186.  
  187. // Appears as if the light is entering from one side and filling all the LEDs up
  188. void ledFiller(int time, int count, boolean forward)
  189. {
  190. int lastFilledLed = endPin; //Just so that it's not garbage or zero. This is set again depending on whether you want forward or backward.
  191. for(int j=1; j<=count; j++)
  192. {
  193. ledReset();
  194. if(forward)
  195. {
  196. lastFilledLed = endPin;
  197. while(lastFilledLed >= startPin)
  198. {
  199. for(int i=startPin; i<=lastFilledLed; i++)
  200. {
  201. ledOn(i);
  202. delay(time);
  203. ledOff(i);
  204. }
  205. ledOn(lastFilledLed);
  206. lastFilledLed--;
  207. }
  208. }
  209. else
  210. {
  211. lastFilledLed = startPin;
  212. while(lastFilledLed <= endPin)
  213. {
  214. for(int i=endPin; i>=lastFilledLed; i--)
  215. {
  216. ledOn(i);
  217. delay(time);
  218. ledOff(i);
  219. }
  220. ledOn(lastFilledLed);
  221. lastFilledLed++;
  222. }
  223. }
  224. }
  225. }
  226.  
  227.  
  228. /*
  229. Two lights start from either end or from the middle and either grow inwards or outwards
  230. This one requires 2 parallel streams which I call "threads" here. As one increases the other decreases.
  231. Odd number of LEDs needs to be handled differently from even number of LEDs
  232. */
  233. void ledSweep(int time, int count, boolean sweepIn)
  234. {
  235. for(int j=1; j<=count; j++)
  236. {
  237. int numberOfLeds = endPin - startPin + 1;
  238. ledReset();
  239. delay(time);
  240. if(numberOfLeds % 2 == 0) //Even number of LEDs
  241. {
  242. if(sweepIn)
  243. {
  244. int thread1 = endPin;
  245. int thread2 = startPin;
  246.  
  247. while(thread1 > thread2)
  248. {
  249. ledOn(thread1);
  250. ledOn(thread2);
  251. delay(time);
  252. thread1--;
  253. thread2++;
  254. }
  255. }
  256. else
  257. {
  258. int thread1 = numberOfLeds/2;
  259. int thread2 = thread1 + 1;
  260. while(thread2 <= endPin)
  261. {
  262. ledOn(thread1);
  263. ledOn(thread2);
  264. delay(time);
  265. thread1--;
  266. thread2++;
  267. }
  268. }
  269. }
  270. else //Odd number of LEDs
  271. {
  272. if(sweepIn)
  273. {
  274. int thread1 = endPin;
  275. int thread2 = startPin;
  276. while(thread1 > thread2)
  277. {
  278. ledOn(thread1);
  279. ledOn(thread2);
  280. delay(time);
  281. thread1--;
  282. thread2++;
  283. }
  284. ledOn(thread1); //Turn on the middle Led. At this point, thread1 = thread2 = middleLed
  285. delay(time);
  286. }
  287. else
  288. {
  289. int middleLed = (numberOfLeds + 1)/2 + startPin - 1; //Get the pin number of the middle Led
  290. int thread1 = middleLed - 1;
  291. int thread2 = middleLed + 1;
  292. ledOn(middleLed);
  293. delay(time);
  294. while(thread2 <= endPin)
  295. {
  296. ledOn(thread1);
  297. ledOn(thread2);
  298. delay(time);
  299. thread1--;
  300. thread2++;
  301. }
  302.  
  303. }
  304. }
  305. }
  306. }
  307.  
  308. void loop()
  309. {
  310. ledSweep(100,3,true);
  311. ledSweep(100,3,false);
  312. ledSerial(40, 6, true);
  313. ledSerial(40, 6, false);
  314. ledBlinkAll(400, 4);
  315. ledBlinkAlternate(400,8);
  316. ledStretch(50, 3, true);
  317. ledStretch(50, 3, false);
  318. ledFiller(50, 1, true);
  319. ledFiller(50, 1, false);
  320. ledFiller(50, 1, true);
  321. ledFiller(50, 1, false);
  322. }