Guest User

Untitled

a guest
Apr 15th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. const int ledPin = 9;
  2.  
  3. int mode = 0;
  4.  
  5. int power = 0;
  6.  
  7. int input = 0;
  8.  
  9. int oldInput = 0;
  10.  
  11. unsigned long previousMillis = 0;
  12.  
  13. unsigned long currentMillis;
  14.  
  15. bool increasing = true;
  16.  
  17. void setup(){
  18.  
  19.   pinMode(ledPin, OUTPUT);
  20.   Serial.begin(9600);
  21.  
  22. }
  23.  
  24. void loop(){
  25.  
  26.   oldInput = input;
  27.  
  28.   input = Serial.read();
  29.  
  30.   if(oldInput != input){
  31.     if(input >= 50){
  32.       mode = 0;
  33.     }
  34.     else if(input == 1){
  35.       mode = 1;
  36.     }
  37.     else if(input == 2){
  38.       mode = 2;
  39.     }
  40.     else{
  41.       power = 0;
  42.     }
  43.  
  44.     //power = 0;
  45.   }
  46.  
  47.   currentMillis = millis();
  48.  
  49.   if(mode == 0){
  50.    
  51.     mode0();
  52.    
  53.   }
  54.   else if(mode == 1){
  55.  
  56.     mode1();
  57.    
  58.   }
  59.   else if(mode == 2){
  60.  
  61.     mode2();
  62.    
  63.   }
  64.  
  65.   analogWrite( 9, power);
  66.  
  67. }
  68.  
  69. void mode0(){ //power set to input from slider
  70.  
  71.   power = input;
  72.  
  73. }
  74.  
  75. void mode1(){ //slow increase then reset to zero cycle
  76.  
  77.   if(currentMillis - previousMillis >= 100){
  78.  
  79.       previousMillis = currentMillis;
  80.    
  81.       if(power <= 255){
  82.      
  83.         power += 5;
  84.    
  85.       }
  86.       else{
  87.      
  88.         power = 0;
  89.    
  90.       }
  91.     }
  92. }
  93.  
  94. void mode2(){ //slow increase then decrease cycle
  95.  
  96.   if(currentMillis - previousMillis >= 100){
  97.  
  98.       previousMillis = currentMillis;
  99.  
  100.       if(power + 5 >= 255){
  101.        
  102.         increasing = false;
  103.        
  104.       }
  105.       else if(power - 5 <= 45){
  106.        
  107.         increasing = true;
  108.        
  109.       }
  110.    
  111.       if(increasing && power + 5 < 255){
  112.      
  113.         power += 5;
  114.    
  115.       }
  116.       else if(power - 5 > 45){
  117.      
  118.         power -= 5;
  119.    
  120.       }
  121.     }
  122.  
  123. }
  124.  
  125. void mode3(){ //burst
  126.  
  127.   //if(currentMillis - previousMillis
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment