Go Down

Topic: Combine codes for servo project (Read 388 times) previous topic - next topic

Jakes

Good day.

I have to codes.
1st i needed 2 servos working togeher running in diffrant directions. Got that to work running in loop. The basic to what i want it to do. The code is as follow.


#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;        // Define right servo

int posLeft;            //angle of left servo
int posRight;           //angle of right servo

//opposite side servos rotate together

void setup() {
  servoLeft.attach(8);  // Set left servo to digital pin 8
  servoRight.attach(9);  // Set right servo to digital pin 9

}

void loop() {     // Loop throught motion test
  foward();       // Example: move foward
  delay(2000);     // Wait 1000 milliseconds (1 seconds)
  reverse();
  delay(2000);
}
//Motion routines for foward and reverse
void foward() {
  servoLeft.write(10);
  servoRight.write(170);
}

void reverse() {
  servoLeft.write(170);
  servoRight.write(10);
}


This works great.
Next i wanted to control with i switch. Got code from one of the topics and that works great.

#include <Servo.h>
const byte switchPinA = 2; //active low
const byte switchPinB = 3; //active low
const byte servoPin = 9;
Servo servo;
byte switchAstate = HIGH;
byte switchBstate = LOW;
byte servoPos = 20;

void setup() {
  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);
  servo.attach(servoPin);
}

void loop () {
   readButtons();
   moveServo();
}

void readButtons() {
   switchAstate = digitalRead(switchPinA);
   switchBstate = digitalRead(switchPinB);
}

void moveServo() {
   if (switchAstate == LOW) {
       servoPos = 20;
   }
   if (switchBstate == LOW) {
        servoPos = 110;
   }
   servo.write(servoPos);
   delay(100);
}

Now i want to combine the and have the first code work with the switch idea in second code but with the 2 servos. In pos 1 move only foward and second position move reverse.

i am new to this been trying lost of thing using relay and got to this site bought my kit and love it. I can make my idea work just need help.

Jakes

At last i want to add an led to show the position of servo if foward or reverse. I am strugeling can any one help me wuth the right code.

Jakes

this what i have so far

#include <Servo.h>
const byte switchPinA = 2; //active low
const byte switchPinB = 3; //active low

Servo servoLeft;   // Define left servo
Servo servoRight;  // Define Right servo

int posLeft        // Angle of left servo
int posRight       // Angle of right servo

//opposite side servos rotate together

byte switchAstate = LOW;
byte switchBstate = LOW;

void setup() {
  pinMode(switchPinA, INPUT_PULLUP);
  pinMode(switchPinB, INPUT_PULLUP);

  servoLeft.attach(8);     // Set left servo to pin 8
  servoRight.attach(9);    // Set Right servo to pin 9
}

void loop () {
   readButtons();
   moveservo();
}

void readButtons() {
   switchAstate = digitalRead(switchPinFoward);
   switchBstate = digitalRead(switchPinReverse);
}

void Foward() {
   if (switchAstate == LOW) {
       servoLeft.write(10);
       servoRight.write(170);
   }
void Reverse() {
   if (switchBstate == LOW) {
        servoLeft.write(170);
        servoRight.write(20);
   }
   servo.write(servoPos);
   delay(100);
}

error code

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

switch_2_servo_position_testing:9: error: expected initializer before 'int'

 int posRight       // Angle of right servo

 ^

C:\Users\Jaco Kruger\Documents\Arduino\switch_2_servo_position_testing\switch_2_servo_position_testing.ino: In function 'void loop()':

switch_2_servo_position_testing:26: error: 'moveservo' was not declared in this scope

    moveservo();

              ^

C:\Users\Jaco Kruger\Documents\Arduino\switch_2_servo_position_testing\switch_2_servo_position_testing.ino: In function 'void readButtons()':

switch_2_servo_position_testing:30: error: 'switchAstate' was not declared in this scope

    switchAstate = digitalRead(switchPinFoward);

    ^

switch_2_servo_position_testing:30: error: 'switchPinFoward' was not declared in this scope

    switchAstate = digitalRead(switchPinFoward);

                               ^

switch_2_servo_position_testing:31: error: 'switchPinReverse' was not declared in this scope

    switchBstate = digitalRead(switchPinReverse);

                               ^

C:\Users\Jaco Kruger\Documents\Arduino\switch_2_servo_position_testing\switch_2_servo_position_testing.ino: In function 'void Foward()':

switch_2_servo_position_testing:35: error: 'switchAstate' was not declared in this scope

    if (switchAstate == LOW) {

        ^

switch_2_servo_position_testing:39: error: a function-definition is not allowed here before '{' token

 void Reverse() {

                ^

switch_2_servo_position_testing:46: error: expected '}' at end of input

 }

 ^

exit status 1
expected initializer before 'int'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

slipstick

Please post your code in </> code tags as described in http://forum.arduino.cc/index.php/topic,148850.0.html Point 7. It will be easier to read and code with smiley faces in never looks right.

Make sure you have ; at the end of all statements. - that will fix a few errors.

Then if you want to use a function moveservo() you need to define it somewhere. And you could take out the two functions you've defined but never use unless you're intending to use them in moveservo() in which case you need to sort out the pin names.

Steve

Go Up