Go Down

Topic: Servo's triggered by TTL? (Read 1 time) previous topic - next topic

_Franky_

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

_Franky_

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 :)

-Frank

Code: [Select]
int analogPin = 0;

int val = 0;

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

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

retrolefty

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.  ;)

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

_Franky_

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

-Frank

I've wrote a working code now!

Code: [Select]
#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);
 }
}

mem

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.

_Franky_

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

Code: [Select]

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

}

mem



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



Go Up