[SOLVED] PWM buttons control

ted
Wed Jun 13, 2018 9:35 pm
Hi
I trying to control PWM duty by buttons, but I have no pulses on PA3 unless this line is disabled and contain of the loop.

Timer2.attachInterrupt(0, Tim2_ISR);


stevestrong
Thu Jun 14, 2018 7:35 am
Don’t mix pwmtimer2 with Timer2, use only one of them.
As I said before, there is no need for pwmtimer2 instance.

edogaldo
Thu Jun 14, 2018 8:00 am
In PWM context
Timer2.setCompare(TIMER_CH4, i);

ted
Thu Jun 14, 2018 10:08 am
Thanks edogaldo.
One button is working “PB5”, PB3 is not.

edogaldo
Thu Jun 14, 2018 10:26 am
How is the buttons wiring?
Is it the same for both buttons?

ted
Thu Jun 14, 2018 10:47 am
Separate two buttons, I can see that voltage on pin is dropping to 0V when I pressed the button, but nothing happens with the width of the pulses

edogaldo
Thu Jun 14, 2018 11:10 am
Are the buttons wired between the PIN and GND or between the PIN and VCC?
In the first case you have to change your code setting the pins mode to INPUT_PULLUP (default HIGH) and checking for the pins to go LOW..

ted
Thu Jun 14, 2018 11:49 am
between the PIN and VCC

ted
Thu Jun 14, 2018 12:16 pm
I changed to PB6 and is working. Now I need to add serial begin 9600 and print ln “i”

ted
Thu Jun 14, 2018 12:32 pm
Serial monitor is working, but the width is changing to fast, so is hard to set up designated number.
Can be reduced by 10x ?

Another question.
In the first code there are 2 channels, how to get a second channel (not regulated) in your version ?


ted
Thu Jun 14, 2018 12:45 pm
Maybe “delay(100);” somewhere ?

volatile int i = 128; //initializing a integer for incrementing and decrementing duty ratio.

void setup() {
pinMode(PB6, INPUT_PULLDOWN);// sets the pin0 as output
pinMode(PB5, INPUT_PULLDOWN);// sets the pin1 as output
pinMode(PA3, PWM);
analogWrite(PA3, i);
Serial.begin(9600);
}
// Serial.begin(9600);
//Serial.println(i);
void loop()
{
if (digitalRead(PB6) == HIGH)
{
if (i < 255)
{
i++;//if pin PB3 is pressed and the duty ratio value is less than 255
analogWrite(PA3, i); // analogWrite values from 0 to 255
delay(30);
}
}
if (digitalRead(PB5) == HIGH)
{
if (i > 0)
{
i--;// if pin PB5 is pressed and the duty ratio value is greater than 0
analogWrite(PA3, i); // analogWrite values from 0 to 255
delay(30);
}
}
Serial.println(i);
}


edogaldo
Thu Jun 14, 2018 1:28 pm
What about printing only when you change i?!

ted
Thu Jun 14, 2018 2:10 pm
Thanks for your input.
That will be ok, I Am more concern about second channel.

ted
Thu Jun 14, 2018 6:35 pm
Thanks edogaldo
I did it, everything is working as I need, except the speed, I need to reduced it because by pressing the buttons is hard to regulate the width with accuracy of one pulse.

fpiSTM
Thu Jun 14, 2018 7:43 pm
Maybe you should use an EXTI for button press using a RISING or FALLING trigger.

ted
Thu Jun 14, 2018 11:20 pm
EXIT – is something new to me, google doesn’t say much about this, can you give me more info ?
I was thinking to use another timer with 10x lower frequency to control regulation. I need to control about +/_10% of the width

ted
Fri Jun 15, 2018 2:39 am
I did it, delay(300); instead delay(30);
Thanks guys for help.

ted
Fri Jun 15, 2018 7:50 am
Some improvements.
How to switch from delay 300 to 30 when button is pressed longer than 2 sec ?

Leave a Reply

Your email address will not be published. Required fields are marked *