user80082: My project is to use an Arduino to control two solenoid valves with PWM and receive an input signal from a sensor as feedback.

The valves direct the flow of water which cause a hydraulic actuator to move which is detected by an LVDT sensor.
What I want my program to do is to allow me to set a desired displacement as a goal which causes the Arduino to dynamically change the duty cycle of the pwm signal based off of the feedback input the Arduino receives. I am going to implement a PID to control it.
The circuit consist of the Arduino sending PWM signals to two logic level NPN mosfets to control the solenoids that are powered by 2 external 12V sources.


What I specifically need help with is how to write the part of the program that will let me produce 2 PWM signals that will allow me to change the frequency of the pwm waves so that the solenoids have enough time to switch and then also the ability to set the duty cycle.
I am a total novice programmer and its my first time working with electronics. I'm just hoping someone can point me in the right direction.

user7508: If you only need two PWM pins, I suggest using hardware PWM with
Timer 1. The Timer 1 library makes the process quite easy. It
supports frequencies from 0.12 Hz up to a few MHz, although the
resolution degrades above 7.8 kHz.
Compared to an interrupt-based solution, hardware PWM has less jitter
and it does not consume CPU cycles. The drawback is that you will be
limited to 2 channels operating at the same frequency, but this
doesn't seem to be an issue for your project.

80082: <p>My project is to use an Arduino to control two solenoid valves with PWM and receive an input signal from a sensor as feedback.</p>
<p><a href="https://i.stack.imgur.com/LH76m.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LH76m.jpg" alt="enter image description here" /></a></p>
<p>The valves direct the flow of water which cause a hydraulic actuator to move which is detected by an LVDT sensor.</p>
<p>What I want my program to do is to allow me to set a desired displacement as a goal which causes the Arduino to dynamically change the duty cycle of the pwm signal based off of the feedback input the Arduino receives. I am going to implement a PID to control it.</p>
<p>The circuit consist of the Arduino sending PWM signals to two logic level NPN mosfets to control the solenoids that are powered by 2 external 12V sources.</p>
<p><a href="https://i.stack.imgur.com/CT8QG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CT8QG.jpg" alt="enter image description here" /></a></p>
<p><a href="https://i.stack.imgur.com/n6X9D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n6X9D.jpg" alt="enter image description here" /></a></p>
<p>What I specifically need help with is how to write the part of the program that will let me produce 2 PWM signals that will allow me to change the frequency of the pwm waves so that the solenoids have enough time to switch and then also the ability to set the duty cycle.</p>
<p>I am a total novice programmer and its my first time working with electronics. I'm just hoping someone can point me in the right direction.</p>
 7508: <p>If you only need two PWM pins, I suggest using hardware PWM with
Timer 1. The <a href="https://github.com/PaulStoffregen/TimerOne" rel="nofollow noreferrer">Timer 1</a> library makes the process quite easy. It
supports frequencies from 0.12 Hz up to a few MHz, although the
resolution degrades above 7.8 kHz.</p>
<p>Compared to an interrupt-based solution, hardware PWM has less jitter
and it does not consume CPU cycles. The drawback is that you will be
limited to 2 channels operating at the same frequency, but this
doesn't seem to be an issue for your project.</p>
