Pages: [1]   Go Down
Author Topic: Servo's triggered by TTL?  (Read 1135 times)
0 Members and 1 Guest are viewing this topic.
Florida!
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Florida!
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

-Frank

Code:
int analogPin = 0;

int val = 0;

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

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Florida!
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-Frank

I've wrote a working code now!

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

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Florida!
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

}
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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

Logged

Florida!
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome!

Thanks Mem!

Logged

Pages: [1]   Go Up
Jump to: