willyerburgh
Published © GPL3+

Digital Depth Control

A method of smoothly controlling solenoids to ensure any number of rams are equal length, and the ability to digitally set that depth.

BeginnerFull instructions provided791
Digital Depth Control

Things used in this project

Story

Read more

Schematics

Hydraulic Schematic

This shows you how the hydraulics are meant to be laid out.

Electronic Schematic

This shows you how the electronics should look.

Code

Smooth Depth Control Code

Arduino
//libs
#include <LiquidCrystal.h>

//pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
const byte leds[3] = {3, 5, 6};
const byte potPins[3] = {A2, A3, A4};
const byte setPin = A0;
const byte ledT = 2;

//vars
int potVals[3];
int highestVal;
int lowestVal;
int averageVal;
int setVal;
int offTrackUp[3];
int offTrackDown[3];
byte intensity[3];
const byte stroke = 200;

void setup() {

  //set up lcd
  lcd.begin(16, 2);
  lcd.clear();
  
  //set led pins
  for(byte i = 0; i < 3; i++)  {
    pinMode(leds[i], OUTPUT);
  }
  
  //set pot pins
  for(byte i = 0; i < 3; i++)  {
    pinMode(potPins[i], INPUT);
  }
  //setting up mis pins
  pinMode(setPin, INPUT);
  pinMode(ledT, OUTPUT);
}

void loop() {

  //finding the highest and lowest pots
  highestVal = 0;
  lowestVal = 1024;
  for (byte i = 0; i < 3; i++)  {
    potVals[i] = analogRead(potPins[i]);
    if(potVals[i] > highestVal) highestVal = potVals[i];
    if(potVals[i] < lowestVal) lowestVal = potVals[i];
  }
  //finding the average vals from these
  averageVal = (highestVal + lowestVal) / 2;

  //reading the setting pot and depth
  setVal = analogRead(setPin);
  int depth = map(setVal, 0, 1023, 0, stroke);

  //displaying the depth on the lcd
  lcd.setCursor(0,0);
  lcd.print("Depth: ");
  lcd.print(depth);
  delay(200);
  lcd.clear();

  //if average is lower than set pot, then turn on trans led and go to handleUp
  if (averageVal < 0.95*setVal) {
    HandleUp ();
    digitalWrite(ledT, HIGH);
  }
  //if the average is higher than set pot, turn off trans led and handleDown 
  if (averageVal > 1.05*setVal) {
    HandleDown ();
    digitalWrite(ledT, LOW);
  }
  //if they are within range, handleSet
  if (averageVal > 0.95*setVal && averageVal < 1.05*setVal) {
    HandleSet ();
  }
}
  
void HandleUp () {

  for (byte i = 0; i < 3; i++) {
    //create a vaule to let us know how far out of sync one pot is
    offTrackUp[i] = potVals[i] - lowestVal;

    //if this is smaller than 200, then map it's vaule to scale down the further away from lowestVal
    if (offTrackUp[i] < 200) {
      intensity[i] = map (offTrackUp[i], 0, 200, 255, 0);
      analogWrite(leds[i], intensity[i]);
    }
    if (offTrackUp[i] > 200) {
      digitalWrite(leds[i], LOW);
    }
  }
}

void HandleDown () {

  for (byte i = 0; i < 3; i++) {
    //create a vaule to let us know how far out of sync one pot is
    offTrackDown[i] =  highestVal - potVals[i];

    //if this is smaller than 200, then map it's vaule to scale down the further away from heightestVal
    if (offTrackUp[i] < 200) {
      intensity[i] = map (offTrackDown[i], 0, 200, 255, 0);
      analogWrite(leds[i], intensity[i]);
    }
    if (offTrackDown[i] > 200) {
      digitalWrite(leds[i], LOW);
    }
  }
}

void HandleSet () {

  for (byte i = 0; i < 3; i++) {
    //if the vals are between a small range, then turn off, if not, leave on until they are
    digitalWrite(leds[i], potVals[i] > 0.95*setVal && potVals[i] < 1.05*setVal ? LOW : HIGH);
  }
}

Credits

willyerburgh
2 projects • 1 follower

Comments