2222
2323void Servo::attach (int pin, int channel, int freq, int resolution) {
2424 _channel = channel;
25+ _pin = pin;
2526 if (channel > 15 ) channel = 15 ;
26- ledcSetup (_channel, freq, resolution);
27- ledcAttachPin (pin, channel);
28- ledcWrite (_channel, FIRSTDUTY);
27+ ledcAttachChannel (pin, freq, resolution, channel);
28+ ledcWriteChannel (_channel, FIRSTDUTY);
2929}
3030
3131/* *
@@ -34,12 +34,18 @@ void Servo::attach(int pin, int channel, int freq, int resolution) {
3434 * @retval None
3535**/
3636
37+ void ESC::attach (int pin, int channel, int freq, int resolution) {
38+ _channel = channel;
39+ if (channel > 15 ) channel = 15 ;
40+ ledcAttachChannel (pin, freq, resolution, channel);
41+ ledcWriteChannel (_channel, FIRSTDUTY);
42+ }
43+
3744void Servo::write (int value) {
3845 if (value < SERVOMIN) value = SERVOMIN;
3946 if (value > SERVOMAX) value = SERVOMAX;
4047 int servoValue = (value - SERVOMIN) * (DUTYCYLEMAX - DUTYCYLEMIN) / (SERVOMAX - SERVOMIN) + DUTYCYLEMIN; // mapping to SERVOMIN-SERVOMAX values from DUTYCYLEMIN-DUTYCYLEMAX values
41- ledcWrite (_channel, servoValue); // _channel select servoValue(duty) to be set for selected channel
42- // delay(DELAYMS);
48+ ledcWriteChannel (_channel, servoValue); // _channel select servoValue(duty) to be set for selected channel
4349}
4450
4551void Servo::writeMicroseconds (int value) {
@@ -49,31 +55,26 @@ void Servo::writeMicroseconds(int value) {
4955 this ->write (value);
5056}
5157
52- void ESC::attach (int pin, int channel, int freq, int resolution) {
53- _channel = channel;
54- if (channel > 15 ) channel = 15 ;
55- ledcSetup (_channel, freq, resolution);
56- ledcAttachPin (pin, channel);
57- ledcWrite (_channel, FIRSTDUTY);
58+ void ESC::write (int value) {
59+ if (value < ESCMIN) value = ESCMIN;
60+ if (value > ESCMAX) value = ESCMAX;
61+ int escValue = (value - ESCMIN) * (ESCDUTYCYLEMAX - ESCDUTYCYLEMIN) / (ESCMAX - ESCMIN) + ESCDUTYCYLEMIN; // mapping to SERVOMIN-SERVOMAX values from DUTYCYLEMIN-DUTYCYLEMAX values
62+ ledcWriteChannel (_channel, escValue); // _channel select servoValue(duty) to be set for selected channel
5863}
5964
6065int Servo::read () {
61- int dutyCycle = ledcRead (_channel);
62- int newDutyCycle = map (dutyCycle, DUTYCYLEMIN, DUTYCYLEMAX, SERVOMIN, SERVOMAX) + 1 ;
63- if (newDutyCycle % 45 == 0 ) newDutyCycle--;
64- return map (newDutyCycle, SERVOMIN, SERVOMAX, 0 , 180 );
66+ int dutyCycle = ledcRead (_pin);
67+ int newDutyCycle = map (dutyCycle, DUTYCYLEMIN, DUTYCYLEMAX, SERVOMIN, SERVOMAX);
68+ if (newDutyCycle % 45 == 0 ){
69+ return newDutyCycle;
70+ } else {
71+ return newDutyCycle + 1 ;
72+ }
6573}
6674
67- int Servo::readMicroseconds () {
75+ int Servo::readMicroseconds () {
6876 this ->read ();
6977}
70- void ESC::write (int value) {
71- if (value < ESCMIN) value = ESCMIN;
72- if (value > ESCMAX) value = ESCMAX;
73- int escValue = (value - ESCMIN) * (ESCDUTYCYLEMAX - ESCDUTYCYLEMIN) / (ESCMAX - ESCMIN) + ESCDUTYCYLEMIN; // mapping to SERVOMIN-SERVOMAX values from DUTYCYLEMIN-DUTYCYLEMAX values
74- ledcWrite (_channel, escValue); // _channel select servoValue(duty) to be set for selected channel
75- // delay(DELAYMS);
76- }
7778
7879/* void Servo360::attach(int pin, int channel, int freq, int resolution) {
7980 _360channel = channel;
@@ -107,4 +108,4 @@ int Servo360::read() {
107108
108109int Servo360::readMicroseconds() {
109110 this->read();
110- }*/
111+ }*/
0 commit comments