2

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

enter image description here

As can be seen from the picture, 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 frequency based off of the feedback input the Arduino receives.

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.

Based off of youtube videos this is the code I've written.

int myPin1=3;
int myPin2=5;
int readPin=A3;

//V2 is input from sensor
int V2=0;
int delayTime=50;
float D1=0.5;
float D2=0.5;
int readVal;

int goal=;

void setup() {
  // Set pin states:
  pinMode(myPin1,OUTPUT);
  pinMode(myPin2,OUTPUT);
  pinMode(readPin,INPUT);
  Serial.begin(9600);

  //set frequency of the PWM pins
  SetPinFrequencySafe(myPin1, 1000);
  SetPinFrequencySafe(myPin2, 1000);
}

void loop() {
  // Volatge reading from the VTLD:
  readVal=(5/1023)analogRead(readPin);
  V2 = (5./1023.)*readVal
  Serial.print("Voltage is ");
  Serial.println(V2);


  //PWM for the 2 output pins  
  analogWrite(myPin1,D1);
  
  analogWrite(myPin1,D2);
  delay(delayTime);


  //Varying of dutcy cycle untill getting the desired actuator position
while (V2!=goal) {   
  if (V2>goal) {
    D1=D1+0.01;
    D2=D2-0.01;
  }
   if (V2<goal) {
    D1=D1-0.01;
    D2=D2+0.01;
  }
}
  if (V2=goal) {
    D1=0.5
    D2=0.5
  }

  
}

I have seen people using PID libraries but I'm not sure which one is suitable to use for my project.

2
  • 2
    I successfully used this PID library before. Just try it
    – chrisl
    Sep 2 at 13:03
  • 2
    Welcome Marius, it would be much easier to answer your question if we had more information. Can you post your schematic or drawing of the circuit. What are you using to drive the solenoids, what is the sensor? Post links to the hardware devices that give technical information.
    – Gil
    Sep 2 at 15:24

Browse other questions tagged or ask your own question.