Pages: [1]   Go Down
Author Topic: Problem with sensors and servos  (Read 346 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}

}
Logged

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9753
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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
  }
}

Logged

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 148
Posts: 9753
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. I have done it and its working smiley
Logged

Pages: [1]   Go Up
Jump to: