Servo Timer conflict? Help please! :(

Hey guys–

I’ve been writing a program for a school design project of mine. It’s the first time I’ve ever used an Arduino (the particular model is the Deicimila), let alone a microcontroller. I’ve had a lot of success. I have two programs that achieve two goals… last night I attempted to combine them and they were not compatible when in the same code-- and I was unable to figure out why.

The first of the two causes a servo to go through two motions, and then a solenoid is triggered. On its own, it executes perfectly.

#include <Servo.h>

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

int ledPin = 13; // variable for ‘on’ LED
int counter = 0; // set counter for servo trigger
int power_switchPin = 7; // set ‘on’ switch to Pin 7
int solenPin = 4; // set solenoid switch to Pin 4
int power_switch_value; // number of interrupts counted

void setup()
{
// attaches the pins
pinMode(ledPin, OUTPUT);
pinMode(solenPin, OUTPUT);
pinMode(power_switchPin, INPUT);
}

void loop()
{
power_switch_value = digitalRead(power_switchPin); //check to see if power switch on

if(power_switch_value == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}

if(counter == 0 && power_switch_value == HIGH) { //run servo motions, trigger solenoid
myservo.attach(10);
delay(5000);
myservo.write(90);
delay(5000);
myservo.write(0);
delay(750);
digitalWrite(solenPin, HIGH);
delay(500);
digitalWrite(solenPin, LOW);
myservo.detach();
counter = 1;
}
else { //prevent servo jitter
myservo.attach(10);
myservo.write(0);
myservo.detach();
}

The second program is meant to use a interrupt to take data from a reflection-sensor (mounted on a wheel so that I can measure the distance my car has travelled, which will then be used to eventually trigger the solenoid). It also uses a timer to keep track of time, so I can compute the velocity of my car. The distance equation doesn’t currently give the correct result, but that will be fixed easily. It stores the data in an array and dumps it to my computer when I’ve flicked a switch. This too has been working when run on its own.

#include <Streaming.h>
#include “TimerOne.h”

int n = 0; // number of interrupts counted
int rotn_counter[60]; // define rotation counter array
int rotn_index = 0;
int power_dump_switchPin = 7;
int dumpSW = 0; //state of dump switch
int c = 0; // counter to ensure data is dumped only once

void setup() {
for (int i = 0; i <= 59; i++) // fill array with known values
{
rotn_counter = -1;

  • }*

  • pinMode(power_dump_switchPin, INPUT);*

  • attachInterrupt(0, rotn_sensor, FALLING);*

  • Timer1.initialize(); // initialize timer1*

  • Timer1.attachInterrupt(save_counter, 500000); // attaches dump_counter() as a timer overflow interrupt*

}
void loop()
{

  • dumpSW = digitalRead(power_dump_switchPin);*

  • if (dumpSW == HIGH && c == 0)*

  • {*

  • dump_data();*

  • c = 1;*

  • }*
    }
    *void rotn_sensor() { // count rotations detected by sensor *
    n++;
    }
    void save_counter() //save rotations detected to array
    {

  • if( rotn_index <= 59)*

  • {*

  • rotn_counter[rotn_index] = n; // count this interrupt*

  • rotn_index++;*

  • }*
    }
    void dump_data() //dump stored data to computer
    {

  • float time;*

  • float distance;*

  • Serial.begin(9600); // start serial communication*

  • Serial.println(“Dumping data:”);*

  • Serial << “Time” << “\t” << “Distance” << endl;*

  • for (int i = 0; i <= 59; i++)*

  • {*

  • time = i/2.0;*
    distance = (rotn_counter_/4.0)3.14;
    Serial << time << “\t” << distance << “\t” << rotn_counter << endl;
    }
    }[/quote]*
    Unfortunately I need to combine the two programs, and here is where problems occur! :frowning: I don’t currently have a copy of the combined program. I must have saved over it accidentally, and today I burnt out my sensor so I can’t properly make one and test to see which part causes the error. I probably should have waited until then to make this topic, but I was hoping that someone could offer some insight!
    Basically, when the Servo code is integrated with the Sensor code, and the servo switch is OFF (ie. the servo portion of the code is dormant) the sensor code’s timer does not even run! I realize once activated the "delay()"s will cause problems, but I haven’t reached that stage yet-- eventually it will be triggered by sensor data.
    When I tested it my servo was attached to pin 10 or 11, don’t remember. (Once again, if my sensor was working…) My sensor is attached to pin 2. I heard the Arduino Deicimila has two (or three?) timers. Do the Servos use timers? Are they conflicting with the timer that I set up? Any help greatly, greatly appreciated._

I am having the same issue. The problem is that both TimerOne and Servo use Timer 1 and configure it differently. I'm looking into a resolution and will post when I find it.

Rather than using interrupts for the reflection sensor, you could probably get by with fast polling; the wheel of the car likely won't be turning anywhere near fast enough for you to miss an event, and even if you do, its likely a percentage of error that would be masked by wheel slippage over distance anyway. Your only other option would probably be to make a custom library that incorporated both the servo and timer libraries together...