daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Apr 14th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Control a servo using a timer on a ESP32 (no ledc stuff)
  2. // by misan April 2018
  3.  
  4. int servo=1500;
  5. #define SERVO T2 // pin D11 on UC406 ESP32 UNO
  6. hw_timer_t * timer1 = NULL; // servo
  7.  
  8. volatile boolean flip = false;
  9.  
  10. void IRAM_ATTR itr1(void) {
  11.   if(flip) {
  12.     digitalWrite(SERVO,LOW);
  13.     flip = false;
  14.     timerAlarmWrite(timer1, 20000, true);  // 20 ms between pulses
  15.   }
  16.   else {
  17.     timerAlarmWrite(timer1, servo, true); //  servo pulse in microseconds
  18.     flip = true;
  19.     digitalWrite(SERVO,HIGH);
  20.   }
  21. }
  22.  
  23. void setup() {
  24.   pinMode(SERVO,OUTPUT);
  25.   timer1 = timerBegin(1, 80, true);
  26.   timerAttachInterrupt(timer1, &itr1, true);
  27.   timerAlarmWrite(timer1, 20000, true);
  28.   timerAlarmEnable(timer1);
  29. }
  30.  
  31. int angle=0;
  32. void loop() {
  33.   servo=1400+sin((angle++)/57.7)*800;
  34.   delay(5);
  35. }
RAW Paste Data
Top