element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Help a noob
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 6 replies
  • Answers 2 answers
  • Subscribers 389 subscribers
  • Views 1069 views
  • Users 0 members are here
Related

Help a noob

wazi
wazi over 4 years ago

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.

 

}

  • Sign in to reply
  • Cancel

Top Replies

  • BigG
    BigG over 4 years ago +4 suggested
    LCD flickering is due to using this "lcd.clear();" repeatedly. Don't use after the first clear. For your first line on LCD. Just write once and that's it. Then all you need to do is update second line…
  • wazi
    wazi over 4 years ago in reply to Andrew J +4
    Hi Andrew Thanks so much for that, I really appreciate your assistance with this project. You are going to have a very happy 7 year old when he presses the buttons to launch his rocket. I changed this…
  • Andrew J
    Andrew J over 4 years ago in reply to wazi +3 verified
    No probs, we all start somewhere and you've clearly had a good go and got it pretty much working. I'm not too clear on what you are trying to achieve with the display of digits: does your value have an…
  • BigG
    0 BigG over 4 years ago

    LCD flickering is due to using this "lcd.clear();" repeatedly.

    Don't use after the first clear.

    For your first line on LCD. Just write once and that's it.

    Then all you need to do is update second line. To do this you need to create a 16 char buffer and insert your variables then just overwrite the second line with this buffer. No need to clear first.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Andrew J
    0 Andrew J over 4 years ago

    Assuming you don't want to actually display 10ths of a second, then you just need to add ".0" to your seconds variable that you output to the LCD.

     

    If you do want to display 10ths of a second then you'll need to do a few things:

    • change DownCntr to be a float type - integers can't hold decimal places
    • Decrement DownCntr by 0.1 : instead of DownCntr--, use DownCntr -= -0.1.  Why the delay(50) after waiting 100millis?  That will probably need to go to keep the down count reasonably accurate.
    • Change the ShowTimer code to use a float format rather than a decimal format when initialising variable seconds - either truncate to 1decimal place or round to 1 decimal place.
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • wazi
    0 wazi over 4 years ago in reply to Andrew J

    Hi Andrew

    Thanks for the reply, i really appreciate it.

    I am sorry for sounding like an idiot. I am not really experienced in coding and still have a long way to go.

     

    I am assuming this is the section of code you are referring to to change?

    I really don't need the 10ths of seconds to display

     

    void ShowTimer() {

      String seconds = String (DownCntr, DEC);

      while (seconds.length() < 3)seconds = "0" + seconds; //format to 3 numbers.

      // while (seconds.length() < 3)seconds = "0.1"  ; //format to 3 numbers.

     

      lcd.setCursor(1, 0);

      lcd.print("Launching In");

      lcd.setCursor(1, 1);

      lcd.print(seconds);

      lcd.setCursor(5, 1);

      lcd.print("Seconds ");

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Andrew J
    0 Andrew J over 4 years ago in reply to wazi

    No probs, we all start somewhere and you've clearly had a good go and got it pretty much working. 

     

    I'm not too clear on what you are trying to achieve with the display of digits: does your value have an implied decimal point or do you want to add a decimal point plus 0.  So if your pot gives you a starting value of 199, should that display as 19.9, 19.0 or 199.0?

     

    Keeping the code simple and understandable then instead of your line String seconds = String(etc...:

    Treat 199 as 19.0:

    Float displayCount = DownCntr / 10; // This will treat the answer as an integer so 199/10 becomes 19.9 but the decimal places will be dropped leaving 19 (I don't think it will round up!).  displayCount will h
    String seconds = String(displayCount, 1);

    Treat 199 as 19.9

    Float displayCount = DownCntr / 10.0; // This will treat the answer as an integer so 199/10 becomes 19.9 and displayCount will hold 19.9
    String seconds = String(displayCount, 1);

     

    Treat 199 as 199.0

    String seconds = String(DownCntr) + ".0"; // just append the decimal place

     

    I think you are trying to achieve a specific display value length of 3: 101, 100, 099, 098, ... 010, 009...000.  Adding 2 characters changes this test to < 5 characters as I assume you still want to display 3 significant digits.

     

    Can I just add that I haven't been able to try this code but I believe it will work - let me know if it doesn't!!  Good luck.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • wazi
    0 wazi over 4 years ago in reply to Andrew J

    Hi Andrew

    Thanks so much for that, I really appreciate your assistance with this project. You are going to have a very happy 7 year old when he presses the buttons to launch his rocket.

     

    I changed this -

     

    //String seconds = String (DownCntr, DEC);

    //while (seconds.length() < 3)seconds = "0" + seconds; //format to 3 numbers.

     

    To this -

     

    float displayCount = (DownCntr / 10.0);         // This will treat the answer as an integer so 199/10 becomes 19.9 and displayCount will hold 19.9 

    String seconds = String(displayCount, 1);

     

    And everything is working as I wanted it to. Having the decimal was not a must but I thought it would add something "Special" to the display and make it feel like he is launching SpaceX

     

    To Andrew Johnson and BigG, Thank you for your input and advice on this project. You guys are real Gents in my book.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Andrew J
    0 Andrew J over 4 years ago

    Excellent, well done.  I see I got my comment wrong - it should of course read “...will treat the answer as a float...” and not “...as an integer...”

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube