willyerburgh
Published © GPL3+

Ensuring Potentiometers Move in Sync!

This code will help make your potentiometers move in sync by reading their locations and making sure they stay within a small range.

BeginnerProtip804
Ensuring Potentiometers Move in Sync!

Things used in this project

Story

Read more

Schematics

roatary_flow_vs_ardunio_KNm00KztUa.jpg

Code

Flow Divider Code

Arduino
Always the precise control of cylinder extension
//pins
const byte leds[3] = {2, 3, 4};
const byte potPins[3] = {A0, A3, A5};
const byte buttonPin = 5;

//vars
int potVals[3];
int highestVal;
int lowestVal;
boolean overShootUp = false;
boolean overShootDown = false;
byte DownUp;

void setup() {
  //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);
  }
  //set DownUp buttonState
  pinMode(buttonPin, INPUT_PULLUP);
}

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];
  }

  DownUp = digitalRead(buttonPin);
  
  //setting the direction of travel
  if (DownUp == HIGH) {
    if (overShootUp) {
     handleShootUp();
    }
    else {
      handleNormalUp();
    }
  }
  if (DownUp == LOW) {
    if (overShootDown) {
     handleShootDown();
    }
    else {
      handleNormalDown();
    }
  }
}


void handleNormalUp () {
  
  //is one pot going too high
  if ((highestVal - lowestVal) > 100){
    overShootUp = true;
  }
  //pots are within range move them all
  if (highestVal - lowestVal < 50) {
    for(byte i = 0; i < 3; i++) {
      digitalWrite(leds[i], HIGH);
    }
  }
  //if one pot moves to fast then turn it off
  else {
    for(byte i = 0; i < 3; i++) {
      digitalWrite(leds[i], highestVal - potVals[i] >=40 ? HIGH : LOW);
    }
  }
}

void handleNormalDown () {

  //is one pot going too low
  if ((lowestVal - highestVal) < 100){
    overShootDown = true;
  }
  //pots are within range move them all
  if (lowestVal - highestVal < 50) {
    for(byte i = 0; i < 3; i++) {
      digitalWrite(leds[i], HIGH);
    }
  }
  //if one pot moves too fast then turn if off
  else {
    for(byte i = 0; i < 3; i++) {
      digitalWrite(leds[i], highestVal - potVals[i] >=40 ? HIGH : LOW);
    }
  }
}

void handleShootUp() {
  //check if pot is back in range
  if (highestVal - lowestVal < 50) {
    overShootUp = false;  
  }
  //pot is not in range, turn it off
  else {
    for (byte i = 0; i < 3; i++) {
      if (lowestVal == potVals[i]) {
        digitalWrite(leds[i], HIGH);
      }
      if (highestVal == potVals[i]) {
        digitalWrite(leds[i], LOW);
      }
    }
  }
}

void handleShootDown() {
  //check if pot is back in range
  if (highestVal - lowestVal < 50) {
    overShootDown = false;  
  }
  //pot is not in range, turn it off
  else {
    for (byte i = 0; i < 3; i++) {
      if (highestVal == potVals[i]) {
        digitalWrite(leds[i], HIGH);
      }
      if (lowestVal == potVals[i]) {
        digitalWrite(leds[i], LOW);
      }
    }
  }
}

Credits

willyerburgh
2 projects • 1 follower

Comments