Multiplying one code for 6 servos - problem and no time

Hello.

I am doing a small project for disabled person (guitar playing tool).
I have a problem. I have got a code that is done for one servo and I need to control 6 servos in the same way.

I am a beginner in programming. I have no idea where to start and I have two days to do it.
My attepts were quite bad.
Have you got any helpful advices good people?

Huge thanks for instrest.

Simon.

Code:

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.


#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
const int buttonPin = 2;     // the number of the pushbutton pin
int servoPosition;

void changePosition(){

}

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
  servoPosition = 30;
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

int prev = HIGH;
int state = HIGH;

void loop()
{
  prev = HIGH;
  state = digitalRead(buttonPin);
  while((state == HIGH && state == prev)){
     state = digitalRead(buttonPin);
     prev = state;
  }

  Serial.print(state);

  if(servoPosition == 30){
    servoPosition = 150;
  } else {
    servoPosition = 30;
  }

  myservo.write(servoPosition);
  delay(1000);

  state = digitalRead(buttonPin);
  while((state == LOW && state == prev)){
     state = digitalRead(buttonPin);
    prev = state;
  }

}

Hi Simon. Can you explain what the sketch is supposed to do. I can somewhat guess by reading the sketch but need more clarity from you. Will there be 6 buttons also?

Paul

Hello.

Yes, yes. There are 6 buttons (exactly 6 cables tahen from inputs to ground with 1K resistors which are touching when needed and triggering servos).

The loops are made for holding the button state and change it after release (and servos).

I have some progress with all that. I have multiplied two times some lines and changed couple of functions conditions.

I know it is a very crappy way and it is working not particular well. Better than nothing indeed.

Like this:

#include <Servo.h>

Servo myservo;
Servo myservo2;// create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
const int buttonPin = 2;
const int buttonPin2 = 4;  // the number of the pushbutton pin
int servoPosition;
int servoPosition2;

void changePosition(){

}

void setup()
{
  myservo.attach(9);
  myservo2.attach(10);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
  servoPosition = 30;
  servoPosition2 = 30;
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  digitalWrite(buttonPin, HIGH);
  digitalWrite(buttonPin2, HIGH);
}

int prev = HIGH;
int prev2 = HIGH;
int state = HIGH;
int state2 = HIGH;

void loop()
{
  prev = HIGH;
  prev2 = HIGH;
  state = digitalRead(buttonPin);
  state2 = digitalRead(buttonPin2);
  while((state == HIGH && state == prev && state2 == HIGH && state2 == prev2)){
    
     state = digitalRead(buttonPin);
     prev = state;
    
     state2 = digitalRead(buttonPin2);
     prev2 = state2;
     
  }

  Serial.print(state);
  Serial.print(state2);
  
if(prev != HIGH)
{
  if(servoPosition == 30){
    servoPosition = 150;
  } else {
    servoPosition = 30;
  }
}

if(prev2 != HIGH)
{
  if(servoPosition2 == 30){
    servoPosition2 = 150;
  } else {
    servoPosition2 = 30;
  }
}

  myservo.write(servoPosition);
  myservo2.write(servoPosition2);
  delay(1000);

  state = digitalRead(buttonPin);
  state2 = digitalRead(buttonPin2);
  while((state == LOW && state == prev) || (state2 == LOW && state2 == prev2)){
     state = digitalRead(buttonPin);
     state2 = digitalRead(buttonPin2);
    prev = state;
    prev2 = state2;
  }

}

I have checked this has compiled OK, but further than that I cannot test it for you.

#include <Servo.h>

#define servoCount 6

Servo myservo[servoCount];// create servo object to control a servo
                // a maximum of eight servo objects can be created

const int buttonPin[servoCount] = {2, 4, 5, 6, 7, 8};  // the number of the pushbutton pin
const int servoPin[servoCount] = {9, 10, 11, 12, 13, 3};  // the number of the servo pin

int servoPosition[servoCount] = {30, 30, 30, 30, 30, 30}; // variable to store the servo position
int prev[servoCount] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};


void setup() {
  
  for (int s = 0; s < servoCount; s++) {
    
    myservo[s].attach(servoPin[s]);
    myservo[s].write(servoPosition[s]);
    pinMode(buttonPin[s], INPUT_PULLUP);
    
  }
  
  Serial.begin(9600);
}

void loop() {
  
  for (int s = 0; s < servoCount; s++) {
    
    int state = digitalRead(buttonPin[s]);
    
    if (state != prev[s]) {

      Serial.print("Button ");
      Serial.print(s);
      Serial.print(" state = ");
      Serial.println(state);
      
      if(prev[s] != HIGH) {
        
        if(servoPosition[s] == 30){
          servoPosition[s] = 150;
        }
        else {
          servoPosition[s] = 30;
        }
        myservo[s].write(servoPosition[s]);
      }
      
      prev[s] = state;
    }
  }
}