Replies: 4 comments 7 replies
-
H'm, I did try creating multiple timers this morning without any improvement, so I think my problem has nothing to do with the timers. It's quite baffling. I will just go back to banging my head against the table. That usually works. Jennifer |
Beta Was this translation helpful? Give feedback.
-
Hi Jennifer, After a quick look, I see a few issues with your code:
I suggest adding some serial prints to help you debug. |
Beta Was this translation helpful? Give feedback.
-
Thanks, Michael and Phil. I've made an even more stripped down version of the sketch (got rid of torpedoes) and it confirms I can only use a task two times before it stops working. #include <LedFlasher.h>
#include <arduino-timer.h>
const int WEAP_BTN = 8;
const int PHAS_PIN = 4;
auto sysTmr = timer_create_default();
Timer<>::Task phasTsk;
int fireCnt = 0; // how many times weapons fired
int fireLmt = 4; // max number weapons fire
bool weapFlag = 0; // prevents overlapping fire
bool phasFlag = 0;
LedFlasher phasers(PHAS_PIN, 50, 100); // phaser flicker effect
void setup() {
Serial.begin(9600);
pinMode(WEAP_BTN, INPUT);
phasers.begin();
}
void loop() {
sysTmr.tick();
if (digitalRead(WEAP_BTN) == HIGH && !weapFlag) { // prevents overlapping fire and firing in spacedock
weapFlag = 1;
randomFire();
}
if (phasFlag) {
phasers.update();
}
}
void randomFire() {
Serial.println("random");
if (fireCnt < fireLmt) {
phasFlag = 1;
firePhaser("fire");
fireCnt++;
} else {
fireCnt = 0;
weapFlag = 0;
}
}
bool firePhaser(const char *which) {
if (strcmp(which, "fire") == 0) {
Serial.println("fire");
unsigned long duration = 500;
Serial.print("phasTsk: ");
Serial.println(phasTsk);
Serial.print("number of tasks: ");
Serial.println(sysTmr.empty());
Serial.print("timer size: ");
Serial.println(sysTmr.size());
phasTsk = sysTmr.in(duration, firePhaser, "stop");
return true;
} else {
Serial.println("stop");
Serial.print("timer size: ");
Serial.println(sysTmr.size());
Serial.print("phasTsk: ");
Serial.println(phasTsk);
sysTmr.cancel(phasTsk);
Serial.print("phasTsk: ");
Serial.println(phasTsk);
phasTsk = NULL;
Serial.print("phasTsk: ");
Serial.println(phasTsk);
digitalWrite(PHAS_PIN, LOW);
phasFlag = 0;
randomFire();
return false;
}
} And here's the output of the Serial.println's:
So "random" shows every time the Thanks, Phil, for saying I'm good with one timer and multiple tasks, instead of multiple timers with a single task. Question: is a timer created with an automatic task? Well, it's back to banging my head on the table. I could just go back to reading Jennifer |
Beta Was this translation helpful? Give feedback.
-
UPDATE: Although I had success with my simplified phaser sketch, I was again having trouble once I added the photon torpedoes function back in. The phasers function could just use So I gave up and switched the torpedo call to I think Anyway, I've finally got it working, although there were still some residual problems. I found out that it's not a good idea to separately call a function that a task will call. Once I figured that out, however, it seems to be pretty reliable. Thanks again for all the help. Jennifer |
Beta Was this translation helpful? Give feedback.
-
Dear Michael,
Is it better to have multiple timers with one task apiece, or one timer with multiple tasks? I ask because I can't reliably set multiple tasks and cancel them individually. The code below fires phasers and photon torpedoes on a model starship. I want to "randomly" alternate firing phasers and torpedoes with each press of a button, for a total of four weapons fire: one phaser, three torpedoes (port and starboard); one torpedo, one phaser, two torpedoes; etc.
I'm using the LedFlasher library from Nick Gammon to fire the phasers, but I use your timer to tell it when to stop. (I've also tried disabling LedFlasher.) I also use your timer to repetitively increase the intensity of the torpedo effect, and again to ramp down the effect. I've tried it with one timer and assigning a task without obtaining an identifier, and just doing a blanket cancel when I want to end the task. And I've also created multiple tasks and using systemTimer.cancel(phasTask) and systemTimer.cancel(torpTask).
Whatever method I try, it only seems to work for two calls to the randomFire function. After that, the fireTorpedo function or firePhaser functions act unreliably. The randomFire function, for instance, just keeps firing torpedoes endlessly.
I've also tried instantiating the timer with Timer<2> systemTimer, but that causes a different problem — the phaser fires endlessly.
I'd appreciate any suggestions. Tomorrow I'll try creating multiple timers with one task apiece. Ultimately I have four tasks for this sketch, to control spotlights, thrusters, phasers and torpedoes. But for now, I'd just be happy to handle two tasks.
Thanks,
Jennifer Petkus
Beta Was this translation helpful? Give feedback.
All reactions