Pages: [1]   Go Down
Author Topic: HiTec Dig. Servo - Example works, My code doesn't  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I tried the example sweep and my servo works great, I tried my own example and it doesn't.  I am assuming it is related to the timing.  I have a sensor that when it goes low, I want the servo to swap where it was (180 to 0, 0 to 180).  The LCD shows the updated sense count, so I know the sensor is working.  The example sweep works, so I know the servo is working.  Where am I going wrong?

Code:
#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(22, 23, 31, 32, 33, 34);
Servo myservo;

int inPin = 43;
int outPin = 13;

int reading;                // the current reading from the input pin/sensor
int sense_count=0;     // sensor count
int servo_val=0;         // servo value

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  
  lcd.begin(20, 4);
  lcd.clear();
  lcd.print("Servo Test v0.1");

  myservo.attach(52);
  myservo.write(0);
  delay(15);
}

void loop()
{
  reading = digitalRead(inPin);
  
  lcd.setCursor(14, 3);
  
  if (reading == LOW) {
    sense_count++;
    lcd.print(sense_count);
    delay(15);
    
    if (servo_val == 0) {
      servo_val = 180;
    } else {
      servo_val = 0;
    }
    
    myservo.write(servo_val);
    delay (15);
  }
  
}

I am aware that the default min/max for the servo in the Servo.h library are not the HiTec recommended values of 900 and 2100.  But, when I try to pass those initially in myservo.attach(52, 900, 2100), my servo goes crazy.
Logged

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

15ms for a 180 degree sweep?
That's a fast servo.

Quote
my servo works great, I tried my own example and it doesn't
But what does it do?

Your idea of "works" isn't clear.
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.

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

With the Example code for Sweep, it works.
With this code, the servo doesn't move a bit.

Is 15ms per a degree the actual calculation?  So that is 2700ms for a 180 degree swap?

Thanks.
Logged

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

15ms is the time you're allowing the servo to travel 180 degrees, before you're reading the input pin again.
There is no "calculation".
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.

Pages: [1]   Go Up
Jump to: