Advertisement
TolentinoCotesta

Ali Farfalla

Apr 3rd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <Servo.h>
  2. #define WAIT 600
  3.  
  4. Servo Servo1;  // create servo object to control a servo
  5. Servo Servo2;  // create servo object to control a servo
  6.  
  7. int StartButton = 2;            
  8. int Servo1Pos, oldServo1Pos;    // variable to read the value from the analog pin
  9. int Servo2Pos, oldServo2Pos;
  10. unsigned long StartTime, StopTime, WaitTime, pressTime;
  11.  
  12. const int DelayServo = 1000;
  13. const int minPos = 5;
  14. const int maxPos = 175;
  15.  
  16. const int offset1 = 7;
  17. const int offset2 = -7;
  18.  
  19.  
  20. bool flapping = false;
  21.  
  22. void setup() {
  23.   pinMode(StartButton, INPUT_PULLUP);
  24.   Servo1.attach(9);  // attaches the servo on pin 9 to the servo object
  25.   Servo2.attach(10);
  26.   Serial.begin(115200);
  27.   Servo1.write(minPos);
  28.   Servo2.write(minPos);
  29. }
  30.  
  31. void loop() {
  32.   /*
  33.   Servo1Pos = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)  
  34.   Servo1Pos = map(Servo1Posl, 0, 1020, 5, 110);     // scale it to use it with the servo (value between 0 and 180)
  35.   Servo1Pos = constrain(Servo1Pos, 0, 180);
  36.   myservo.write(Servo1Pos);                  // sets the servo position according to the scaled value
  37.   */
  38.   // Gestione pulsante
  39.   pressTime = millis();
  40.   checkButton();
  41.  
  42.   if( (flapping == true) && (millis() - WaitTime > WAIT) ){
  43.     WaitTime = millis();
  44.     SingleClick();
  45.   }
  46.  
  47.   // Questa è la funzione che esegue il movimento vero e proprio
  48.   runServo();
  49.  
  50. }
  51.  
  52.  
  53.  
  54. void runServo(){  
  55.   // Se il tempo trascorso è maggiore di DelayServo (1000 millisecondi) spengo il servomotore per risparmiare batteria
  56.   if(millis() - StartTime > DelayServo){
  57.     Servo1.detach();
  58.     Servo2.detach();
  59.     Serial.println("OFF");
  60.   }
  61.   else {
  62.     // Altrimenti li "riaccendo" e sposto il motore
  63.     if(!Servo1.attached()){
  64.        Servo1.attach(9);
  65.        Servo2.attach(10);      
  66.        Serial.println("ON");
  67.     }
  68.     Servo1.write(Servo1Pos + offset1);
  69.     Servo2.write(Servo2Pos + offset2);
  70.   }  
  71.   delay(5);  
  72. }
  73.  
  74.  
  75. #define SHORT   50
  76. #define MEDIUM  500
  77. #define LONG    500
  78.  
  79. // Controlliamo se il pulsante è stato premuto brevemente o più a lungo
  80. void checkButton() {
  81.   static int pressType;
  82.   static unsigned long pTime;
  83.   if (digitalRead(StartButton) == LOW) {
  84.     delay(50);
  85.     pressType = 0;
  86.     while (digitalRead(StartButton) == LOW) {
  87.       pTime = millis() - pressTime;
  88.       if (pTime > SHORT && pTime <= MEDIUM)
  89.         pressType = 1;
  90.       else if (pTime > MEDIUM && pTime <= LONG)
  91.         pressType = 2;
  92.       else if (pTime > LONG) {
  93.         pressType = 3;
  94.         break;
  95.       }
  96.     }
  97.     switch (pressType) {
  98.       case 0: break;
  99.       case 1: SingleClick();  break;
  100.       case 2: MediumClick();  break;
  101.       case 3: LongClick(); delay(500); break;
  102.     }
  103.   }
  104. }
  105.  
  106. void SingleClick(){  
  107.   // Memorizzo il momento di partenza dei servo
  108.   StartTime = millis();
  109.   if(Servo1Pos == maxPos){
  110.     Servo1Pos = minPos;
  111.     Servo2Pos = minPos;      
  112.   }
  113.   else{
  114.     Servo1Pos = maxPos;
  115.     Servo2Pos = maxPos;
  116.   }      
  117. }
  118.  
  119. void MediumClick(){
  120.   return;
  121. }
  122.  
  123. void LongClick(){
  124.   flapping = !flapping;
  125.   delay(1000);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement