Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
New major release.
Now you can easily use code written for use with the Servo Library for Arduino.
  • Loading branch information
Dlloydev committed May 29, 2023
1 parent 3c9ba03 commit 0f0a69a
Show file tree
Hide file tree
Showing 24 changed files with 564 additions and 372 deletions.
428 changes: 272 additions & 156 deletions README.md

Large diffs are not rendered by default.

60 changes: 0 additions & 60 deletions Using pwmWrite.md

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
The delay of 50 ms was added to control the plotter sample period.
*/

#include <pwmWrite.h>
#include <Servo.h>

const int servoPin = 8;
volatile float ke = 0.0; // easing curve
Expand All @@ -44,10 +44,11 @@ volatile float pos = 90; // servo position (degrees)
volatile float ye; // calculated servo position (normalized)

hw_timer_t *Timer0 = NULL;
Pwm pwm = Pwm();

Servo myservo = Servo();

void IRAM_ATTR Timer0_ISR() {
ye = pwm.writeServo(servoPin, pos, speed, ke);
ye = myservo.write(servoPin, pos, speed, ke);
}

void setup() {
Expand All @@ -56,20 +57,20 @@ void setup() {
Timer0 = timerBegin(0, 80, true);
timerAttachInterrupt(Timer0, &Timer0_ISR, true);
timerAlarmWrite(Timer0, 20000, true); // 20 ms
pwm.writeServo(servoPin, pos, speed, ke); // attach servo
myservo.write(servoPin, pos, speed, ke); // attach servo
timerAlarmEnable(Timer0);

// slowly go to 0 degrees, linear easing
pos = 0.0;
speed = 30;
ke = 0.0;
ye = pwm.writeServo(servoPin, pos, speed, ke);
ye = myservo.write(servoPin, pos, speed, ke);
while (ye > 0);

// setup faster speed and new easing curve
speed = 100;
ke = 0.7;
ye = pwm.writeServo(servoPin, pos, speed, ke);
ye = myservo.write(servoPin, pos, speed, ke);
}

void loop() {
Expand Down
75 changes: 0 additions & 75 deletions examples/Servo_Easing_Time/Servo_Easing_Time.ino

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
by dlloydev, February 2023.
*/

#include <pwmWrite.h>
#include <Servo.h>

Pwm pwm = Pwm();
Servo myservo = Servo();

const int potPin1 = 0, servoPin1 = 6;
const int potPin2 = 1, servoPin2 = 7;
Expand All @@ -22,27 +22,27 @@ void loop() {

int val1 = analogRead(potPin1); // read the pot value (0-4095)
val1 = map(val1, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin1, val1); // set the servo position (degrees)
myservo.write(servoPin1, val1); // set the servo position (degrees)

int val2 = analogRead(potPin2); // read the pot value (0-4095)
val2 = map(val2, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin2, val2); // set the servo position (degrees)
myservo.write(servoPin2, val2); // set the servo position (degrees)

int val3 = analogRead(potPin3); // read the pot value (0-4095)
val3 = map(val3, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin3, val3); // set the servo position (degrees)
myservo.write(servoPin3, val3); // set the servo position (degrees)

int val4 = analogRead(potPin4); // read the pot value (0-4095)
val4 = map(val4, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin4, val4); // set the servo position (degrees)
myservo.write(servoPin4, val4); // set the servo position (degrees)

int val5 = analogRead(potPin5); // read the pot value (0-4095)
val5 = map(val5, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin5, val5); // set the servo position (degrees)
myservo.write(servoPin5, val5); // set the servo position (degrees)

int val6 = analogRead(potPin6); // read the pot value (0-4095)
val6 = map(val6, 700, 3395, 0, 180); // align pot pointer to servo arm
pwm.writeServo(servoPin6, val6); // set the servo position (degrees)
myservo.write(servoPin6, val6); // set the servo position (degrees)

delay(10);
}
24 changes: 14 additions & 10 deletions examples/Servo_Sweep_Inverted/Servo_Sweep_Inverted.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
This demonstrates using an inverted PWM output for servo control.
It allows using only one transistor to drive a higher voltage Servo control signal.
No additional latency is added like found with software inversion, because the inverted
pulse remains at the start of the refresh period rather than being flipped to the end.
No additional latency added as the signal is inverted by the timer peripheral.
Choose/edit an attach function parameter to customize the signal configuration.
*/

#include <pwmWrite.h>
#include <Servo.h>

Pwm pwm = Pwm();
Servo myservo = Servo();

const int servoPin = 5, ch = 0;

void setup() {
pwm.attach(servoPin, ch, 544, 2400, 0, 1, true);
Serial.begin(115200);
myservo.attach(servoPin, true); // attach to first free channel, inverted pwm
//myservo.attach(servoPin, 3, true); // attach to channel 3, inverted pwm
//myservo.attach(servoPin, 6, 500, 2500, true); // attach to ch 6, minUs=500 maxUs=2500, inverted pwm
myservo.printDebug();
}

void loop() {
for (int pos = 0; pos <= 180; pos++) { // go from 0-180 degrees
pwm.writeServo(servoPin, pos); // set the servo position (degrees)
for (int pos = 0; pos <= 180; pos++) {
myservo.write(servoPin, pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos--) { // go from 180-0 degrees
pwm.writeServo(servoPin, pos); // set the servo position (degrees)
for (int pos = 180; pos >= 0; pos--) {
myservo.write(servoPin, pos);
delay(15);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
by dlloydev, December 2022.
*/

#include <pwmWrite.h>
#include <Servo.h>

Pwm pwm = Pwm();
Servo myservo = Servo();

const int speedPin1 = 32, speedPin2 = 12;
const int servoPin1 = 14, servoPin2 = 13;
Expand All @@ -20,9 +20,9 @@ void loop() {
speed1 = (analogRead(speedPin1)) / 4095.0; // 0-100% speed
speed2 = (analogRead(speedPin2)) / 2047.0; // 0-200% speed

pwm.writeServo(servoPin1, pos1);
myservo.write(servoPin1, pos1);
delay(5); // for simulator
pwm.writeServo(servoPin2, pos2);
myservo.write(servoPin2, pos2);
delay(5); // for simulator

if (pos1 < 0) pos1 = 0;
Expand Down
File renamed without changes.
14 changes: 12 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@

Pwm KEYWORD1
pwm KEYWORD1
pinStatus_t KEYWORD1
Servo KEYWORD1
myservo KEYWORD1
mem_t KEYWORD1

#######################################
# Methods and Functions
#######################################

write KEYWORD2
writeServo KEYWORD2
writePwm KEYWORD2
writeMicroseconds KEYWORD2
read KEYWORD2
readMicroseconds KEYWORD2
tone KEYWORD2
note KEYWORD2
readMicroseconds KEYWORD2
attach KEYWORD2
attachInvert KEYWORD2
attachServo KEYWORD2
attachPwm KEYWORD2
attached KEYWORD2
attachedPin KEYWORD2
firstFreeCh KEYWORD2
detach KEYWORD2
detached KEYWORD2
pause KEYWORD2
resume KEYWORD2
setFrequency KEYWORD2
setResolution KEYWORD2
printConfig KEYWORD2
printDebug KEYWORD2

#######################################
# Constants
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"keywords": "pwm, servo, tone, esp32, analogWrite, esp32-s2, esp32-s3, esp32-c3, ledc",
"description": "ESP32 PWM, Servo, Easing and Tone. Smart GPIO pin management and advanced control features.",
"license": "MIT",
"version": "4.3.4",
"version": "5.0.0",
"frameworks": "arduino",
"platforms": "espressif32",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32 ESP32S2 AnalogWrite
version=4.3.4
version=5.0.0
author=David Lloyd
maintainer=David Lloyd <[email protected]>
sentence=ESP32 PWM, Servo, Easing and Tone.
Expand Down
Loading

0 comments on commit 0f0a69a

Please sign in to comment.