Added NAN as allowed value for ServoPin::set()

Sets duty-cycle to 0, which allows an analog servo to freely rotate.  Use Servo::set() with an actual number in degrees to restore position.
This commit is contained in:
Gregg 2024-06-30 17:15:00 -05:00
parent 212370f57c
commit e23247c39f
2 changed files with 35 additions and 44 deletions

View File

@ -251,16 +251,21 @@ void ServoPin::set(double degrees){
if(!channel) if(!channel)
return; return;
double usec=(degrees-minDegrees)*microsPerDegree+minMicros; if(!isnan(degrees)){
double usec=(degrees-minDegrees)*microsPerDegree+minMicros;
if(usec<minMicros) if(usec<minMicros)
usec=minMicros; usec=minMicros;
else if(usec>maxMicros) else if(usec>maxMicros)
usec=maxMicros; usec=maxMicros;
usec*=timer->freq_hz/1e6*(pow(2,(int)timer->duty_resolution)-1); usec*=timer->freq_hz/1e6*(pow(2,(int)timer->duty_resolution)-1);
channel->duty=usec;
} else {
channel->duty=0;
}
channel->duty=usec;
ledc_channel_config(channel); ledc_channel_config(channel);
} }

View File

@ -25,54 +25,40 @@
* *
********************************************************************************/ ********************************************************************************/
#include "Pixel.h" #include "PwmPin.h"
#define MAX_BRIGHTNESS 255 // maximum brightness when flashing RGB [0-255] ServoPin servo(21,0,500,2200,-60,60);
#define PIXEL_PIN 26 // set this to whatever pin you are using - note pin cannot be "input only"
#define NPIXELS 8 // set to number of pixels in strand
Pixel testPixel(PIXEL_PIN, PixelType::RGBW);
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
delay(1000); delay(1000);
Serial.printf("\n\nPixel Test on pin %d with %d pixels\n\n",PIXEL_PIN,NPIXELS); Serial.print("\n\nReady\n\n");
}
////////////////////////////////////// for(int count=0;count<3;count++){
for(int i=-60;i<61;i++){
servo.set(i);
delay(10);
}
void flashColor(boolean r, boolean g, boolean b, boolean w){ for(int i=60;i>-61;i--){
servo.set(i);
for(int i=0;i<MAX_BRIGHTNESS;i++){ delay(10);
testPixel.set(Pixel::RGB(i*r,i*g,i*b,i*w),NPIXELS); }
delay(4);
} }
for(int i=MAX_BRIGHTNESS;i>=0;i--){ delay(5000);
testPixel.set(Pixel::RGB(i*r,i*g,i*b,i*w),NPIXELS);
delay(4); servo.set(NAN);
}
delay(10000);
servo.set(0);
} }
////////////////////////////////////// //////////////////////////////////////
void loop(){ void loop(){
Serial.printf("Red...");
flashColor(1,0,0,0);
Serial.printf("Green...");
flashColor(0,1,0,0);
Serial.printf("Blue...");
flashColor(0,0,1,0);
Serial.printf("White...");
flashColor(0,0,0,1);
Serial.printf("Pausing.\n");
delay(1000);
} }