Convert digital value from analog input pin to pwm output

12 views (last 30 days)
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

Answers (1)

Ege Keskin
Ege Keskin on 3 Feb 2019
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
}

Communities

More Answers in the  Power Electronics Control

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!