Advertisement
mindthump

Rotate Array in C (LEDs)

Jun 25th, 2020 (edited)
1,350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const int strip_len = 42;
  4. char strip[strip_len];
  5. const char colors[] = {'R', 'W', 'B', '\0'};
  6. //const char colors[] = {'R', 'O', 'Y', 'G', 'B', 'I', 'V', '\0'};
  7. int num_colors = sizeof(colors) / sizeof(colors[0]) - 1;
  8.  
  9. void show_strip();
  10.  
  11. void init_strip();
  12.  
  13. void rotate();
  14.  
  15. int main() {
  16.     init_strip();
  17.     show_strip();
  18.     // Rotate once for each slot (LED)
  19.     for (int j = 0; j < strip_len - 1; j++) {
  20.         rotate();
  21.         show_strip();
  22.     }
  23.     printf("\n");
  24.     return 0;
  25. }
  26.  
  27. void rotate() {
  28.     char last;
  29.     last = strip[strip_len - 1];
  30.     for (int i = strip_len - 1; i >= 0; i--) {
  31.         strip[i] = strip[i - 1];
  32.     }
  33.     strip[0] = last;
  34. }
  35.  
  36. void show_strip() {
  37.     for (int n = 0; n < strip_len; n++) {
  38.         printf("%c", strip[n]);
  39.     }
  40.     printf("\n");
  41. }
  42.  
  43. void init_strip() {
  44.     int chunk_len = strip_len / num_colors;
  45.     for (int q = 0; q < num_colors; q++) {
  46.         for (int p = 0; p < chunk_len; p++) {
  47.             int led = (q * chunk_len) + p;
  48.             strip[led] = colors[q];
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement