Hi
How tom change generator frequency using buttons ?
//https://www.i-programmer.info/programming/148-hardware/17804-programming-the-esp32-in-c-pwm-first-example.html?start=4#google_vignette
#include <driver/ledc.h>
int k;
void setup() {
Serial.begin(115200);
pinMode(16, INPUT_PULLUP); //k++
pinMode(17, INPUT_PULLUP); //k--
// Конфигурация таймера
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 10000, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
ledc_channel_config_t channelConfig1 = {
.gpio_num = 18,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 0
};
ledc_channel_config(&channelConfig1);
ledc_channel_config_t channelConfig2 = {
.gpio_num = 19,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_1,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 256
};
ledc_channel_config(&channelConfig2);
}
void loop()
{
if (digitalRead(16) == LOW)
{
k++;
}
if (digitalRead(17) == LOW)
{
k--;
}
Serial.println(k);
delay( 100 );
}
/*
void changeFreq(float freq) {
if (digitalRead(16) == LOW) {
k++;
}
if (digitalRead(17) == LOW) {
k--;
}
Serial.println(k);
delay(100);
}
*/
Use a global variable instead of the fixed 10000. Change the global variable to what you want and call the function again.
I understand what to do but I don't know how.
tom321:
but I don't know how.
Sorry to tell you that you actually need to spend some time learning about programming and about the C language.
I did it this way but k doesn't change
//https://www.i-programmer.info/programming/148-hardware/17804-programming-the-esp32-in-c-pwm-first-example.html?start=4#google_vignette
#include <driver/ledc.h>
int k;
//+++++++++++++++++++++++++++++++
void frequencyChange() //void somefunc()
// your variable here
{
if (digitalRead(16) == LOW) {
k++;
}
if (digitalRead(17) == LOW) {
k--;
}
//++++++++++++++ // put your setup code here, to run once:+++++++++++++++
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = k, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
Serial.println(k);
Serial.println(" k1 ");
//+++++++++++++++++++++++++++++
}
//+++++++++++++++++++++++++++++++++
void setup() {
Serial.begin(115200);
pinMode(16, INPUT_PULLUP); //k++
pinMode(17, INPUT_PULLUP); //k--
/*
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 10000, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
*/
ledc_channel_config_t channelConfig1 = {
.gpio_num = 18,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 0
};
ledc_channel_config(&channelConfig1);
ledc_channel_config_t channelConfig2 = {
.gpio_num = 19,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_1,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 256
};
ledc_channel_config(&channelConfig2);
}
void loop()
{
Serial.println(k);
Serial.println(" k2 ");
delay( 100 );
}
tom321:
tom
Tom who? Have you asked him?
Or are you referring to yourself in the 3rd person?
1 Like
So, why not actually debug the code by printing the values where you actually change based on pin reading values? Debugging means tracking down the source of the problem, not just displaying a final value.
k2 does not change = 0, when pressing the buttons
Why not actually display what value is being read form the buttons, rather than show what subsequent code is producing? Come on, get to the source of the error!!!!
tom321
May 10, 2025, 6:29pm
11
and this are the readings when buttons are inside void loop()
void loop() {
if (digitalRead(16) == LOW) {
k++;
}
if (digitalRead(17) == LOW) {
k--;
}
Serial.print(" k2 = ");
Serial.println(k);
delay(100);
}
And what would cause that to happen? Time for a schematic showing the wiring of your switches!
tom321
May 10, 2025, 7:12pm
13
switches are working, k2 is changing when buttons are inside void loop.
ledsyn
May 10, 2025, 7:41pm
14
*Shows picture of k2 = 0*
*Now shows picture of k2 changing*
It sounds like the setup is identical but obviously it cannot be. What did you change?
Now that you have a variable that changes, what do you think can be done with it?
tom321
May 10, 2025, 11:06pm
16
Variable they should change when they are in
void frequencyChange()
loop, not in
void loop()
ledsyn
May 11, 2025, 5:22am
18
If post #16 was directed at me, you need to use Reply.
Just as @camsysca answered you
tom321:
frequencyChange()
is never called. WYSIWYG.
tom321
May 11, 2025, 11:05am
19
Working code = frequency changes
//https://www.i-programmer.info/programming/148-hardware/17804-programming-the-esp32-in-c-pwm-first-example.html?start=4#google_vignette
#include <driver/ledc.h>
int k;
//const int btn_inc = 16;
//const int btn_dec = 17;
const int maxFreq = 2000;
const int minFreq = 1000;
//unsigned long tim_btn_inc, tim_btn_dec;
//+++++++++++++++++++++++++++++++
// variables will change:
int freq = maxFreq;
int interval = 1; // variables will change:
/*
//++++++++++++
void frequencyChange() //void somefunc()
// your variable here
{
pinMode(16, INPUT_PULLUP); //k++
pinMode(17, INPUT_PULLUP); //k--
int k;
if (digitalRead(16) == LOW) {
k++;
}
if (digitalRead(17) == LOW) {
k--;
}
//++++++++++++++ // put your setup code here, to run once:+++++++++++++++
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = k, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
Serial.print(" k1 = ");
Serial.println(k);
//+++++++++++++++++++++++++++++
}
//+++++++++++++++++++++++++++++++++
*/
void setup() {
Serial.begin(115200);
pinMode(32, INPUT_PULLUP); //k++
pinMode(33, INPUT_PULLUP); //k--
/*
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 10000, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
*/
ledc_channel_config_t channelConfig1 = {
.gpio_num = 12,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 0
};
ledc_channel_config(&channelConfig1);
ledc_channel_config_t channelConfig2 = {
.gpio_num = 27,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_1,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 256
};
ledc_channel_config(&channelConfig2);
}
void loop()
{
if (digitalRead(32) == LOW) {
k++;
if (k >18000) k = 18000;
// Serial.println(k); //for debugging purposes
}
if (digitalRead(33) == LOW) {
k--;
if (k < 5000) k = 5000;
//Serial.println(k); //for debugging purposes
}
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = k, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
//+++++++++++++++++++++++++++++++
Serial.print(" k3 = ");
Serial.println(k);
delay( 2 );
}
tom321
May 11, 2025, 11:08am
20
When I create separate loop for frequency changing code is not working = no output signal.
//https://www.i-programmer.info/programming/148-hardware/17804-programming-the-esp32-in-c-pwm-first-example.html?start=4#google_vignette
#include <driver/ledc.h>
int k;
//const int btn_inc = 16;
//const int btn_dec = 17;
const int maxFreq = 2000;
const int minFreq = 1000;
//unsigned long tim_btn_inc, tim_btn_dec;
//+++++++++++++++++++++++++++++++
// variables will change:
int freq = maxFreq;
int interval = 1; // variables will change:
/*
//++++++++++++
void frequencyChange() //void somefunc()
// your variable here
{
pinMode(16, INPUT_PULLUP); //k++
pinMode(17, INPUT_PULLUP); //k--
int k;
if (digitalRead(16) == LOW) {
k++;
}
if (digitalRead(17) == LOW) {
k--;
}
//++++++++++++++ // put your setup code here, to run once:+++++++++++++++
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = k, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
Serial.print(" k1 = ");
Serial.println(k);
//+++++++++++++++++++++++++++++
}
//+++++++++++++++++++++++++++++++++
*/
void setup() {
Serial.begin(115200);
pinMode(32, INPUT_PULLUP); //k++
pinMode(33, INPUT_PULLUP); //k--
/*
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 10000, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
*/
ledc_channel_config_t channelConfig1 = {
.gpio_num = 12,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 0
};
ledc_channel_config(&channelConfig1);
ledc_channel_config_t channelConfig2 = {
.gpio_num = 27,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_1,
.timer_sel = LEDC_TIMER_0,
.duty = 512,
.hpoint = 256
};
ledc_channel_config(&channelConfig2);
}
void loop()
{
}
//++++++++++++
void frequencyChange() //void somefunc()
// your variable here
{
if (digitalRead(32) == LOW) {
k++;
if (k > 18000) k = 18000;
// Serial.println(k); //for debugging purposes
}
if (digitalRead(33) == LOW) {
k--;
if (k < 5000) k = 5000;
//Serial.println(k); //for debugging purposes
}
ledc_timer_config_t timerConfig = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = k, //+++++++++++++++++++++++++++++
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&timerConfig);
//+++++++++++++++++++++++++++++++
Serial.print(" k3 = ");
Serial.println(k);
delay( 2 );
}