Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. /* This code was written for the Stitch the Loop e-textiles curriculum by the
  2. Exploring Computer Science e-textiles team. ECS 2018 GPL V3 for non commercial use.
  3. ECS 2018 CC- BY NC SA. */
  4.  
  5. /*
  6. ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
  7. This program is an EXAMPLE of the many possible solutions. This code will compile as is.
  8. ◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
  9.  
  10. */
  11. /* STUDENT META HERE
  12. STUDENT NAME(S)
  13. Date Written
  14. Brief description of what program does here.
  15. */
  16.  
  17. // The first section is where to declare global objects and call up additional files the program needs to use.
  18. /*NAMING SECTION: We name things to keep track of them easily.
  19. What will you name your other lights?
  20. */
  21. //1,2,9,12
  22. int led1 = 1;
  23. int led2old = 2;
  24. int led2 = 3;
  25. int led3 = 9;
  26. int led4 = 6;
  27. int led4old = 12;
  28.  
  29.  
  30. int blinkCounter = 1;
  31. int heartCounter = 1;
  32. int fadeBrightness = 5;
  33. bool fadeUp = true;
  34.  
  35. /*SETUP SECTION: Put things here that you only need to do once:
  36. For instance, OUTPUTS (lights)
  37. */
  38. void setup() {
  39. // The second section is for things that only need to be done once at the beginning of the program.
  40. pinMode(led1, OUTPUT);
  41. pinMode(led2, OUTPUT);
  42. pinMode(led2old, OUTPUT);
  43. pinMode(led3, OUTPUT);
  44. pinMode(led4, OUTPUT);
  45. pinMode(led4old, OUTPUT);
  46.  
  47. }
  48.  
  49.  
  50. void loop() {
  51. /* The third section is for things that happen repeatedly in the program loop
  52. while the program is running. The code is executed in the order coded. */
  53.  
  54.  
  55. blinkPattern(led1);
  56. twinkle(led2);
  57.  
  58. twinkle(led2old);
  59. heartBeat(led3);
  60. fade(led4);
  61. fade(led4old);
  62.  
  63. delay(100);
  64. }
  65.  
  66. // The fourth section is for functions that are called up by the third section.
  67.  
  68. void randomPattern(int led)
  69. {
  70. //random brightness 50-250
  71. int chance = random(1000);
  72. int brightness = random(200) + 50;
  73. if (chance <500)
  74. analogWrite(led, brightness);
  75. else
  76. off(led);
  77. }
  78.  
  79. void twinkle(int led)
  80. {
  81. int brightness = random(250);
  82. analogWrite(led, brightness);
  83.  
  84. }
  85.  
  86. void blinkPattern(int led)
  87. {
  88. if (blinkCounter == 1)
  89. on(led);
  90. else if (blinkCounter == 6)
  91. off(led);
  92. else if(blinkCounter > 10)
  93. blinkCounter = 0;
  94.  
  95. blinkCounter ++;
  96. }
  97.  
  98. void heartBeat(int led)
  99. {
  100. if (heartCounter == 1)
  101. on(led);
  102. else if (heartCounter == 3)
  103. off(led);
  104. else if (heartCounter == 5)
  105. on(led);
  106. else if (heartCounter == 7)
  107. off(led);
  108. else if(heartCounter > 15)
  109. heartCounter = 0;
  110.  
  111. heartCounter ++;
  112. }
  113.  
  114. void fade(int led)
  115. {
  116. if (fadeUp) {
  117. if(fadeBrightness >= 200)
  118. fadeUp = false;
  119. //fadeBrightness = 5;
  120. else
  121. fadeBrightness += 10;
  122. }
  123. else {
  124. if(fadeBrightness <= 5)
  125. fadeUp = true;
  126. //fadeBrightness = 5;
  127. else
  128. fadeBrightness -= 10;
  129. }
  130.  
  131. analogWrite(led, fadeBrightness);
  132.  
  133. }
  134.  
  135. void on(int led) {
  136. digitalWrite(led, HIGH);
  137. }
  138.  
  139. void off(int led) {
  140. digitalWrite(led, LOW);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement