I'm new to coding. Can somebody look over this short program?

Most of this is not my own but i made some changes. This is for controlling servos with potentiometers to be used in master slave control.

#include <Servo.h>

Servo myservo3;

Servo myservo5;

Servo myservo6;

Servo myservo7;

Servo myservo8;

Servo myservo9;

int potpin = 0;
int potpin2 = 1;

int potpin3 = 2;

int potpin4 = 3;

int potpin5 = 4;

int potpin6 = 5;

int val = 0;
int val2 = 0;

int val3 = 0;

int val4 = 0;

int val5 = 0;

int val6 = 0;

void setup()
{

myservo3.attach(9);
myservo5.attach(10);

myservo6.attach(11);

myservo7.attach(12);

myservo8.attach(13);

myservo9.attach(14);

}

void loop()
{

val = analogRead(potpin);
val = map(val, 3, 1023, 0, 176);

myservo3.write(val);

delay(25);

val2 = analogRead(potpin2);
val2 = map(val2, 3, 1023, 0, 176);

myservo5.write(val2);

delay(25);

val3 = analogRead(potpin3);
val3 = map(val3, 3, 1023, 0, 175);

myservo6.write(val3);

delay(25);

val4 = analogRead(potpin4);
val4 = map (val4, 3, 1023, 0, 175)

myservo7.write(val4)

delay(25);

val5 = analogRead(potpin5);
val5 = map (val5, 3, 1023, 0, 175)

myservo8.write(val5)

delay(25);

val6 = analogRead(potpin6);
val6 = map (val6, 3, 1023, 0, 175)

myservo9.write(val6)

delay(25);

}

You have many lines that do not have a terminating semicolon. Your program would benefit greatly from the use of arrays and a for loop in order to eliminate the duplicated code. We would benefit if you put your program in code tags.

I added terminating semicolons where they were missing. This is the first project i've worked on and i didn't take the time to teach myself anything. Would you mind informing me about what arrays and for loops are? I also have heard something about a servo header file that i need

#include <Servo.h>

Servo myservo3;

Servo myservo5;

Servo myservo6;

Servo myservo7;

Servo myservo8;

Servo myservo9;


int potpin = 0;
int potpin2 = 1;

int potpin3 = 2;

int potpin4 = 3;

int potpin5 = 4;

int potpin6 = 5;

int val = 0;
int val2 = 0;

int val3 = 0;

int val4 = 0;

int val5 = 0;

int val6 = 0;

void setup()
{

myservo3.attach(9);
myservo5.attach(10);

myservo6.attach(11);

myservo7.attach(12);

myservo8.attach(13);

myservo9.attach(14);

}

void loop()
{

val = analogRead(potpin);
val = map(val, 3, 1023, 0, 176);

myservo3.write(val);

delay(25);

val2 = analogRead(potpin2);
val2 = map(val2, 3, 1023, 0, 176);

myservo5.write(val2);

delay(25);

val3 = analogRead(potpin3);
val3 = map(val3, 3, 1023, 0, 175);

myservo6.write(val3);

delay(25);

val4 = analogRead(potpin4);
val4 = map (val4, 3, 1023, 0, 175);

myservo7.write(val4);

delay(25);

val5 = analogRead(potpin5);
val5 = map (val5, 3, 1023, 0, 175);

myservo8.write(val5);

delay(25);


val6 = analogRead(potpin6);
val6 = map (val6, 3, 1023, 0, 175);

myservo9.write(val6);

delay(25);

}

robotarm.ino (1.05 KB)

Hi there here is how i would do it:

// Include Servo library
#include <Servo.h>

//Creating Servo Objects
  Servo myservo3;
  Servo myservo5;
  Servo myservo6;
  Servo myservo7;
  Servo myservo8;
  Servo myservo9;

//Creating Sensor pins
  const int potpin  = A0;
  const int pot2pin = A1;
  const int pot3pin = A2;
  const int pot4pin = A3;
  const int pot5pin = A4;
  const int pot6pin = A5;

// initialing the variables used to hold the values from Sensors
int val = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;

void setup()
{

// Setting/attachin Servo objects to Arduino Pins
  myservo3.attach(8);
  myservo5.attach(9);
  myservo6.attach(10);
  myservo7.attach(11);
  myservo8.attach(12);
  myservo9.attach(13);
//There is no Pin 14 In general it will refer to analog A0 
}

void loop()
{

// Reading from Pots and assigning to variables
  val  = analogRead(potpin);
  val2 = analogRead(pot2pin);
  val3 = analogRead(pot3pin);
  val4 = analogRead(pot4pin);
  val5 = analogRead(pot5pin);
  val6 = analogRead(pot6pin);  

// Changing the 10 bit values to 8 bit   
  val  = map(val, 3, 1023, 0, 176);
  val2 = map(val2, 3, 1023, 0, 176);
  val3 = map(val3, 3, 1023, 0, 175);
  val4 = map(val4, 3, 1023, 0, 175);
  val5 = map(val5, 3, 1023, 0, 175);
  val6 = map(val6, 3, 1023, 0, 175);

// Writing to the sevos     
  myservo3.write(val);
  myservo5.write(val2);
  myservo6.write(val3);
  myservo7.write(val4);
  myservo8.write(val5);
  myservo9.write(val6);

// delay for 500 milliseconds  
  delay(50);

}

To speed up processing first read all the values off the sensors/pots etc and assign them to variables then start writing to outputs.

I'll post another code using for loops and while loops :slight_smile:

This is the first project i've worked on and i didn't take the time to teach myself anything.]/quote]
Take the time now and you will save more in the future.

Would you mind informing me about what arrays and for loops are?

Arrays are a way of holding several values of the same type, such as your pot pin numbers. A for loop allows you to read the value in an array sequentially (and much more) so you could read the first pot pin and apply its value to the first servo then move onto the next pot pin and corresponding servo with only about 5 lines of code instead of 3 lines for each servo.

I suggest that you do some basic research before going any further. The fact that you did not bother to do that before is not a badge of honour.

Ok, thank you so much for your assistance, both of you!