Convert digital value from analog input pin to pwm output

12 ビュー (過去 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
}

コミュニティ

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by