fork download
  1. uint16_t pwm_interval = ...;
  2. uint16_t on_time[LEDS]; // this is the input determining LED brigthenss, must be <= pwm_interval
  3.  
  4.  
  5. // this is the PWMing state
  6. bool led_state[LEDS];
  7. uint16_t led_next_toggle_time[LEDS];
  8.  
  9. // setup...
  10. uint16_t start_time = TCNT1;
  11. for (int i = 0; i < LEDS; i++) {
  12. led_state[i] = false;
  13. led_next_toggle_time[i] = start_time;
  14. }
  15.  
  16. bool timeReached (uint16_t target_time)
  17. {
  18. return (uint16_t)(TCNT1 - target_time) < UINT16_C(0x8000);
  19. }
  20.  
  21. // drive the leds
  22. while (1) {
  23. for (int i = 0; i < LEDS; i++) {
  24. if (timeReached(led_next_toggle_time[i])) {
  25. if (led_state[i]) {
  26. Turn led i OFF;
  27. led_state[i] = false;
  28. led_next_toggle_time[i] += pwm_inteval - on_time[i];
  29. } else {
  30. Turn led i ON;
  31. led_state[i] = true;
  32. led_next_toggle_time[i] += on_time[i];
  33. }
  34. }
  35. }
  36. }
  37.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: ‘uint16_t’ does not name a type
 uint16_t pwm_interval = ...;
 ^
prog.cpp:2:1: error: ‘uint16_t’ does not name a type
 uint16_t on_time[LEDS]; // this is the input determining LED brigthenss, must be <= pwm_interval
 ^
prog.cpp:6:16: error: ‘LEDS’ was not declared in this scope
 bool led_state[LEDS];
                ^
prog.cpp:7:1: error: ‘uint16_t’ does not name a type
 uint16_t led_next_toggle_time[LEDS];
 ^
prog.cpp:10:1: error: ‘uint16_t’ does not name a type
 uint16_t start_time = TCNT1;
 ^
prog.cpp:11:1: error: expected unqualified-id before ‘for’
 for (int i = 0; i < LEDS; i++) {
 ^
prog.cpp:11:17: error: ‘i’ does not name a type
 for (int i = 0; i < LEDS; i++) {
                 ^
prog.cpp:11:27: error: ‘i’ does not name a type
 for (int i = 0; i < LEDS; i++) {
                           ^
prog.cpp:16:19: error: ‘uint16_t’ was not declared in this scope
 bool timeReached (uint16_t target_time)
                   ^
prog.cpp:17:1: error: expected ‘,’ or ‘;’ before ‘{’ token
 {
 ^
prog.cpp:22:1: error: expected unqualified-id before ‘while’
 while (1) {
 ^
stdout
Standard output is empty