Hello everyone, i am using a simple sketch for a PWM signal, However every time i need to change the frequency i have to upload a new sketch, is there a way to set the frequency using a 2nd pot ?
#include <PWM.h>
int32_t frequency = 25000;
const int Pin = 9;
int PWM = 0;
void setup() {
InitTimersSafe();
bool succes = SetPinFrequencySafe(9, frequency);
}
void loop() {
PWM = map(analogRead(A0), 0, 1023, 0, 255);
analogWrite (Pin, PWM);
delay(100);
}
What happens if, in loop, you read your second pot, convert (map) the value to your changed frequency and call SetPinFrequencySafe() just before the analogWrite? Seems reasonable but I've never used that PWM library.
I'd go about the task by getting a powers supply for your system, how ever you may power it. Using an Ohm meter find the center resistance of your pot. Leaving the pot set to the center resistance and using the projects final power supply, connect the pot to V+, gnd, and a Ax pin. Make and upload a sketch to the Arduino that will read and print out the reading. If the reading is stable enough, you now have a center point analog reading of the pot.
With the center reading, you can develop code to deliver a center frequency, you decide.
With a 10 bit AD converter you'll get 1023(1024) steps between 0V and V+( possibly 5v). 512 would be your center frequency, and you'll have 512 steps of adjustments above and below the center frequency to code as you like.
to make things more easy (for me at least) i have split the code into 2 halves, they work independently, but when i put them together my duty cycle pot only works from 0 to about 2% and then goes to full on.