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);
As I said before, there is no need for pwmtimer2 instance.
Timer2.setCompare(TIMER_CH4, i);One button is working “PB5”, PB3 is not.
Is it the same for both buttons?
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..
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 ?
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);
}
That will be ok, I Am more concern about second channel.
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.
I was thinking to use another timer with 10x lower frequency to control regulation. I need to control about +/_10% of the width
Thanks guys for help.
How to switch from delay 300 to 30 when button is pressed longer than 2 sec ?



