Advertisement
elektronek

Jan Vaverka - servo

Mar 21st, 2023 (edited)
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.42 KB | Writing | 0 0
  1. #include <Servo.h>
  2. Servo servo1;
  3. Servo servo2;
  4. Servo servo3;
  5. Servo servo4;
  6.  
  7. unsigned long prevMillis=0;
  8. uint16_t time=10000;  // 10 sekund v milisekundach
  9. uint8_t flag=0;
  10.  
  11. ISR(PCINT0_vect)  // PB
  12. {
  13.   flag=1;
  14. }
  15.  
  16. ISR(PCINT2_vect)  // PD
  17. {
  18.   flag=1;
  19. }
  20.  
  21. void setup() {
  22.   pinMode(4, OUTPUT); // Vystup pro spinani 10 sekund
  23.   pinMode(5, INPUT_PULLUP); // PCINT21
  24.   pinMode(6, INPUT_PULLUP); // PCINT22
  25.   pinMode(7, INPUT_PULLUP); // PCINT23
  26.   pinMode(8, INPUT_PULLUP); // PCINT0
  27.   servo1.attach(9);
  28.   servo2.attach(10);
  29.   servo3.attach(11);
  30.   servo4.attach(12);
  31.  
  32.   PCICR |= (1<<PCIE0)|(1<<PCIE2);
  33.   PCMSK0 |= (1<<PCINT0);
  34.   PCMSK2 |= (1<<PCINT23)|(1<<PCINT22)|(1<<PCINT21);
  35.   sei();
  36. }
  37.  
  38. void loop() {
  39.   // time update
  40.   if (flag)
  41.   {
  42.     flag=0;
  43.     prevMillis=millis();
  44.   }
  45.  
  46.   if (prevMillis+time> millis())
  47.   {
  48.     digitalWrite(4, HIGH);
  49.   }
  50.   else
  51.   {
  52.     digitalWrite(4, LOW);
  53.   }
  54.  
  55.   if (!digitalRead(5))
  56.   {
  57.     servo1.write(150);
  58.     delay(1);
  59.   }
  60.   else
  61.   {
  62.     servo1.write(0);
  63.     delay(1);
  64.   }
  65.  
  66.   if (!digitalRead(6))
  67.   {
  68.     servo2.write(150);
  69.     delay(1);
  70.   }
  71.   else
  72.   {
  73.     servo2.write(0);
  74.     delay(1);
  75.   }
  76.  
  77.   if (!digitalRead(7))
  78.   {
  79.     servo3.write(150);
  80.     delay(1);
  81.   }
  82.   else
  83.   {
  84.     servo3.write(0);
  85.     delay(1);
  86.   }
  87.  
  88.   if (!digitalRead(8))
  89.   {
  90.     servo4.write(150);
  91.     delay(1);
  92.   }
  93.   else
  94.   {
  95.     servo4.write(0);
  96.     delay(1);
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement