Advertisement
pippero

Untitled

Jan 6th, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. //Multi PWM handler
  2. #include <Arduino.h>
  3. #include <TimerOne.h>
  4.  
  5. #define MAX_LED 9
  6.  
  7. unsigned char PWMcounter = 0;
  8. unsigned char PWM1 = 50;
  9. unsigned char tmp_i;
  10.  
  11. struct led {
  12. unsigned char pin;
  13. unsigned char PWMlevel;
  14. bool status;
  15. bool last_status;
  16. };
  17.  
  18. led leds[MAX_LED] = { {.pin=2, .PWMlevel=5},
  19. {.pin=3, .PWMlevel=10},
  20. {.pin=4, .PWMlevel=15},
  21. {.pin=5, .PWMlevel=20},
  22. {.pin=6, .PWMlevel=25},
  23. {.pin=7, .PWMlevel=30},
  24. {.pin=8, .PWMlevel=35},
  25. {.pin=9, .PWMlevel=40},
  26. {.pin=10, .PWMlevel=45}};
  27.  
  28.  
  29. void multiPWMhandler () {
  30. PWMcounter++;
  31. if (PWMcounter>50) PWMcounter = 0;
  32. for (tmp_i=0; tmp_i<MAX_LED; tmp_i++) {
  33. leds[tmp_i].status = PWMcounter < leds[tmp_i].PWMlevel;
  34. if (leds[tmp_i].status != leds[tmp_i].last_status) {
  35. leds[tmp_i].last_status = leds[tmp_i].status;
  36. digitalWrite(leds[tmp_i].pin,leds[tmp_i].status);
  37. }
  38. /* if (PWMcounter < leds[tmp_i].PWMlevel) digitalWrite(leds[tmp_i].pin,HIGH);
  39. else digitalWrite(leds[tmp_i].pin,LOW); */
  40. }
  41.  
  42. }
  43.  
  44. void setup() {
  45. for (tmp_i=0; tmp_i<MAX_LED; tmp_i++) {
  46. pinMode(leds[tmp_i].pin, OUTPUT);
  47. leds[tmp_i].status = false;
  48. leds[tmp_i].last_status = true;
  49. }
  50. Timer1.initialize(100); // Run every 0.00001 seconds 10Khz
  51. Timer1.attachInterrupt(multiPWMhandler);
  52.  
  53. }
  54.  
  55. void loop() {
  56.  
  57. }
  58.  
  59. RAW Paste Data
  60. #include <Arduino.h>
  61. #include <TimerOne.h>
  62.  
  63. #define MAX_LED 9
  64.  
  65. unsigned char PWMcounter = 0;
  66. unsigned char PWM1 = 50;
  67. unsigned char tmp_i;
  68.  
  69. struct led {
  70. unsigned char pin;
  71. unsigned char PWMlevel;
  72. bool status;
  73. bool last_status;
  74. };
  75.  
  76. led leds[MAX_LED] = { {.pin=2, .PWMlevel=5},
  77. {.pin=3, .PWMlevel=10},
  78. {.pin=4, .PWMlevel=15},
  79. {.pin=5, .PWMlevel=20},
  80. {.pin=6, .PWMlevel=25},
  81. {.pin=7, .PWMlevel=30},
  82. {.pin=8, .PWMlevel=35},
  83. {.pin=9, .PWMlevel=40},
  84. {.pin=10, .PWMlevel=45}};
  85.  
  86.  
  87. void multiPWMhandler () {
  88. PWMcounter++;
  89. if (PWMcounter>50) PWMcounter = 0;
  90. for (tmp_i=0; tmp_i<MAX_LED; tmp_i++) {
  91. leds[tmp_i].status = PWMcounter < leds[tmp_i].PWMlevel;
  92. if (leds[tmp_i].status != leds[tmp_i].last_status) {
  93. leds[tmp_i].last_status = leds[tmp_i].status;
  94. digitalWrite(leds[tmp_i].pin,leds[tmp_i].status);
  95. }
  96. /* if (PWMcounter < leds[tmp_i].PWMlevel) digitalWrite(leds[tmp_i].pin,HIGH);
  97. else digitalWrite(leds[tmp_i].pin,LOW); */
  98. }
  99.  
  100. }
  101.  
  102. void setup() {
  103. for (tmp_i=0; tmp_i<MAX_LED; tmp_i++) {
  104. pinMode(leds[tmp_i].pin, OUTPUT);
  105. leds[tmp_i].status = false;
  106. leds[tmp_i].last_status = true;
  107. }
  108. Timer1.initialize(100); // Run every 0.00001 seconds 10Khz
  109. Timer1.attachInterrupt(multiPWMhandler);
  110.  
  111. }
  112.  
  113. void loop() {
  114.  
  115. }
  116.  
  117. My Pastes
  118. Nothing here yet...
  119. Public Pastes
  120. Untitled
  121. MatLab | 6 min ago
  122. hw061
  123. C | 7 min ago
  124. u6-4.5.1
  125. Python | 17 min ago
  126. Задание 4.5.1
  127. Python | 28 min ago
  128. Untitled
  129. JavaScript | 39 min ago
  130. ReactorControl3
  131. Lua | 51 min ago
  132. Script - Classific...
  133. Lua | 1 hour ago
  134. PlayerMovement.cs...
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement