Advertisement
TolentinoCotesta

vmeter

Feb 2nd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #define DOT      1     // DOT = 1 Si accende solo un led alla volta
  2. #define NUM_LEDS 9
  3. const byte led[NUM_LEDS] = { 4, 5, 6, 7, 8, 9, 10, 11, 12};
  4.  
  5. void setup() {
  6.   for(int i=0; i<NUM_LEDS; i++) {
  7.     pinMode((const byte) led[i], OUTPUT);  
  8.   }
  9.   Serial.begin(9600);
  10. }
  11.  
  12. void loop() {  
  13.   vMeter();
  14. delay(100);
  15. }
  16.  
  17.  
  18. void vMeter(){
  19.   volatile uint16_t leds;
  20.   static uint8_t currentStep = 0;
  21.  
  22.   uint8_t newStep = map(analogRead(A0), 0, 1023, 0, NUM_LEDS);
  23.   newStep = constrain(newStep, 0, NUM_LEDS);  
  24.   if(newStep != currentStep){
  25.     if(currentStep - newStep > 0)
  26.       currentStep -= 1;
  27.     else
  28.       currentStep += 1;  
  29.   }
  30.  
  31.   leds = 0;
  32.   if(DOT == 0){
  33.     for (int n=1; n<=NUM_LEDS ; n++) {
  34.       if (currentStep >= n)
  35.         bitClear(leds, n-1);
  36.     }
  37.   }
  38.   else {
  39.     for(int n=1; n<=NUM_LEDS ; n++) {
  40.     if(currentStep >= n){
  41.       bitClear(leds, n-1);
  42.       if(n>1)
  43.         bitSet(leds, n-2);
  44.       }
  45.     }  
  46.   }
  47.  
  48.   for(int i=0; i<NUM_LEDS; i++) {
  49.     boolean isBitSet = leds & (1 << i);
  50.     digitalWrite(led[i], isBitSet);
  51.   }
  52.   Serial.println(leds, BIN);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement