Hi! It's my first post on this forum, so it is my duty to greet everyone thats willing to help people like me overcome trivial, and those less trivial problems with Arduino and so on!
The PR part is done, so it's time to describe my little problem
My problem is associated with programming of the Adafruit 12bit PWM Servoshield.
One pushbutton is used to select the motors, the rest to control servo position.
Motors are arranged in this order:
( 1 )
( 2,3 )
( 4,5 )
( 6 )
( 7 )
( 8 )
The code i wrote was supposed to be as simple as possible, but something went wrong.
All motors are moving at once trying to reach maximum position, and only one pushbutton is working. If pressed, all motors are going to minimum. If not pressed, all servos are trying to reach maximum again.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <Servo.h>
#include <LiquidCrystal.h>
int button1 = 6; //defines button (button1), and the pin (1)
int button2 = 7; //defines button (button2), and the pin (2)
int button3 = 8; //defines button (button3), and the pin (3)
byte buttonPin = 2;
byte buttonPresses = 0;
byte lastPressCount = 0;
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
void setup() {
 pinMode(buttonPin, INPUT);
 digitalWrite(buttonPin, LOW);
 Serial.begin(9600);
 Serial.println("Initializing program...");
 pwm.begin();
 pwm.setPWMFreq(60);
 yield();
#define SERVOMINÂ 150
#define SERVOMAXÂ 455
 pinMode(button1, INPUT);
 pinMode(button2, INPUT);
 pinMode(button3, INPUT);
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 lcd.begin(16, 2);
}
void setServoPulse(uint8_t n, double pulse) {
 double pulselength;
 pulselength = 1000000; // 1,000,000 us per second
 pulselength /= 60; // 60 Hz
 Serial.print(pulselength); Serial.println(" us per period");
 pulselength /= 4096; // 12 bits of resolution
 Serial.print(pulselength); Serial.println(" us per bit");
 pulse *= 1000;
 pulse /= pulselength;
 Serial.println(pulse);
 pwm.setPWM(n, 0, pulse);
}
void loop()
{
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 lcd.begin(16, 2);
 {
  if (digitalRead(buttonPin) == LOW)
  {
   buttonPresses++;
   delay(250);
  }
  if (buttonPresses == 7)buttonPresses = 0;
  if (lastPressCount != buttonPresses)
  {
   Serial.print("Press counter= ");
   Serial.println(buttonPresses, DEC);
   {
    if (buttonPresses == 1)
     lcd.print("Base rotation");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(15, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(15, 0, SERVOMAX);
     }
    }
    if (buttonPresses == 2)
     lcd.print("Control JOINT 1");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(14, 0, SERVOMIN);
      pwm.setPWM(13, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(14, 0, SERVOMAX);
      pwm.setPWM(13, 0, SERVOMAX);
     }
    }
    if (buttonPresses == 3)
     lcd.print("Control JOINT 2");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(11, 0, SERVOMIN);
      pwm.setPWM(10, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(11, 0, SERVOMAX);
      pwm.setPWM(10, 0, SERVOMAX);
     }
    }
    if (buttonPresses == 4)
     lcd.print("Control JOINT 3");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(7, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(7, 0, SERVOMAX);
     }
    }
    if (buttonPresses == 5)
     lcd.print("Gripper rotation");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(5, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(5, 0, SERVOMAX);
     }
    }
    if (buttonPresses == 6)
     lcd.print("Gripper control");
    {
     if (digitalRead(button2) == LOW)
     {
      pwm.setPWM(3, 0, SERVOMIN);
     }
     if (digitalRead(button3) == LOW)
     {
      pwm.setPWM(3, 0, SERVOMAX);
     }
    }
    lastPressCount = buttonPresses;
   }
  }
 }
}
The general assumptions are:
- Use [button1] to select arranged motors, and [button2] and [button3] to control the position
- Control position up/down only when button is pressed (not min->max or max->min)
- Display current position on lcd (i had no idea how do this, so its not included anywhere in the code)
I hope that someone will point out my mistakes and/or correct the code, because im out of ideas right now.