Convert digital value from analog input pin to pwm output

16 次查看(过去 30 天)
Hello everyone,
My name is Janwillem90 and i'm programming my Arduino uno with simulink blocks. The goal is to control 2 PWM outputs with 1 analog input (potentiometer).
What I want: potentiometer in neutral position (digital value is about 511.5) means no PWM on both output pins. Turning the potentiometer to left (digital value decrease) the PWM output on pin 12 gets proportionally higher (from 0% to 100% dutycycle). Turning the potentiometer from neutral position to the right: digital value increase and the PWM output on pin 11 gets proportionally higher (from 0% to 100% dutycycle).
I've been thinking how to solve this. Because I'm not that skillful with this and willing to learn i thought that the following steps need to be taken:
  • select output pin based on input value
  • convert input value (from 0-1023 max to 0-255 max)
Can anyone please help me how to do this?
Many thanks.
Janwillem90

回答(1 个)

Ege Keskin
Ege Keskin 2019-2-3
You might want to take a simple route and avoid programming your arduino anything other than the Arduino IDE.
You are looking for something like this;
void setup ()
{
%DECLARE YOUR 2 PWM PINS AS OUTPUT, AND THE MIDDLE PIN OF THE POT AS THE INPUT
}
void loop()
{
int Potval=analogRead(your declared pot pin);
int pwm1=0;
int pwm2=0;
if(Potval>512)
{
pwm1 = map(Potval,0,512,0,255);
analogwrite(pwmpin,pwm1);
}
else
{
pwm2 = map(Potval,512,1024,0,255);
analogwrite(pwmpin,pwm2);
}
end
}

社区

更多回答在  Power Electronics Control

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

产品


版本

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by