Servo's triggered by TTL?

Hello all!

I have a setup (not arduino) that powers a servo and through a TTL signal, tells the servo to move from one position to another. The TTL works on the servo like an "on and off" switch, going back from position A to position B viceversa. I was wondering if in the Arduino programming language there was a way that would do something similar or exactly the same.

-Frank

Hello all!

I've made some progress and this might clear things up a bit from what I have originally said. I've wrote a code that I saw on one of the tutorials. I've written the code posted below and the serial monitor displays a lot of numbers (scrolling very fast). I have a switch that like said in the post above, acts like an off and on switch. So the voltage from the wires go from 5v to 0v and then zeros begin to scroll down the serial monitor. Is it possible I can link this to say servo control? Suggestions and hints are always welcome :slight_smile:

-Frank

int analogPin = 0;

int val = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  val = analogRead(analogPin);
  Serial.println(val);
}

What you wish to do is certainly very simple to do with an arduino. There is a servo library that has all the software commands needed to control a servo, and reading the status of a switch wired to a arduino digial input pins is very simple. However you should really learn how to write programs starting with the basics, rather then just have someone give you a working sketch just for this one project.

It's a teach a person to fish kind of thing. :wink:

Look over the two servo example sketches in the the arduino IDE's File/Example/Servo menu and see if you can make sense of them.

Lefty

I'm starting to understand coding, thanks for the tips Lefty :slight_smile:

-Frank

I've wrote a working code now!

#include <Servo.h>

int analogPin = 0;
int val = 0;
int pos;
Servo myservo;

void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
}

void loop()
{
  val = analogRead(analogPin);
  Serial.println(val);
  
  if(val != 0)
  { 
     myservo.writeMicroseconds(1000);
  }
  else
  { 
  myservo.writeMicroseconds(1500);
  }
}

You should add a delay in your loop function so that the servo library is not constantly disturbing the interrupt handler to write values. A delay of 15ms is customary, see the servo knob and sweep example sketches.

Hey Mem,

So like this? I don't see too much of a difference. When the servo library disturbs the interrupt handlers, what happens?

-Frank

#include <Servo.h>

int analogPin = 0;
int val = 0;
int pos;
Servo myservo;

void setup()
{
  myservo.attach(9);
  Serial.begin(9600);
}

void loop()
{
  val = analogRead(analogPin);
  Serial.println(val);
  
  if(val != 0)
  { 
     myservo.writeMicroseconds(950);
  }
  else
  { 
     myservo.writeMicroseconds(1600);
  }

  delay(15);

}

Franky , yes thats it.

When the servo library disturbs the interrupt handlers, what happens?
While Servo is writing its data, any other interrupt handlers must wait until the write is finished. The write only takes a microsecond but without some delay the sketch is spending most of its time disabling and enabling interrupts for no benefit. The Servo values are sent to the servos every 20ms so there is no point in updating more quickly than that.

Awesome!

Thanks Mem!