Pages: [1]   Go Down
Author Topic: continuous rotation servo won't stop  (Read 934 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everyone,
I am trying to make a line following robot. I use continuous rotation servos, the problem is that the servos won't stop turning once started.
This is because the code doesn't check the sensor output after the first time.

Thanks a lot!

I used this sensor: http://dx.com/p/arduino-line-hunting-sensor-module-118057
and these servos: http://www.ebay.com/itm/clear-up-Spring-RC-SM-S4306R-360-Degree-Continuous-Rotation-Robot-Servo-SMS4306R-/350696583715?pt=US_Character_Radio_Control_Toys&hash=item51a7253223

Code:
#include <Servo.h>
  int sensorpin = 3;
  int speed0 = 91;
  int speed1 = 61;
  int speed2 = 111;  
  int speed3 = 71;
 
Servo leftwheel;
Servo rightwheel;

void setup()
{
leftwheel.attach(8);
rightwheel.attach(9);
}

void loop()
{
  while(sensorpin == HIGH){
  leftwheel.write(speed1);
  rightwheel.write(speed2);
}

  while(sensorpin != HIGH){
  leftwheel.write(speed3);
  rightwheel.write(speed3);
  }
}

I already tried: continue, goto, return, break, while, if, if else, swith case


* linefollowerpaint.png (17.19 KB, 586x576 - viewed 14 times.)
« Last Edit: January 26, 2013, 08:13:55 am by giedow » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The usual advice is to use the writeMicroseconds method on the Servo objects to find the stop point, but it may drift with load or battery state.
« Last Edit: January 26, 2013, 08:03:11 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

How would you write that in the code?

And thank you!
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4255
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where did you get these numbers ?

Code:
  int speed0 = 91;
  int speed1 = 61;
  int speed2 = 111; 
  int speed3 = 71;
 

Your comments say that the servos will be stopped with the speed value set to speed1, speed2 or speed3.  It is very unlikely that they are all true bearing in mind the range of values this implies (61, 111 and 71)
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/ServoWriteMicroseconds
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  int sensorpin = 3;
...
  while(sensorpin == HIGH){

sensorpin is the pin number, to get its current value you need to use something like
if ( digitalRead(sensorpin)==HIGH)

http://arduino.cc/en/Reference/digitalRead
http://arduino.cc/en/Tutorial/Button

Cheers,
John
Logged

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

 I got these numbers just by trying without the sensor, my mistake(I copy/pasted that and forgot to delete the text)
Logged

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

Thanks AWOL and johncc

I got it to work

Code:
#include <Servo.h>
  int sensorpin = 3;
  int speed0 = 91;
  int speed1 = 61;
  int speed2 = 111; 
  int speed3 = 71;
 
Servo leftwheel;
Servo rightwheel;

void setup()
{
    pinMode(3,INPUT);
    leftwheel.attach(8);
rightwheel.attach(9);
leftwheel.writeMicroseconds(50);
rightwheel.writeMicroseconds(50);
}

void loop()
{
  if ( digitalRead(sensorpin)!=HIGH){
  leftwheel.write(speed1);
  rightwheel.write(speed2);
 
 
}

  else if ( digitalRead(sensorpin)==HIGH){
  leftwheel.write(speed3);
  rightwheel.write(speed3);
 
  }
}
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's great!  Fiddle and learn!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Considering that digitalRead() can only return HIGH or LOW, if the value is not HIGH, is there any possible way that the value can be anything other than LOW?

If you come to the correct conclusion that there is not, then explain why you need to test for HIGH.

The if/else if(/else) construct is used to test multiple conditions when there could be more than two. The if/else construct is preferred when there are only 2 possible conditions (as in the case of digitalRead).
Logged

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

hadn't thought about that
Logged

Pages: [1]   Go Up
Jump to: