learnelectronics

Charlieplexing with Arduino

Aug 26th, 2019
1,845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. //--------------------------------------------
  2. //
  3. //             Charlieplexing Demo
  4. //
  5. //              Learnelectronics
  6. //               Aug, 23, 2019
  7. //
  8. //      www.youtube.com/c/learnelectronics
  9. //        email: [email protected]
  10. //
  11. //---------------------------------------------
  12.  
  13.  
  14.  
  15. #define A 8                     // We call pin #8 A
  16. #define B 9                     // "   "   "   #9 B
  17. #define C 10                    // "   "   "  #10 C
  18. #define D 11                    // "   "   "  #11 D
  19.  
  20. #define PIN_CONFIG 0            // Setup our Boolean config for the 4 pins
  21. #define PIN_STATE 1             // Setup our Boolean state for the 4 pins
  22.  
  23. #define LED_Num 12              // How many LEDs are we using
  24.  
  25.  
  26. // Now we have to create a matrix for the pins so we know which of the 3 states they are in
  27.  
  28. int matrix[LED_Num][2][4] = {
  29.   //           PIN_CONFIG                  PIN_STATE
  30.   //    A       B       C      D         A     B    C    D
  31.   { { OUTPUT, OUTPUT, INPUT, INPUT }, { HIGH, LOW, LOW, LOW } },
  32.   { { OUTPUT, OUTPUT, INPUT, INPUT }, { LOW, HIGH, LOW, LOW } },
  33.   { { INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, HIGH, LOW, LOW } },
  34.   { { INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, LOW, HIGH, LOW } },
  35.   { { OUTPUT, INPUT, OUTPUT, INPUT }, { HIGH, LOW, LOW, LOW } },
  36.   { { OUTPUT, INPUT, OUTPUT, INPUT }, { LOW, LOW, HIGH, LOW } },
  37.   { { OUTPUT, INPUT, INPUT, OUTPUT }, { HIGH, LOW, LOW, LOW } },
  38.   { { OUTPUT, INPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } },
  39.   { { INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, HIGH, LOW, LOW } },
  40.   { { INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } },
  41.   { { INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, HIGH, LOW } },
  42.   { { INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } }  
  43. };
  44.  
  45.  
  46. //Here we turn the LED on by changing the pin state and mode
  47. void lightOn( int led ) {
  48.   pinMode( A, matrix[led][PIN_CONFIG][0] );
  49.   pinMode( B, matrix[led][PIN_CONFIG][1] );
  50.   pinMode( C, matrix[led][PIN_CONFIG][2] );
  51.   pinMode( D, matrix[led][PIN_CONFIG][3] );
  52.   digitalWrite( A, matrix[led][PIN_STATE][0] );
  53.   digitalWrite( B, matrix[led][PIN_STATE][1] );
  54.   digitalWrite( C, matrix[led][PIN_STATE][2] );
  55.   digitalWrite( D, matrix[led][PIN_STATE][3] );
  56. }
  57.  
  58. void setup() {}
  59.  
  60.  
  61. //here we just incrementally change the number of the LED to beturned on.
  62. void loop() {
  63.   for( int l = 0; l < LED_Num; l++ ) {
  64.     lightOn( l );
  65.     delay( 5000 / LED_Num );
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment