Pin not declared

I am trying to control three servos with three buttons. I keep getting an error on my code concering my right pin, and I have not been able to troubleshoot the issue.

Here is the code:

#include <Servo.h>
Servo myservo; // create servo object to control a servo

Servo myservo2;

Servo myservo3;

int pos = 85; // variable to store the servo’s starting position

int pos2 = 85;

int pos3 = 85;

const int maxDeg = 165; // limits the maximum range of the servo’s movement to 165

const int minDeg = 5; // limits the minimum range of the servo’s movement to 5

const int movement = 2; // the amount to move the servo when the button is pushed. Value can be changed.

const int leftPin = 5; // tells the Arduino the location of the signal cable from the switch

const int middlePin = 6;

const int rightpin = 7;

const int outputPin = 8; // tells the Arduino the location of the signal cable to the servo

const int outputPin2 = 9;

const int outputPin3 = 10;

int leftPressed = 0; // variables we will use to keep information about the switch states

int middlePressed = 0;

int rightPressed = 0;

void setup()
{

myservo.attach(outputPin); // left motor

myservo2.attach(outputPin2);

myservo3.attach(outputPin3);

// This code tells the Arduino to set the states of the pins to input mode

pinMode(leftPin, INPUT);

pinMode(middlePin, INPUT);

pinMode(rightPin, INPUT);

}

void loop()
{

leftPressed = digitalRead(leftPin);

middlePressed = digitalRead(middlePin);

rightPressed = digitalRead(rightPin);

if(leftPressed) {

if(pos < maxDeg)

pos += movement;

myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

}

if(middlePressed) {

if(pos < maxDeg)

pos += movement;

myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

}

if(rightPressed) {

if(pos < maxDeg)

pos += movement;

myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

}

}

attempt_with_3.ino (1.9 KB)

rightpin != rightPin

Please remember to use code tags when posting code

Also, suggest you always use this format (even when there is only one line to execute):

if(........)
{
some stuff
}

Your almost there.

Use CTRL T to format your code.
Attach your sketch between code tags
[code]Paste your sketch here[/code]

#include <Servo.h>
Servo myservo; // create servo object to control a servo

Servo myservo2;

Servo myservo3;

int pos = 85; // variable to store the servo's starting position

int pos2 = 85;

int pos3 = 85;

const int maxDeg = 165; // limits the maximum range of the servo's movement to 165

const int minDeg = 5; // limits the minimum range of the servo's movement to 5

const int movement = 2; // the amount to move the servo when the button is pushed. Value can be changed.

const int leftPin = 5; // tells the Arduino the location of the signal cable from the switch

const int middlePin = 6;

const int rightpin = 7;

const int outputPin = 8; // tells the Arduino the location of the signal cable to the servo

const int outputPin2 = 9;

const int outputPin3 = 10;

int leftPressed = 0; // variables we will use to keep information about the switch states

int middlePressed = 0;

int rightPressed = 0;

void setup() 
{

myservo.attach(outputPin); // left motor

myservo2.attach(outputPin2);

myservo3.attach(outputPin3);

// This code tells the Arduino to set the states of the pins to input mode

pinMode(leftPin, INPUT);

pinMode(middlePin, INPUT);

pinMode(rightPin, INPUT);

}

void loop() 
{

leftPressed = digitalRead(leftPin);

middlePressed = digitalRead(middlePin);

rightPressed = digitalRead(rightPin);

  if(leftPressed)
  {

    if(pos < maxDeg)

    pos += movement;

    myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

    }

     if(middlePressed)
     {

    if(pos < maxDeg)

    pos += movement;

    myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

    }

     if(rightPressed)
     {

    if(pos < maxDeg)

    pos += movement;

    myservo.write(pos); // tells the servo to go to the position stored in the variable ‘pos’

    }

}

But you have not corrected:
const int rightpin = 7;

Did you want:

  if(leftPressed)
  {
    if(pos < maxDeg)
    pos += movement;
    myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos'
  }

Or did you want:

if(leftPressed)
{
    if(pos < maxDeg)
    {
    pos += movement;
    myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos'
    }
}

From your second option, does this just isolate the input per pin?

I thought that pushing the bracket further would alter the void loop.

I fixed the code with "rightPin".

  if(leftPressed)
  {
    if(pos < maxDeg)
    pos += movement;
    myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos'
  }

Above is the same as:

if(leftPressed)
  {
    if(pos < maxDeg)
    {
    pos += movement;
    }

myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos'
  }

which may or may not be what you want. :wink:

Which is different than:

if(leftPressed)
{
    if(pos < maxDeg)
    {
    pos += movement;
    myservo.write(pos); // tells the servo to go to the position stored in the variable 'pos'
    }
}