Re: Controls for a project

Robin2:
With the code style that I suggested you would just need to read the extra Pots in the Pot function and add one extra line for each motor to the motors function.

There would be nothing wrong with having a separate function for each motor.

...R

Robin2. Here's what i came up with for 4 servos. is there any way i can simplify the program

#include <Servo.h>;
Servo armServo;
Servo jibServo;
Servo bucServo;
Servo swiServo;

int armPos;
int jibPos;
int bucPos;
int swiPos;
int pos;

int delayTime;

int potPin;

const int buttonPin = 2;
const int startPin = 13;
int ledState = LOW; 
int buttonState;         // current state of the button
int lastButtonState = LOW;     // previous state of the button
long lastDebounceTime = 0;  
long debounceDelay = 50;

void setup(){
  
  pinMode(buttonPin,INPUT);
  pinMode(startPin,OUTPUT);
  digitalWrite(startPin, ledState);
  
  armServo.attach(12);
  jibServo.attach(11);
  bucServo.attach(10);
  swiServo.attach(9);
  Serial.begin(9600);
}

void loop() {
  
  POWER();
  if (ledState == LOW) {
    Serial.println("Power is Off");
    delay(100);    
    return;
  }
  
  ARM();
  JIB();
  BUC();
  SWI();
}

void POWER() {
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  digitalWrite(startPin, ledState);
  lastButtonState = reading;
}
  
void SPEED() {
  int servospeed = analogRead(potPin);
  servospeed = map(servospeed,0,1023,-70,70);
  servospeed = abs(servospeed);
  servospeed = map(servospeed,70,0,1,100);
  delayTime = servospeed;
}

void POSITIONS() {
  int x = analogRead(potPin);
  if (x < 500) {
    DOWN();
  }
  else if (x > 525) {
    UP();
  }
}

void DOWN() {
  if (pos > 30) {
    pos = pos - 1;
    delay(delayTime);
  }
}
                    
void UP() {
  if (pos < 145) {
    pos = pos + 1;
    delay(delayTime);
  }  
}

void ARM() {
  potPin = A0;
  pos = armPos;
  SPEED();
  POSITIONS();
  armServo.write(pos);
  armPos = pos;
  delay(1);
}

void JIB() {
  potPin = A1;
  pos = jibPos;
  SPEED();
  POSITIONS();
  jibServo.write(pos);
  jibPos = pos;
  delay(1);
}

void BUC() {
  potPin = A2;
  pos = bucPos;
  SPEED();
  POSITIONS();
  bucServo.write(pos);
  bucPos = pos;
  delay(1); 
}

void SWI() {
  potPin = A3;
  pos = swiPos;
  SPEED();
  POSITIONS();
  swiServo.write(pos);
  swiPos = pos;
  delay(1); 
}