필터 지우기
필터 지우기

Convert digital value from analog input pin to pwm output

조회 수: 16 (최근 30일)
Janwillem90
Janwillem90 2019년 2월 2일
답변: Ege Keskin 2019년 2월 3일
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 Community

카테고리

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