ESP32Servo
analogWriteExample.ino
Go to the documentation of this file.
1 /*
2  Mega analogWrite() test
3 
4  This sketch fades LEDs up and down one at a time on digital pins 2 through 13.
5  This sketch was written for the Arduino Mega, and will not work on previous boards.
6 
7  The circuit:
8  * LEDs attached from pins 2 through 13 to ground.
9 
10  created 8 Feb 2009
11  by Tom Igoe
12 
13  This example code is in the public domain.
14 
15  */
16 // These constants won't change. They're used to give names
17 // to the pins used:
18 const int lowestPin = 2;
19 const int highestPin = 33;
20 #include <ESP32Servo.h>
22 
23 void setup() {
24  Serial.begin(115200);
25  // Allow allocation of all timers
30 }
31 
32 void loop() {
33  if (!myservo.attached()) {
34  myservo.setPeriodHertz(50); // standard 50 hz servo
35  myservo.attach(33, 1000, 2000); // Attach the servo after it has been detatched
36  }
37  myservo.write(0);
38  // iterate over the pins:
39  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
40  if (ESP32PWM::hasPwm(thisPin) && // Is it possible for this pin to PWM
41  (ESP32PWM::channelsRemaining() > 0 || // New channels availible to allocate
42  pwmFactory(thisPin) != NULL || // already allocated this pin in the factory
43  thisPin == 25 || // one of the 2 DAC outputs, no timer needed
44  thisPin == 26)) { // one of the 2 DAC outputs, no timer needed
45  if (pwmFactory(thisPin) == NULL) { // check if its the first time for the pin or its a DAC
46  if (thisPin == 25 || // one of the 2 DAC outputs, no timer needed
47  thisPin == 26) {
48  Serial.println("DAC to pin " + String(thisPin));
49  } else
50  Serial.println("Writing to pin " + String(thisPin));
51  pinMode(thisPin, OUTPUT);
52  }
53  // fade the LED on thisPin from off to brightest:
54  for (int brightness = 0; brightness < 255; brightness++) {
55  analogWrite(thisPin, brightness);
56  delay(1);
57  myservo.write(brightness);
58  }
59  // fade the LED on thisPin from brithstest to off:
60  for (int brightness = 255; brightness >= 0; brightness--) {
61  analogWrite(thisPin, brightness);
62  myservo.write(brightness);
63  delay(1);
64  }
65 
66  }
67  }
68  myservo.detach(); // Turn the servo off for a while
69  delay(2000);
70 
71 }
static void allocateTimer(int timerNumber)
Definition: ESP32PWM.cpp:25
static int channelsRemaining()
Definition: ESP32PWM.h:110
void write(int value)
Definition: ESP32Servo.cpp:131
ESP32PWM * pwmFactory(int pin)
Definition: ESP32PWM.cpp:305
void analogWrite(uint8_t APin, uint16_t AValue)
Definition: analogWrite.cpp:11
void setPeriodHertz(int hertz)
Definition: ESP32Servo.h:138
bool attached()
Definition: ESP32Servo.cpp:183
void loop()
void setup()
void detach()
Definition: ESP32Servo.cpp:120
static bool hasPwm(int pin)
Definition: ESP32PWM.h:99
int attach(int pin)
Definition: ESP32Servo.cpp:73
const int highestPin
const int lowestPin
Servo myservo