I am trying to make up a small timer that will be used as a time delay for a model rocket. It will fire an electrical ejection charge. I may use it in the Adafruit "Trinket".
To detect liftoff, either a very small thin wire will be burned by the rocket engine exhaust, or a pull-pin will let go (well proven methods for starting timers). The wire will therefore act like a switch that is "HIGH" to keep the timer from starting, then when it goes LOW the timer will start.
Due to the desire to be able to have feedback to know the status , a piezo beeper will be be sequenced to beep certain ways depending on whether it is waiting for liftoff, has started the delay time, and a long beep when it fires (I know about the current limitations of the pin outputs, I'll be using a transistor to fire the charge)
The outline for the program is like this:
Launch sensor pin (high waiting for launch, low when launched)
Status LED and status Piezo beeper
"Fire" LED that lights when the time delay has reached the programmed time to fire.
Begin loop.
delay of 100, for 1/10 second increments
If Sensor pin high, do nothing, loop back to start to check again 1/10 second later
If sensor pin low, then start the timing count:
Time = Time +1 (adds 1/10 second increments)
Counter1 = Counter1 + 1 (adds 1/10 second increments)
If Counter1 = 10, Counter1 = 0 (resets the counter so it only is used for tenths of a second.
If sensor pin high again, reset Time to zero (opportunity to reset the time count)
If counter1 = 2, Beep the piezo beeper and light LED 12 (on for 1/10 second)
(there would later be other beep timing depending on status, using the 1/10 second increments form couture)
And finally the most important:
If Time = EjectDelayTime, then light LED 10 (and sound peizo beeper) for 1 second
End of loop
I put together the programming but it is not behaving correctly at all. LED 10 (ejection)is flashing, every second, on about 90% of the time (the Piezo beeper on pin 9 is also beeping with it), when LED 10 it ought not to light at all until launch has been detected and the time has run to the delay time.
LED 12 is on continuously, when it and the peizo beeper out to be on only 1/10 second per second.
I added serial commands to debug it.
What I should see in the serial command is Buttonstate for the launch sensor (0 or 1), the Time count, and the Counter1 count.
When I run it, only the Buttonstate is working right, 1 when high, 0 when low.
Time reads 50, which is my test delay time (50 tenths of a second). Bt that ought not to be possible, it should take 5 seconds to reach 50, but it produces this number every 1/10 seconds
Counter reads zero.
I suspect part of the problem may be that the curly brackets for nesting the If commands may be out of whack.
Can soon please help me figure out what is going wrong?
FWIW - this is for a serious project. See my rocket website for some of the stuff I've done.
http://georgesrockets.com/GRP/GRP-home.htm
- George Gassaway
// For a model rocket ejection delay timer
int led10 = 10; // Power to this LED will also be used by a transistor to fire the ejection
int led12 = 12; // visual Staus
int beeper1 = 9; // audio status
int EjectDelayTime = 50; // Time of the delay in tenths of a second. THis value will be changed as needed for the desired model and conditions
int Time = 0; // time loop counter, in 1/10 second
int counter1 = 0; // a counter only for tenths of a second, is reset to zero when it reaches 10
const int buttonPin = 6; // Pin location for the breakwire that starts the timer. Named Pushbutton from existing code I used in this
int buttonState = 0; // when the pushbutton is high, the timer does not start. Pushbutton low timer starts
void setup() {
pinMode(led10, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(beeper1, OUTPUT);
pinMode(buttonPin, INPUT);// initialize serial communication at 9600 bits per second:
Serial.begin(9600);}
void loop()
{
delay(100); // wait for a 1/10 second (100 milliseconds)buttonState = digitalRead(buttonPin);
{
digitalRead(buttonPin);
}// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(led12, HIGH);
digitalWrite(beeper1, HIGH);
Time = 0; // resets time count back to zero, can stop a time count back to zero. But count will resume if the pin goes low again
}
else { // this begins the count, in 1/10 second increments
// turn LED off:
digitalWrite(led12, LOW);
Time = Time + 1;
counter1 = counter1 +1;
}
{
if (counter1 = 10);
counter1 = 0; // reset counter1 to zero
}{
// the following If/Else is to make the beeper beep for 1/10 second, every second
if (counter1 = 2) {
digitalWrite(led12, HIGH);
digitalWrite(beeper1, HIGH);
}
else {
digitalWrite(led12, LOW);
digitalWrite(beeper1, LOW);
}
}// Fires ejection for 1 second
{
if (Time = EjectDelayTime ) {
digitalWrite(led10, HIGH); // Fires ejection for 1 second
digitalWrite(beeper1, HIGH);
delay (1000);
digitalWrite(led10, LOW); // stops firing, transistor goes low
digitalWrite(beeper1, LOW);}
{
if (counter1 = 10);
counter1 = 0; // reset counter1 to zero
}}
Serial.print(buttonState);
Serial.print(" Time ");
Serial.print(Time);
Serial.print(" Counter= ");
Serial.println(counter1);} // last line