Program for Micro Servo Robot Arm had declaration problems

Hello,

I have been working for over a week to download a program to operate the Micro Servo Robot Arm.

I have attached the program I cut and pasted into the Arduino IDE from this web site below. The program attached is (sketch_26a) it was copied from

http://s-rtronics.blogspot.com/p/micro-servo-robotic-arm-using-10k-pot.html

I have also attached the program I tried to format because the program (sketch_26a) had some // in places that blocked the code. At least that's what I believe.

The program I tried to improve is (sketch_25R1), it is also attached. I still get declaration problems.

Any help with the program would be greatly apprecitated, I've been trying for weeks to get this program running, I've found programs on several websites for the same Micro Servo Robot Arm and none have worked.

I am trying to learn the code, but I'm not good at all, if someone sees the problems and could make the changes that would be greatly appreciated. I'm a mechanical engineer. I am 3d printing the parts but need to get the program running.

Thanks so much!

Dan Hanson

sketch_dec26a.ino (3.92 KB)

sketch_dec25aR1.ino (4.29 KB)

int PosCount = 0; // to count number of positions increased when button pressed
int PosCountMax = 0; // number of positions recorded when double button push initiates replay mode int PosReached = 0; // used to check that arm has moved from one recorded position to next position
 void setup()
 {
 for(int i = 0; i <100 ; i++ )
{
 for(int p = 0; p <4 ; p++ )
{
 ArmPos[i][p] = -1; 
}
 }
 pinMode(PinButton1 , OUTPUT); 
digitalWrite(PinButton1 ,LOW); 
pinMode(PinButton2, INPUT_PULLUP);
 // I have made a small change here due to problem using some Arduinos //attachInterrupt( digitalPinToInterrupt(PinButton2),ButtonPress , LOW );
 // digitalPinToInterrupt(PinButton2) may not be defined! attachInterrupt( 1,ButtonPress , LOW ); 
// interupt to capture button presses 
// attach servos to relervent pins on arduino nano Arm0Servo.attach(12); // grip 90 to 180 open limits of servo movement Arm1Servo.attach(11);
 // elbow to 130 up Arm2Servo.attach(10);
 // shoulder 10 to 50 down Arm3Servo.attach(9); 
// turn 0 to 180 right
 }
void loop()
 {
 switch(mode)
{ 
case 1 : 
// program robot arm. 1 press to remember position. 2 presses to progress next case 2 replay mode 
// analogRead(pin) that reads poteniometers on training arm PosArm[0] = map(analogRead(14),480,1024,180,90); 
// map (480,1024 value from potentiometer to 180,90 value sent to servo) PosArm[1] = map(analogRead(15),180,1000,130,10);
 PosArm[2] = map(analogRead(16),950,400,80,10);
 PosArm[3] = map(analogRead(17),0,1024,180,10); MoveArm(); 
// call method if(buttonPress == 1)
{
 // flag set by interupt when button is pressed buttonPress = 0;
 // reset flag if( millis() > (lastButtonPressTime + 1000))
{
 // only one button press in one secound
 // record position of arm PosArm to array[100][] of armpositions ArmPos[PosCount][0] = PosArm[0]; ArmPos[PosCount][1] = PosArm[1]; ArmPos[PosCount][2] = PosArm[2]; ArmPos[PosCount][3] = PosArm[3]; if( PosCount < 100)
 {
 // stop recording if over 100 positions recorded (memory limitations) PosCount++;
 }
 }
else
}
 // more than one button press mode = 2; 
// go to next phase PosCountMax = PosCount;
 // set number of arm positions recorded PosCount = 0; 
// reset count ready to read arm position array from begining 
}
 lastButtonPressTime = millis();
 } 
break;
 case 2 :
 // read arm position array PosReached = 0; 
for(int i = 0; i <4 ; i++ )
{ 
//adjust servo positions
 // we move the servos in small steps and the delay(20) makes arm motion smooth and slow 
if( PosArm[i] > ArmPos[PosCount][i]) PosArm[i] = PosArm[i] - 1; 
// if actual position is greater than requird position reduce position by 1
 if( PosArm[i] < ArmPos[PosCount][i]) PosArm[i] = PosArm[i] + 1;
 // if actual position is less than required position value increase position valuue by 1 
if( PosArm[i] == ArmPos[PosCount][i]) PosReached++; 
// check if servo has reached required position/angle
 }
 if(PosReached == 4) PosCount++;
 // if all 4 servos have reached position then increase array index (PosCount)to next position in array
 if(PosCount == PosCountMax) PosCount = 0;
 // if end of array reached reset index to 0 and repeat. MoveArm(); 
// physically move arm to updated position, this is broken into small steps delay(20);
 // pause between moves so over all motion is slowed down break;
 default : break; 
}
 }
void MoveArm()
 { 
// write arm position data to servos for (int i = 0 ; i < 4 ; i++)
 {
 if (PosArm[i] < 5) PosArm[i] = 5;
 // limit servo movement to prevent hitting end stops if (PosArm[i] > 175) PosArm[i] = 175;
 // servo.write limited 5 - 175 
}
 Arm0Servo.write(PosArm[0]);
 Arm1Servo.write(PosArm[1]); 
Arm2Servo.write(PosArm[2]);
 Arm3Servo.write(PosArm[3]); 
} 
void ButtonPress()
{ 
// interupt to capture button press if(micros() > (bounceTime + 3000))
{
 // debounce timer bounceTime = micros();
 // ingnore interupts due to button bounce buttonPress = 1;
 // flag for button pressed
 }
 }
int PosCount = 0; // to count number of positions increased when button pressed
int PosCountMax = 0; // number of positions recorded when double button push initiates replay mode 
int PosReached = 0; // used to check that arm has moved from one recorded position to next position




void setup()
{
  for (int i = 0; i < 100 ; i++ )
  {
    for (int p = 0; p < 4 ; p++ )
    {
      ArmPos[i][p] = -1;
    }
  }
  pinMode(PinButton1 , OUTPUT);
  digitalWrite(PinButton1 , LOW);
  pinMode(PinButton2, INPUT_PULLUP); // I have made a small change here due to problem using some Arduinos //
  attachInterrupt( digitalPinToInterrupt(PinButton2), ButtonPress , LOW ); // digitalPinToInterrupt(PinButton2) may not be defined!
  attachInterrupt( 1, ButtonPress , LOW ); // interupt to capture button presses
  // attach servos to relervent pins on arduino nano
  Arm0Servo.attach(12); // grip 90 to 180 open limits of servo movement
  Arm1Servo.attach(11);  // elbow to 130 up
  Arm2Servo.attach(10);  // shoulder 10 to 50 down
  Arm3Servo.attach(9);  // turn 0 to 180 right
}
void loop()
{
  switch (mode);
  {
    case 1 :
      // program robot arm. 1 press to remember position. 2 presses to progress next case 2 replay mode
      // analogRead(pin) that reads poteniometers on training arm
      PosArm[0] = map(analogRead(14), 480, 1024, 180, 90); // map (480,1024 value from potentiometer to 180,90 value sent to servo)
      PosArm[1] = map(analogRead(15), 180, 1000, 130, 10);
      PosArm[2] = map(analogRead(16), 950, 400, 80, 10);
      PosArm[3] = map(analogRead(17), 0, 1024, 180, 10); 
      
      MoveArm();

      // call method if(buttonPress == 1)
      {
        // flag set by interupt when button is pressed
        buttonPress = 0; // reset flag

        if ( millis() > (lastButtonPressTime + 1000))
        {
          // only one button press in one secound
          // record position of arm PosArm to array[100][] of armpositions 
          ArmPos[PosCount][0] = PosArm[0]; 
          ArmPos[PosCount][1] = PosArm[1]; 
          ArmPos[PosCount][2] = PosArm[2]; 
          ArmPos[PosCount][3] = PosArm[3]; 
          if( PosCount < 100)
          {
            // stop recording if over 100 positions recorded (memory limitations) 
            PosCount++;
          }
        }
        else
        }
      // more than one button press
      mode = 2;  // go to next phase
      PosCountMax = PosCount; // set number of arm positions recorded
      PosCount = 0; // reset count ready to read arm position array from begining
  }
  lastButtonPressTime = millis();
}
break;
case 2 :

// read arm position array

PosReached = 0;
for (int i = 0; i < 4 ; i++ )
{
  //adjust servo positions
  // we move the servos in small steps and the delay(20) makes arm motion smooth and slow
  if ( PosArm[i] > ArmPos[PosCount][i]) PosArm[i] = PosArm[i] - 1;  // if actual position is greater than requird position reduce position by 1
  if ( PosArm[i] < ArmPos[PosCount][i]) PosArm[i] = PosArm[i] + 1;   // if actual position is less than required position value increase position valuue by 1
  if ( PosArm[i] == ArmPos[PosCount][i]) PosReached++;  // check if servo has reached required position/angle
}
if (PosReached == 4) PosCount++; // if all 4 servos have reached position then increase array index (PosCount)to next position in array

if (PosCount == PosCountMax) PosCount = 0; // if end of array reached reset index to 0 and repeat.

MoveArm(); // physically move arm to updated position, this is broken into small steps 
delay(20);
// pause between moves so over all motion is slowed down break;

default : break;
}
}
void MoveArm()
{
  // write arm position data to servos for (int i = 0 ; i < 4 ; i++)
  {
    if (PosArm[i] < 5) PosArm[i] = 5;
    // limit servo movement to prevent hitting end stops

    if (PosArm[i] > 175) PosArm[i] = 175; // servo.write limited 5 - 175
  }
  Arm0Servo.write(PosArm[0]);
  Arm1Servo.write(PosArm[1]);
  Arm2Servo.write(PosArm[2]);
  Arm3Servo.write(PosArm[3]);
}
void ButtonPress()
{
  // interupt to capture button press
  if (micros() > (bounceTime + 3000))
  {
    // debounce timer
    bounceTime = micros();
    // ingnore interupts due to button bounce
    buttonPress = 1;
    // flag for button pressed
  }
}

Well, there's the question "What is "ArmPos" and "PosArm" ?"

And then there's "Have you got enough RAM for this?"

for (int i = 0; i < 100 ; i++ )
  {
    for (int p = 0; p < 4 ; p++ )
    {
      ArmPos[i][p] = -1;
    }
  }

The compiler is quite right

ArmPos is not declared in the program, mind you neither is the confusingly named PosArm

It looks like the sketch was badly mangled before being posted. A number of declarations are missing and some lines have been appended to the end of comments so they no longer work. It might be easier to write a sketch from scratch.

Look at the example: File->Examples->Servo->Knob. It shows how to read a value from a potentiometer and use that value to set the position of a servo. Repeat that code for four potentiometers and four servos. After you get that working you can add more code to record a series of positions and to play them back.

Thanks for the replies.

In the second file (sketch_25aR1) I attached I tried to correct the lines that ran together in the program from posting.
.

Could someone tell me how to make declarations, I've tried it several times but with no sucess.

I've watched videos on declaring variables but when I try it I get another error message.

Besides the run together lines, I'm wondering if there is some bracket missing or something small that is causing most of the errors.

If someone could show me how to declare these variables I would be very thankful.

I wish I had the talent to start from the beginning, but I don't.

Thanks so much for the replies.

Dan Hanson PhD PE

Danielhhanson:
I wish I had the talent to start from the beginning, but I don't.

You don't have to start from the beginning. The Servo library comes with two examples: Knob and Sweep.
There is also documentation for libraries:

And documentation for the examples:
https://www.arduino.cc/en/Tutorial/HomePage
https://www.arduino.cc/en/Tutorial/LibraryExamples#servo