Hi Guys
Please could you perhaps assist with a piece of code.
The basic idea is to have a countdown timer for my sons model rocket. He sets the time by mapping a pot and then "Arms: the system. After doing the checks he then presses a "Launch" button to initiate the countdown timer.
Once the timer reaches "0" it activates a relay and in turn fires the rocket.
I found the below code on the net and modified it with the help of a friend to use a i2c 16x2 LCD display.
I am in no way an expert but having fun learning.
I have 2 x minor issues now.
1st I need to add a decimal point to the countdown seconds (Currently displaying 000 seconds, I would like 00.0)
2nd, the LCD display is flickering. Even more so when the countdown initiates. ( I think this is due to bad coding)
Is anyone able to point me in the right direction and possibly recommend some improvements?
Code below -
#define FuseTIME 1500 //Fuse current duration in milliseconds.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);//change address according to your LCD
#define Fuss 7 //Pin connected to the Fuse relay.
int GoButt = 6; //Pin Connected to the GO button.
int ArmButt = 5; //Pin connected to the ARM button.
#define BuzzPin 4 //Connected to the Speaker.
#define SetPot 0 //Analog Pin connected to the Pot.
void setup(){
lcd.begin();
pinMode(Fuss,OUTPUT);
pinMode(ArmButt, INPUT); // set "ARM" button pin to input
pinMode(GoButt, INPUT); // set "GO" button pin to input
digitalWrite(Fuss,LOW); //OPEN the fuse circuit.
digitalWrite(ArmButt, HIGH); // turn on pullup resistor
digitalWrite(GoButt, HIGH); // turn on pullup resistor
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Rocket");
lcd.setCursor(3,1);
lcd.print("Launcher");
delay(2000);
}
int DownCntr; // down counter (1/10 Secs.)
int Go=0; // Stopped
//================================================================
void loop(){
if(!digitalRead(GoButt)||!digitalRead(ArmButt)){
Go=0; //ABORT!!!
tone(BuzzPin, 440, 1500);
delay(1500);
}
if(Go==0){
WaitARM();
WaitGO();
}
ShowTimer();
if (DownCntr > 50){
if (DownCntr % 10 ==0)tone(BuzzPin, 1000, 50); //Tone every second.
}
else if (DownCntr % 2 ==0)tone(BuzzPin, 1000, 50); //Tone every 1/5 second.
if (DownCntr ==0){
//------------------ ROCKET LAUNCH! --------------------
tone(BuzzPin, 440, FuseTIME); //Launch audible signal
digitalWrite(Fuss,HIGH); //CLOSE the fuse circuit
delay(FuseTIME);
digitalWrite(Fuss,LOW); //OPEN the fuse circuit.
//------------------------------------------------------
Go=0;
}
while (millis()% 100); //Wait until the next 1/10 of second.
delay(50);
DownCntr--;
}
//----------------------------------------
void WaitGO(){
ShowTimer();
while(digitalRead(GoButt));
Go=1;
delay(20);
while(!digitalRead(GoButt)); //Debounce GO button.
}
//------------------------------------------------------
void ReadTimer(){
DownCntr = map(analogRead(SetPot), 0, 1023, 10, 60); // DownCntr = map(analogRead(SetPot), 0, 1023, 5, 60);
DownCntr*=10;
}
//------------------------------------------------------
void ShowTimer(){
String seconds = String (DownCntr, DEC);
while(seconds.length()<3)seconds= "0" + seconds; //format to 3 numbers.
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Launching In");
lcd.setCursor(1,1);
lcd.print(seconds);
lcd.setCursor(5,1);
lcd.print("Seconds");
}
//------------------------------------------------------
void WaitARM(){
while(digitalRead(ArmButt)==1){
ReadTimer();
delay(50);
ReadTimer();
ShowTimer(); //Show Digits.
delay(150);
}
Go=0;
ShowTimer();
tone(BuzzPin, 2000, 150);
delay(200);
tone(BuzzPin, 2000, 150);
delay(200);
tone(BuzzPin, 2000, 150);
delay(20);
while(!digitalRead(ArmButt)); //Debounce ARM button.
}