Issues with ServoTimer1 Library, final project (please help)

So, to start off, I'd like to say HI!. I'm Laser, long time lurker , new member :).

To get on with it, here is the issues I'm having currently:

I'm trying to use ServoTimer1 Library for my final project, I'm using an Arduino nano with a 328p and arduino 1.6.7. Here is the error I'm getting

Arduino: 1.6.7 (Windows 10), Board: "Arduino Nano, ATmega328"

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\ServoTimer1\ServoTimer1.cpp: In static member function 'static void ServoTimer1::seizeTimer1()':

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\ServoTimer1\ServoTimer1.cpp:45:5: error: 'TIMSK' was not declared in this scope

     TIMSK &= ~(_BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) );

     ^

In file included from c:\program files (x86)\arduino\hardware\tools\avr\avr\include\avr\io.h:99:0,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/wiring_private.h:26,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\ServoTimer1/ServoTimer1.h:25,

                 from C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\ServoTimer1\ServoTimer1.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\ServoTimer1\ServoTimer1.cpp:45:20: error: 'TICIE1' was not declared in this scope

     TIMSK &= ~(_BV(TICIE1) | _BV(OCIE1A) | _BV(OCIE1B) | _BV(TOIE1) );

                    ^

exit status 1
Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

I'm almost done with my final project and this may be the only thing holding me back now. Any help would be greatly appreciated :smiley:

I know there is a ServoTimer2 library but I had not heard of ServoTimer1. Post a link to where you got it.

Also post your program so we can see what you can see. And please use the code button </> so your code looks like this and is easy to copy to a text editor

...R

according to the documentation, that library has been incorporated into the main servo library.

See here:

http://playground.arduino.cc/ComponentLib/Servotimer1

Can't believe I didn't read that..... So, Does that mean that the servo library already uses timer1 or that there is a way to make it use timer1. I think I'm having timing issues but can't seem to figure out why.
I started off using Arduino Playground - PinChangeInt , and the servo library. Then when it seemed sporadic, I switched to hardware interrupts on pin 2 and 3 on the nano. I thought maybe if I switched to the timer1 library it would solve my problem by using timer based PWM.

I am trying to read an SR-04 ultrasonic sensor, read a receiver (100hz at 10-20% duty cycle), and output a servo signal (similar to the receiver signal). I'm not using any delays, only triggering the ultrasonic sensor 10x a second. The ISR's are set to only document the times at which the pulse rises and seperate ones for when they fall (4 ISR in total, 2 for each pin) so that it only takes a split microsecond to execute.

I'm hesitant to show all of the code yet as I'm contemplating selling the end device. Here is the ISR's, which would be the only thing that I would think would mess the program up.

void rising()
{
  prev = micros(); //Sets variable to current relative runtime for later use
  attachInterrupt(digitalPinToInterrupt(2), &falling, FALLING);
}

void falling()
{
  pwm = micros() - prev; // Math to determine length of incoming pulse
  attachInterrupt(digitalPinToInterrupt(2), &rising, RISING); // resets interrupt for next pwm cycle
}

void pingrising() // called on rising edge of ultrasonic pulse
{
  attachInterrupt(digitalPinToInterrupt(3), &pingfalling, FALLING); // sets interrupt for falling edge
  dprev = micros(); //Sets variable to current relative runtime for later use
}

void pingfalling() // called on falling edge of ultrasonic pulse
{
  dpwm = micros() - dprev; // Math to determine length of pulse received by ultrasonic sensor
  distance = microToCent(dpwm); // calling function to convert to centimeters
  attachInterrupt(digitalPinToInterrupt(3), &pingrising, RISING); // reset interupt for next cycle
}

void setup()
{
  pinMode(3, INPUT);
  pinMode(2, INPUT);
  pinMode(6, OUTPUT);// trigger pin for SR-04
  servo.attach(10);
  servo.setMinimumPulse(1000);
  servo.setMaximumPulse(2000);
  servo.(digitalPinToInterrupt(2), &rising, RISING);
  attachInterrupt(digitalPinToInterrupt(3), &pingrising, RISING);
}

laser411:
I am trying to read an SR-04 ultrasonic sensor, read a receiver (100hz at 10-20% duty cycle), and output a servo signal (similar to the receiver signal). I'm not using any delays, only triggering the ultrasonic sensor 10x a second. The ISR's are set to only document the times at which the pulse rises and seperate ones for when they fall (4 ISR in total, 2 for each pin) so that it only takes a split microsecond to execute.

Hopefully some of the readers are brighter than I am and can get meaning from that.

I'm hesitant to show all of the code yet as I'm contemplating selling the end device.

I'm equally hesitant to give my time for free so you can boost your profits.

...R

Robin2:
Hopefully some of the readers are brighter than I am and can get meaning from that.

The SR-04 is this:

It is triggered with a logic high on the trigger pin, sends out a high frequency sound wave which bounces back and is read by the receiving transducer. Between the time the pulse is sent out and the time the wave bounces back and hits the receiving transducer, the echo pin is held high. the width of that pulse can be converted using the speed of sound to a distance in whatever unit of measurement you want.

The receiver is a standard radio control receiver, the signal runs at 100hz (100 times a second) with a pulse width of 1000ms - 2000ms for a duty cycle (on time percentage) of 10-20%. Just like what it would take to run a servo.

ISR(Interrupt service routine) is the code I shared in my last post, which I've made as light weight as humanly possible.

Robin2:
I'm equally hesitant to give my time for free so you can boost your profits.

...R

As for that, I wasn't trying to be rude and if it came across that way, I'm sorry. However, this is my project for my final in college and I believe I've thought of a pretty good idea that may get me out of debt and change my daily food routine from always being ramen noodles. All of these forums are 100% public accessibly and tons of people read them everyday. It just feels like something I should protect. I'm not trying to get anyone to contribute code or anything, I'm just trying to figure out this library issue and the possible timing issues. I'm a broke, in-debt, jobless college student who is currently going bald from pulling his hair out :frowning:

That explanation helps a lot but I am still confused.

Is the ISR that you posted trying to measure the pulse widths from the R/C receiver, or from the SR04

Rather than routinely switching the ISR from rising to falling, why not use CHANGE and use digitalRead() to detect whether it is LOW or HIGH.

And, now that I think about it, I'm not sure what problem you are now seeking help with.

...R

Robin2:
That explanation helps a lot but I am still confused.

Is the ISR that you posted trying to measure the pulse widths from the R/C receiver, or from the SR04

Rather than routinely switching the ISR from rising to falling, why not use CHANGE and use digitalRead() to detect whether it is LOW or HIGH.

There's 4 ISR's, 2 for rising and falling of the receiver, 2 for rising and falling of the SR04, so measuring both

I'm not sure how I would implement the CHANGE interrupt without possibility for errors or complicating/lengthening the execute timing?

Problem I am having is when I use a O-scope to read all of the waves, the output is erradic (within spec but basically having nothing to do with the inputs). Looking through it all, code seems fine but there's a timing conflict. I figured ServoTimer1 would fix the problem but it seems now like it is the servo library now.

So, here's my pin layout:

pin 2 = receiver
pin 3 = SR04
pin 6 = trigger for SR-04 (just a digital write high)
pin10= Servo

I'm assuming the normal Servo library uses timer1 by default?
Does timer 1 effect either of the hardware interrupts?
Would it be better to use timer 2? only 100hz is needed, so a 16mhz timer seems overkill

laser411:
I'm hesitant to show all of the code yet as I'm contemplating selling the end device. Here is the ISR's, which would be the only thing that I would think would mess the program up.

So give us a cut-down, but complete sketch that demonstrates the problem you are having. Test it yourself to be sure it does indeed have the issue you are seeking help with, then post that complete sketch. It's better for us anyway, since we have less code we have to look at.

laser411:
I figured ServoTimer1 would fix the problem but it seems now like it is the servo library now.

Without seeing a complete program I can't envisage what is in your mind when you say that. For me that statement is not much diifferent from saying "I figured strawberry jam would fix it". I can't make a connection between ServoTimer1 and your ISRs.

...R

Hi,

I am trying to read an SR-04 ultrasonic sensor, read a receiver (100hz at 10-20% duty cycle), and output a servo signal (similar to the receiver signal).

I think the problem with understanding your problem, is understanding your application.
You have two inputs that you are measuring in the time domain, now what do you want to do with these two time measurements when you have them?
Where does the servo output come into this.
There is simple code to measure the time output of the SR-04,
http://playground.arduino.cc/Main/UltrasonicSensor

The same bit of programming could also be used to measure the output of the Rx.
I hope you are not trying to get a rock solid and stable output from the SR-04, the is quite a bit of jitter, even with a close target.

Tom.... :slight_smile: