Advertisement
Guest User

Untitled

a guest
Feb 14th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. const int pinfov = 10;
  2. const int pinrev = 11;
  3. const int buttadd = 12;
  4. const int buttdec = 13;
  5. int runtime = 1000;
  6. int runperc = 30;
  7. int x;
  8. static unsigned long prev_time, time, lastbutt, period = 0;
  9. bool dir = true;
  10. bool run = false;
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.  
  16.   pinMode(pinfov, OUTPUT);
  17.   pinMode(pinrev, OUTPUT);
  18.   pinMode(buttadd, INPUT);
  19.   pinMode(buttdec, INPUT);
  20.  
  21.   time = micros()/1000;
  22.   prev_time = time;
  23.  
  24.   digitalWrite(pinrev, LOW);
  25.   digitalWrite(pinfov, HIGH);
  26.  
  27.   x = runtime/200*runperc;
  28.  
  29.   while (period < x) {
  30.     time = micros();
  31.     period = time - prev_time;
  32.   }
  33.  
  34.   digitalWrite(pinfov, LOW);
  35.   digitalWrite(pinrev, LOW);
  36.  
  37.   time = micros()/1000;
  38.   prev_time = time;
  39. }
  40.  
  41. void loop()
  42. {
  43.   time = micros()/1000;
  44.   period = abs(time - prev_time);
  45.  
  46.   if (abs(time-lastbutt)>100) {
  47.     lastbutt=time;
  48.     if (digitalRead(buttadd)==HIGH and runperc<100) {
  49.       runperc += 10;
  50.       Serial.println("+ PRESSED");
  51.     }
  52.     if (digitalRead(buttdec)==HIGH and runperc>10) {
  53.       runperc -= 10;
  54.       Serial.println("- PRESSED");
  55.     }
  56.   }
  57.  
  58.   if (period>runtime){
  59.     prev_time = time;
  60.     dir = !dir;
  61.     run = false;
  62.   }
  63.   if (period % 10 < 1) {
  64.    x = runtime/100*runperc;
  65.   if (period < x) {
  66.     if (dir and !run) {
  67.       digitalWrite(pinfov, LOW);
  68.       digitalWrite(pinrev, HIGH);
  69.       Serial.print("REV: ");
  70.       Serial.println(x);
  71.       run = true;
  72.     } else if (!run) {
  73.       digitalWrite(pinrev, LOW);
  74.       digitalWrite(pinfov, HIGH);
  75.       Serial.print("FOV: ");
  76.       Serial.println(x);
  77.       run = true;
  78.     }
  79.   } else if (run) {
  80.     digitalWrite(pinfov, LOW);
  81.     digitalWrite(pinrev, LOW);
  82.     Serial.print("STOP: ");
  83.     Serial.println(runtime - x);
  84.     run = false;
  85.   } }
  86. delay (10);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement