SuperBrainAK

controlling and graphing a variable.

Dec 6th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. *disclaimer this code is a snippet and is not in any function and is missing some of the basic things.*
  2. int Map = 10;                                               //map variable = varmax/5.
  3. float ledvl = 0;                                            //decimal for the leds var/Map.
  4. int led = 0;                                                //the pwm state of the pin.
  5. int ledp = 0;                                               //the percent of pwm to the led.
  6.  
  7.     *this code following is in my every second things.
  8.     if (Button == 6){                                         //toggles controll for v1.
  9.       v1ctrl = !v1ctrl;
  10.       Serial.print("control v1: ");
  11.       (v1ctrl) ? Serial.println("yes") : Serial.println("no");
  12.       if (v1ctrl){
  13.         Map = 14;                                             //creats the map for v1.
  14.       }
  15.       else {
  16.         Map = 10;                                             //resets the map #.
  17.       }
  18.  
  19.  *this code runs whenever it needs
  20.  if (lncdrvl != ncdrvl){
  21.    Serial.print("encoder value: ");      //tells me what the encoder value is.
  22.    Serial.println(ncdrvl);
  23.    if (v1ctrl == 1){
  24.      if (ncdrvl > lncdrvl){              //ups or downs the variable with the encoder
  25.        v1 ++;
  26.      }
  27.      if (ncdrvl < lncdrvl){
  28.        v1 --;
  29.      }
  30.      ledvl = v1/Map;                     //defines where the variable is at in the graph
  31.      Serial.print("variable 1: ");       //tells me what the variable is at.
  32.      Serial.println(v1);
  33.    }
  34.    if (ledvl == 0){                      //its zero so no lights on.
  35.      digitalWrite(led1, LOW);
  36.      digitalWrite(led2, LOW);
  37.      digitalWrite(led3, LOW);
  38.      digitalWrite(led4, LOW);
  39.      digitalWrite(led5, LOW);
  40.    }
  41.    if (1 > ledvl && ledvl > 0){          //its starting to go but not a full 1 so pwm the 1st led.
  42.      led = ledvl * 255;
  43.      analogWrite(led1, led);
  44.    }
  45.    if (ledvl >= 1){                      //its at 1/5.
  46.      digitalWrite(led1, HIGH);
  47.      digitalWrite(led2, LOW);
  48.    }
  49.    if (2 > ledvl && ledvl > 1){          //its more than 1 but not at 2 so pwm the 2nd led.
  50.      ledp = ledvl - 1;
  51.      led = ledp * 255;
  52.      analogWrite(led2, led);
  53.    }
  54.    if (ledvl >= 2){                      //its at 2/5.
  55.      digitalWrite(led2, HIGH);
  56.      digitalWrite(led3, LOW);
  57.    }
  58.    if (3 > ledvl && ledvl > 2){          //its more than 2 but not at 3 so pwm the 3rd led.
  59.      ledp = ledvl - 2;
  60.      led = ledp * 255;
  61.      analogWrite(led3, led);
  62.    }
  63.    if (ledvl >= 3){                      //its at 3/5.
  64.      digitalWrite(led3, HIGH);
  65.      digitalWrite(led4, LOW);
  66.    }
  67.    if (4 > ledvl && ledvl > 3){          //its more than 3 but not at 4 so pwm the 4th led.
  68.      ledp = ledvl - 3;
  69.      led = ledp * 255;
  70.      analogWrite(led4, led);
  71.    }
  72.    if (ledvl >= 4){                      //its at 4/5.
  73.      digitalWrite(led4, HIGH);
  74.      digitalWrite(led5, LOW);
  75.    }
  76.    if (5 > ledvl && ledvl > 4){          //its more than 4 but not at 5 so pwm the 5th led.
  77.      ledp = ledvl - 4;
  78.      led = ledp * 255;
  79.      analogWrite(led5, led);
  80.    }
  81.    if (ledvl == 5){                      //its at 5/5.
  82.      digitalWrite(led5, HIGH);
  83.    }
  84.    Serial.print("ledvalue is: ");        //tells me what the led value is because its giving me whole #'s :P
  85.    Serial.println(ledvl);
  86.    lncdrvl = ncdrvl;
  87.  }
Advertisement
Add Comment
Please, Sign In to add comment