Created
October 23, 2024 01:00
-
-
Save IgorDePaula/157d0cc9974645f697d151d17d2d2681 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // #include <ESP32Servo.h> | |
| // Servo myservo; // create servo object to control a servo | |
| // // 16 servo objects can be created on the ESP32 | |
| // int pos = 0; // variable to store the servo position | |
| // // Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 | |
| // // Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42 | |
| // // Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED) | |
| // // Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21 | |
| // #if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) | |
| // int servoPin = 19; | |
| // #elif defined(CONFIG_IDF_TARGET_ESP32C3) | |
| // int servoPin = 19; | |
| // #else | |
| // int servoPin = 19; | |
| // #endif | |
| // void setup() { | |
| // // Allow allocation of all timers | |
| // ESP32PWM::allocateTimer(0); | |
| // ESP32PWM::allocateTimer(1); | |
| // ESP32PWM::allocateTimer(2); | |
| // ESP32PWM::allocateTimer(3); | |
| // myservo.setPeriodHertz(50); // standard 50 hz servo | |
| // myservo.attach(servoPin, 1000, 2000); // attaches the servo on pin 18 to the servo object | |
| // // using default min/max of 1000us and 2000us | |
| // // different servos may require different min/max settings | |
| // // for an accurate 0 to 180 sweep | |
| // } | |
| // void loop() { | |
| // for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees | |
| // // in steps of 1 degree | |
| // myservo.write(pos); // tell servo to go to position in variable 'pos' | |
| // delay(15); // waits 15ms for the servo to reach the position | |
| // } | |
| // for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees | |
| // myservo.write(pos); // tell servo to go to position in variable 'pos' | |
| // delay(15); // waits 15ms for the servo to reach the position | |
| // } | |
| // } | |
| /********* | |
| Rui Santos & Sara Santos - Random Nerd Tutorials | |
| Complete project details at https://RandomNerdTutorials.com/esp32-servo-motor-web-server-arduino-ide/ | |
| Based on the ESP32Servo Sweep Example | |
| *********/ | |
| #include <ESP32Servo.h> | |
| #define VALOR1 4 | |
| #define VALOR2 14 | |
| static const int servoPin = 26; | |
| static const int servoPin2 = 21; | |
| int valor1, valor2; | |
| Servo servo1; | |
| Servo servo2; | |
| int posDegrees = 0; | |
| int lastTouchState1, lastTouchState2; // the previous state of touch sensor | |
| int currentTouchState1, currentTouchState2; | |
| void setup() { | |
| Serial.begin(115200); | |
| ESP32PWM::allocateTimer(0); | |
| ESP32PWM::allocateTimer(1); | |
| ESP32PWM::allocateTimer(2); | |
| ESP32PWM::allocateTimer(3); | |
| pinMode(VALOR1, INPUT); //touchRead() | |
| pinMode(VALOR2, INPUT); | |
| // pinMode(19, OUTPUT); | |
| // pinMode(21, OUTPUT); | |
| //servo1.write(posDegrees); | |
| servo1.setPeriodHertz(50); | |
| // servo2.write(posDegrees); | |
| servo2.setPeriodHertz(50); | |
| // digitalWrite(19, LOW); | |
| // digitalWrite(21, LOW); | |
| } | |
| void loop() { | |
| //servo1.write(180); | |
| // for (posDegrees = 0; posDegrees < 180; posDegrees++) { | |
| // servo1.write(posDegrees); | |
| // } | |
| //delay(1000); | |
| // for (posDegrees = 180; posDegrees > 0; posDegrees--) { | |
| // servo1.write(posDegrees); | |
| // } | |
| //servo1.write(0); | |
| //delay(1000); | |
| // lastTouchState1 = currentTouchState1; // save the last state | |
| // currentTouchState1 = digitalRead(VALOR1); // read new state | |
| // lastTouchState2 = currentTouchState2; // save the last state | |
| // currentTouchState2 = digitalRead(VALOR2); // read new state | |
| // if (lastTouchState1 == LOW && currentTouchState1 == HIGH) { | |
| // Serial.println("The sensor is touched"); | |
| // servo1.write(posDegrees); | |
| // Serial.println("estou aqui 1"); | |
| // posDegrees++; | |
| // } | |
| // if (lastTouchState2 == LOW && currentTouchState2 == HIGH) { | |
| // Serial.println("The sensor is touched"); | |
| // servo1.write(posDegrees); | |
| // Serial.println("estou aqui 2"); | |
| // posDegrees--; | |
| // } | |
| // if (valor1 < 50) { | |
| // servo1.write(posDegrees); | |
| // posDegrees++; | |
| // } | |
| // Serial.println(valor2); | |
| // if (valor2 < 50) { | |
| // servo1.write(posDegrees); | |
| // posDegrees--; | |
| // } | |
| valor1 = touchRead(15); | |
| valor2 = touchRead(13); | |
| if (valor1 < 50) { | |
| servo1.attach(servoPin, 1000, 2000); | |
| servo2.attach(servoPin2, 1000, 2000); | |
| servo2.write(180); | |
| servo1.write(180); | |
| posDegrees++; | |
| delay(200); | |
| //digitalWrite(19, HIGH); | |
| //digitalWrite(21, LOW); | |
| } | |
| //Serial.println(valor2); | |
| else if (valor2 < 50) { | |
| servo1.attach(servoPin, 1000, 2000); | |
| servo1.write(0); | |
| servo2.attach(servoPin2, 1000, 2000); | |
| servo2.write(0); | |
| posDegrees--; | |
| delay(200); | |
| // digitalWrite(19, LOW); | |
| // digitalWrite(21, HIGH); | |
| } else { | |
| servo1.detach(); | |
| servo2.detach(); | |
| } | |
| servo2.detach(); | |
| servo1.detach(); | |
| //servo1.attach(servoPin, 1000,2000); | |
| // while (posDegrees < 180*10) { | |
| // servo1.write(180); | |
| // //servo1.writeMicroseconds(int value) | |
| // posDegrees++; | |
| // Serial.println(posDegrees); | |
| // delay(20); | |
| // } | |
| // servo1.detach(); | |
| // posDegrees = 0; | |
| // delay(2000); | |
| // servo1.attach(servoPin, 1000,2000); | |
| // while (posDegrees > 0) { | |
| // servo1.write(0); | |
| // posDegrees--; | |
| // Serial.println(posDegrees); | |
| // delay(20); | |
| // } | |
| // servo1.detach(); | |
| // delay(2000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment