infrared servo control

I am trying to control a servo with with two 555 timer hooked up to an infrared emitter. I was able to control a shift register using this method but for some reason I when I try and use a servo all it does is shakes a little and doesnt turn. Here is my code:

volatile long lasttime = 0;

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 10;
////Pin connected to DS of 74HC595
int dataPin = 9;

#include <Servo.h>

Servo myservo; //create servo object to control a servo

int val; //variable to read the value from the analog pin

void setup()
{
// Go with faster than normal serial speed
Serial.begin(115200);

// Set Interrupt 0 (which is on digital pin 2) to call 'onTick'
// when the signal rises.
pinMode(2, INPUT);
attachInterrupt( 0, onTick, RISING );

myservo.attach(11); //attach the servo on pin 11 to the servo object

}

void loop()
{
// Note that we're doing anything in the main loop,
// everything happens in onTick

}

void onTick()
{
// print out how many milliseconds occurred between the last
// clock tick and this one.
long thistime=millis();
long time=thistime-lasttime;
Serial.println(time);
val = map(time, 0, 120, 0, 179); //scale it to use with servo

myservo.write(val);

delay(15);

lasttime = thistime;

}

Where's the servo write?

I posted it from a note pad and accidentally deleted it. I edited it to include the my servo write

Would it have something to do witht the data type millis() returns? can I not map that ? I think the delay might be changing the time returned as well. Can someone help me?

Why have you got a delay in an ISR?

revised code.... still doesnt work. ackk!

volatile long lasttime = 0;
long time = 0;
long poo = 0;

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 10;
////Pin connected to DS of 74HC595
int dataPin = 9;

#include <Servo.h>
#include <stdio.h>
#include <stdlib.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 5, 4, 3, 6);

Servo myservo; //create servo object to control a servo

int potpin = 0; //analog pin used to connect the potentiometer
int val; //variable to read the value from the analog pin

void setup()
{
// Go with faster than normal serial speed
Serial.begin(115200);

// Set Interrupt 0 (which is on digital pin 2) to call 'onTick'
// when the signal rises.
pinMode(2, INPUT);
attachInterrupt( 0, onTick, RISING );

myservo.attach(11); //attach the servo on pin 9 to the servo object

}

void loop()
{
poo = time;

// set up the LCD's number of rows and columns:
lcd.begin(16, 2);

char temp[12];
itoa((long)poo,temp,10);
lcd.print(poo);

val = map(poo, 0, 120, 0, 179); //scale it to use with servo
myservo.write(val);
delay(15);
Serial.println(poo);

// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
// lcd.print("fuck yea!");

}

void onTick()
{
// print out how many milliseconds occurred between the last
// clock tick and this one.
long thistime=millis();
time=thistime-lasttime;
// Serial.println(time);

lasttime = thistime;

}

You are moving the servo to a position defined by the length of time between interrupts, right?

What is generating the interrupt? You mention a 555 timer. I presume that that is generating an interrupt on a regular basis. Is that right?

If it is, then the servo should move once or twice. The first movement occurs when the first interrupt occurs. There is no previous value to measure relative to, so the servo might move anywhere, or not at all.

The next interrupt will have some time recorded to measure relative to. time will be set to the time between the interrupts, in milliseconds.

You map this value from the range 0 to 120 to the range 0 to 179. Is the time between interrupts in the range 0 to 120 milliseconds? Is the time relatively constant?

If the time is constant, I would not expect the servo to move more than once or twice. If the time is not in the range 0 to 120 milliseconds, I'd expect the servo to have difficulties.

Thanks for the help... got it working tonight. The problem was the servo power was screwing up the values read by the infrared detector. As soon as I connected a different power source the the servo everything worked fine :slight_smile: I will post a video soon.