Servo Acting weird :S

void PROBLEM!!(){

okay so Im building a simple line follower that has a servo ( the little - HXT900) that lifts up the line sensors so they are out of the way... for later additions...

I know that my servo is working because I tried a program that only had the servo in it and it was fine.

BUT, as soon as I add it into my line follower program it doesnt act normal. All I want it to do right now is go to 150 degrees and stay there while running my line follower code...

Any help would be great!! }

heres my program:

#include 
Servo myServo;

// DIGITAL INPUTS
int In_RA = 4;
int Out_RA = 2;
int In_LA = 12;
int Out_LA = 13;

//  OUTPUTS
int R_Fwd = 9; 
int R_Rev = 10;
int L_Fwd = 5;
int L_Rev = 6;

void setup() {
  
  pinMode(R_Fwd, OUTPUT);
  pinMode(R_Rev, OUTPUT);
  pinMode(L_Fwd, OUTPUT);
  pinMode(L_Rev, OUTPUT);
  
  pinMode(In_RA, INPUT);
  pinMode(Out_RA, INPUT);
  pinMode(In_LA, INPUT);
  pinMode(Out_LA, INPUT);
  
   myServo.attach(11);
}

  void loop(){
    
    followLine();
    myServo.write(150);
  }
  
  
  void followLine(){
        
    
    int In_LV = digitalRead(In_LA);
    int Out_LV = digitalRead(Out_LA);
    
    int In_RV = digitalRead(In_RA);
    int Out_RV = digitalRead(Out_RA);
    
   
      if (In_LV == LOW && In_RV == LOW){
    analogWrite(L_Fwd, 255);
    analogWrite(R_Fwd, 255);
    analogWrite(L_Rev, 0);
    analogWrite(R_Rev, 0);

  }
  
  else if (In_LV == LOW && In_RV == HIGH){
    analogWrite(L_Fwd, 150);
    analogWrite(R_Fwd, 255);
    analogWrite(L_Rev, 0);
    analogWrite(R_Rev, 0);
  }
  
  else if (In_RV == LOW && In_LV == HIGH){
    analogWrite(L_Fwd, 255);
    analogWrite(R_Fwd, 150);
    analogWrite(L_Rev, 0);
    analogWrite(R_Rev, 0);
  }
  
     else if (Out_LV == LOW){
    analogWrite(L_Fwd, 0);
    analogWrite(R_Rev, 0);
    analogWrite(L_Rev, 255);
    analogWrite(R_Fwd, 255);
  }
    
     else if (Out_RV == LOW){
    analogWrite(R_Fwd, 0);
    analogWrite(L_Rev, 0);
    analogWrite(R_Rev, 255);
    analogWrite(L_Fwd, 255);
  }
        
}

analogWrite on pins 9 and 10 and the Servo-library don't mix. They both use Timer1 und the Uno or Duemilanove and you have no way of changing that. Back to the drawing board.

Korman

So if I changed the pins that I analogWrite to (other than 9 and 10) can I use my servo on either of pins 9 and 10?

Yes, the servos can be attached to any port.

Korman

Thanks! Thats an easy fix :)