Servo Timer conflict? Help please! :(
hey guys--
i've been writing program school design project of mine. it's first time i've ever used arduino (the particular model deicimila), let alone microcontroller. i've had lot of success. have 2 programs achieve 2 goals... last night attempted combine them , not compatible when in same code-- , unable figure out why.
the first of 2 causes servo go through 2 motions, , solenoid triggered. on own, executes perfectly.
the second program meant use interrupt take data reflection-sensor (mounted on wheel can measure distance car has travelled, used trigger solenoid). uses timer keep track of time, can compute velocity of car. distance equation doesn't give correct result, fixed easily. stores data in array , dumps computer when i've flicked switch. has been working when run on own.
unfortunately need combine 2 programs, , here problems occur!
don't have copy of combined program. must have saved on accidentally, , today burnt out sensor can't make 1 , test see part causes error. should have waited until make topic, hoping offer insight!
basically, when servo code integrated sensor code, , servo switch off (ie. servo portion of code dormant) sensor code's timer not run! realize once activated "delay()"s cause problems, haven't reached stage yet-- triggered sensor data.
when tested servo attached pin 10 or 11, don't remember. (once again, if sensor working...) sensor attached pin 2. heard arduino deicimila has 2 (or three?) timers. servos use timers? conflicting timer set up? greatly, appreciated.
i've been writing program school design project of mine. it's first time i've ever used arduino (the particular model deicimila), let alone microcontroller. i've had lot of success. have 2 programs achieve 2 goals... last night attempted combine them , not compatible when in same code-- , unable figure out why.
the first of 2 causes servo go through 2 motions, , solenoid triggered. on own, executes perfectly.
quote
#include <servo.h>
servo myservo; // create servo object control servo
int ledpin = 13; // variable 'on' led
int counter = 0; // set counter servo trigger
int power_switchpin = 7; // set 'on' switch pin 7
int solenpin = 4; // set solenoid switch pin 4
int power_switch_value; // number of interrupts counted
void setup()
{
// attaches pins
pinmode(ledpin, output);
pinmode(solenpin, output);
pinmode(power_switchpin, input);
}
void loop()
{
power_switch_value = digitalread(power_switchpin); //check 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 meant use interrupt take data reflection-sensor (mounted on wheel can measure distance car has travelled, used trigger solenoid). uses timer keep track of time, can compute velocity of car. distance equation doesn't give correct result, fixed easily. stores data in array , dumps computer when i've flicked switch. has been working when run on own.
quote
#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 ensure data dumped once
void setup() {
for (int = 0; <= 59; i++) // fill array 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() 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 sensor
n++;
}
void save_counter() //save rotations detected array
{
if( rotn_index <= 59)
{
rotn_counter[rotn_index] = n; // count interrupt
rotn_index++;
}
}
void dump_data() //dump stored data computer
{
float time;
float distance;
serial.begin(9600); // start serial communication
serial.println("dumping data:");
serial << "time" << "\t" << "distance" << endl;
for (int = 0; <= 59; i++)
{
time = i/2.0;
distance = (rotn_counter/4.0)*3.14;
serial << time << "\t" << distance << "\t" << rotn_counter << endl;
}
}
unfortunately need combine 2 programs, , here problems occur!
don't have copy of combined program. must have saved on accidentally, , today burnt out sensor can't make 1 , test see part causes error. should have waited until make topic, hoping offer insight! basically, when servo code integrated sensor code, , servo switch off (ie. servo portion of code dormant) sensor code's timer not run! realize once activated "delay()"s cause problems, haven't reached stage yet-- triggered sensor data.
when tested servo attached pin 10 or 11, don't remember. (once again, if sensor working...) sensor attached pin 2. heard arduino deicimila has 2 (or three?) timers. servos use timers? conflicting timer set up? greatly, appreciated.
i having same issue. problem both timerone , servo use timer 1 , configure differently. i'm looking resolution , post when find it.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Servo Timer conflict? Help please! :(
arduino
Comments
Post a Comment