Re: Controls for a project

Hi all,
I had the same problem, thanks for the code, It really works!! :slight_smile:

@Robin2 or anyone really. I would like that when i press a push button, the servo motors default a particular angle (maybe 0 degrees)

Below is the code I tried to accomplished this. My push-button is attached to dig pin 2 and i tired inserting the reading of pin 2 is high then set the potvalue to 512 which should be zero. But this didnt seem to work. Could someone assist me to accomplish this. Much thanks

#include <Servo.h>

#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3

Servo servo[4];      //code used for attaching upto 4 servos

byte angle[4] = {90, 90, 90, 90};       // middle point for servo angle

byte potPin[4] = {A0, A1, A2, A3};  // input pins to attach your potentiometers
byte servoPin[4] = {7, 6, 10, 9};    // input pins to attach servos
const int buttonPin = 2 ; //              push-button attached to 2
int buttonState = 0 ;       //   State of the push-button
 int potVal ; 
void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");
  pinMode(buttonPin, INPUT) ;          // set push button as an input

  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH);
   {
    potVal == 512;}
}

void readPotentiometers() {
  int potVal;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    
    if (potVal < 200) {         // dead zone for the joystick I used is 200 to 550.
      angle[n] += 1;
      if (angle[n] > 170) {
        angle[n] = 170;
      }
    }
    
    if (potVal > 550) {         // deadzone upper value
      angle[n] -= 1;
      if (angle[n] < 10) {
        angle[n] = 10;
      }
    }

  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].write(angle[n]);
  }
}