Go Down

Topic: Problem with sensors and servos (Read 361 times) previous topic - next topic

bizonpl18

hey

I make a line tracking car. I use two continuous servos and 5 line tracking sensors. My big problem is that the servos dont work proparly with my sensors. My sensors detect the line but the servos still keep running forward...

Code: [Select]
#include <Servo.h>

Servo leftServo; //creats left servo object
Servo rightServo;
int s1Left = 2;
int s2Left = 4;
int s3Center = 7;
int s4Right = 8;
int s5Right = 12;
void setup() {
leftServo.attach(9);
rightServo.attach(10);  
               Serial.begin(9600);

       pinMode (s1Left,INPUT);// Lezen van de waardes
       pinMode (s2Left, INPUT);
       pinMode (s3Center, INPUT);
       pinMode (s4Right, INPUT);  
pinMode (s5Right, INPUT);
}
void loop() {
       if ((digitalRead(s1Left) == LOW) && (digitalRead(s2Left) == LOW) && (digitalRead(s3Center) == HIGH) && (digitalRead(s4Right) == LOW) && (digitalRead(s5Right) == LOW))  { //rechts hard  
leftServo.write(90);
rightServo.write(90);
                        delay(9600);
}

}

zoomkat

Servo test code you can use to find the actual servo value that stops the servos if that is the issue.

Code: [Select]

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

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

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

PeterH

I see code trying to stop the servos, but I don't spot any code to start them. Have you tested the servos to see what values you need to set them to for forwards, stop, reverse?

Once you know how to drive the servos, I suggest you add Serial.println() to tell you whether those sensor conditions are actually being met.

I only provide help via the forum - please do not contact me for private consultancy.

bizonpl18

ok thanks i have seen my problem, i  had a potentiometer on my servo ;/

zoomkat

To calibrate a continous rotation servo that still has a pot, send a 1500us position to the servo and then tweek the pot until the servo stops rotating.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

bizonpl18

Thank you. I have done it and its working :)

Go Up