Pot and button servo control

Hey everybody looking for a little help with some code that I found that controls servos with joy sticks. It reads the analog values of the pots and moves the servos in 1 degree increments when the pot is above or below the set thresholds. The code works great but I am new to programming and the way the code is written is far to advanced for me. I’m looking for some help to break this code down into simple mans coding so I can add some push buttons that will write specific locations to the servos. The coding is done very effectively as you can see its just a little to hard for me to understand. Where I get lost is where byte is used I’ve done lots of googling and i cant seem to find out what it is used for. I’m not against learning the coding but I am on a deadline for this project so any help is appreciated.

#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

void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");


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

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
}

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

solidcon:
I'm not against learning the coding but I am on a deadline

Obviously you have 'been against learning' in the past.

Learn some C basics (there ist nothing advanced in your code).

There are plenty of tutorials, videos, books, blogs, newsgroups, ...

I wont do your homework, unless you pay me (or the problem is interresting).

solidcon:
The code works great but I am new to programming and the way the code is written is far to advanced for me.

I suspect I wrote that code (or it's father) but I have lost the link to where I wrote it. It would be great if you can post the link from where you found it.

To be honest, I think it would be hard to make it much simpler.

The code reads the value of the joystick. If it is above a certain value it decrements the position of the servo. If it is below a different value it increments the position of the servo.

If you explain what you don't understand I will try to help.

...R

You are correct robbin2

Also thanks Whandall for letting me know that there is information on the internet!

I understand exactly what the code is doing i just don't understand the "language" How can i address the servo pins individually? I want to write a if statement that will write different positions to the servos when a button is pushed. Can i just say servo[2] to address that servo?

My pleasure!

this is as close as i can fake my way though understanding this

#include <Servo.h>

#define leftLightPan 0
#define leftLightTilt 1
#define rightLightPan 2
#define leftLightTilt 3

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

byte angle[4] = {90, 45, 90, 135};       // this is where the servos start and where i want them to return when i push the button. Light facing forward
                                         //I'd also like to have a second set of positions if possible. lights facing backwards 

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 forwardButton = 2;         // button to push to point lights forward
const int backwardsButton = 3;       // button to push to point lights backwards
int buttonState = 0;


void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");
  
  
  pinMode(forwardButton,INPUT); 
  pinMode(backwardsButton,INPUT) ;


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

void loop() {
  
  checkButtons();
  readPotentiometers();
  moveServos();
  delay(10);
}
void checkButtons(){
  buttonState = digitalRead(forwardButton);
  for (byte n = 0; n < 4; n++);
  (buttonState == HIGH){servoPin[n].write(angle[n])};
  
}

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

To be honest, I think it would be hard to make it much simpler.

Deconstructing the recursive looping into inline code can make it easier for some persons to understand.

solidcon:
this is as close as i can fake my way though understanding this

Thank you very much for the link - much appreciated.

Your idea of adding a function to read the button is good. But you are going wrong when you try to use the button.

If you look at the function moveServos() you will see that it uses the value in the array angle

Your button code should just write the desired value into the array and then the moveServos() function will use it. For example

if (buttonState == LOW) { // assume LOW == button pressed
   angle[0] = 90;  // make servo0 go to 90 deg
}

There is one complication - the function readPotentiometers() will over-write the value from your button. This is a design problem rather than a code problem. You have to decide how the button and the joystick must interact. At the very simplest (and for testing) you can comment-out the call to readPotentiometers() so it is not used at all.

...R
PS, after writing the above it crossed my mind that may want to replace the joystick with buttons?
I will wait to hear from you before going down that route - it is not complex.

it might help if i explain what I'm doing. I'm making a pan tilt system for a set of spot lights on the outside of a pickup truck. I'd like to use a single joy stick to control each spot light which the code you wrote works for perfectly. What the buttons do will set the lights to preset locations like facing forward or backwards. If possible I'd like the servos to move slowly to those locations.

solidcon:
What the buttons do will set the lights to preset locations like facing forward or backwards. If possible I’d like the servos to move slowly to those locations.

That sounds like you want to use the joystick to fine-tune the positions.

You could do that by having two position variables - call then mainPos and adjustPos.

The button sets mainPos. The joystick varies adjustPos.
The servo actually moves to the sum of the two positions. Crudely myServo.write(mainPos + adjustPos);

…R