Pages: 1 2 3 [4]   Go Down
Author Topic: HELP Please - Idiot Dad tells his kid he can make a rocket launcher with a Ardui  (Read 4185 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 162
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My response wasn't just a correction; it is different code.
It will work and allows you to put in all the checks you want and they'll have instant response as delay() is not used.

The code will look clean if you create a separate subroutine just for the checks.
It returns a zero if all is OK, otherwise it returns an abort code number.

Think of "millis()" as if it were better named to be "currentTimeFromLongAgoInMilliseconds()"
Code:
int LauncherSystemCheck()
{
    if ( checkRelayPower() != 0 )
         return 1;
    if ( IgnitorContinuity() != 0 )
         return 2;
    if ( KeyInserted() != 0 )
         return 3;

    return 0;
}

void loop()
{
   int abortCode = LauncherSystemCheck();
   if (abortCode != 0)
   {
       Serial.print("Abort!  code = ");
       Serial.println(abortCode);
       while (true)
          ;                       // Never start - infinite loop
   }

   int  new_elapsed_seconds = (millis() - startTime) / 1000;
   if (new_elapsed_seconds != elapsed_seconds)
   {
        elapsed_seconds = new_elapsed_seconds;
        Serial.println(blastoff_seconds - elapsed_seconds);
   
        if (elapsed_seconds == blastoff_seconds)
        {
              Serial.println("Blast Off!");
              // do Blastoff!
        }
    }   
}
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3512
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Happy Father's Day! Hope you're having a great time with your offspring and this project. We're model rocketeers from way back. Here's a sketch to try, maybe it will help get past the countdown part. It's just a demo, no LCD, etc., and does not attempt to address all the features and requirements posted earlier smiley-eek

Code:
#include <Button.h>        //https://github.com/JChristensen/Button
#include <Streaming.h>     //http://arduiniana.org/libraries/streaming/

#define BUTTON_START 4     //wire from pin 4 to ground
#define BUTTON_ABORT 5     //wire from pin 5 to ground
#define redLED 6           //wire from pin 6 to ground through appropriate current limiting resistor (e.g. 330 ohm)
#define grnLED 7           //wire from pin 7 to ground through appropriate current limiting resistor (e.g. 330 ohm)
#define PIEZO 8            //wire from pin 8 to ground through appropriate current limiting resistor (e.g. 100 ohm)
#define DEBOUNCE_MS 25     //debounce interval (ms)
#define COUNTDOWN_SEC 10   //number of seconds to countdown from

Button btnStart(BUTTON_START, true, true, DEBOUNCE_MS);    //instantiate the buttons, pullups on, inverted logic
Button btnAbort(BUTTON_ABORT, true, true, DEBOUNCE_MS);

unsigned long ms;          //the time from millis()
unsigned long msCount;     //time when the count was changed
unsigned long msLED;       //time when the LED was turned on
int count;                 //the countdown counter

enum {START, STANDBY, COUNTDOWN, ABORT, LAUNCH, WAIT};    //states for the state machine
uint8_t STATE;             //current state machine state

void setup(void)
{
    Serial.begin(115200);
    pinMode(redLED, OUTPUT);
    pinMode(grnLED, OUTPUT);
    pinMode(PIEZO, OUTPUT);   
}

void loop(void)
{
    ms = millis();
    btnStart.read();
    btnAbort.read();
   
    switch ( STATE ) {
       
        case START:
            noTone(PIEZO);
            digitalWrite(redLED, LOW);
            digitalWrite(grnLED, HIGH);
            Serial << endl << "Stand by to begin countdown sequence..." << endl;
            STATE = STANDBY;
            break;
       
        case STANDBY:
            if ( btnStart.wasPressed() ) {
                count = COUNTDOWN_SEC;
                tone(PIEZO, 1500, 800);
                digitalWrite(redLED, HIGH);
                digitalWrite(grnLED, LOW);
                msCount = ms;
                msLED = ms;
                STATE = COUNTDOWN;
                Serial << "Countdown: " << _DEC(count) << " ... ";
            }
            break;

        case COUNTDOWN:
            if ( btnAbort.wasPressed() ) {
                STATE = ABORT;
                break;
            }
           
            if ( ms - msLED >= 500 && count > 1) {
                digitalWrite(redLED, LOW);
                msLED = ms;
            }

            if ( ms - msCount >= 1000 ) {
                msCount = ms;
                digitalWrite(redLED, HIGH);
                msLED = ms;
                tone( PIEZO, 1500, --count == 1 ? 900 : 100 );
                if ( count == 0 )
                    STATE = LAUNCH;
                else
                    Serial << _DEC(count) << " ... ";
            }
            break;
           
        case ABORT:
            Serial << endl << "Countdown aborted at T-" << _DEC(count) << " seconds!" << endl;
            STATE = START;
            break;

        case LAUNCH:
            tone(PIEZO, 2000, 1000);
            digitalWrite(redLED, HIGH);
            digitalWrite(grnLED, HIGH);
            msLED = ms;
            Serial << "LAUNCH!" << endl;
            STATE = WAIT;
            break;
           
        case WAIT:
            if ( ms - msLED >= 2000 ) STATE = START;
            break;
    }
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Somerset UK
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Thanks for all the ideas and help

Techylah

Your code looked perfect for what i want to do, so I removed the safety check part for now and just used the timer part

I added the entries into globals and startup

and this into the countdown loop

Code:
  void CountDown2()
 
{
 lcd.clear();
int  new_elapsed_seconds = (millis() - startTime) / 1000;
   if (new_elapsed_seconds != elapsed_seconds)
   {
        elapsed_seconds = new_elapsed_seconds;
        lcd.print(cdt - elapsed_seconds);
          Serial.println(cdt - elapsed_seconds);
   
        if (elapsed_seconds == cdt)
        {
              fire();
        }
    }   
 
} // end CountDown

But it does not run it just returns to the main menu?

It also prints a randon negative number to the serial monitor and LCD

fire() never runs

Any ideas?
Logged

Sometimes, it's not as easy as you think.....

Somerset UK
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have just uploaded the full code to here, you will probally find the problem if you can see all the code

http://lowerstoford.co.uk/files/test.zip
Logged

Sometimes, it's not as easy as you think.....

Somerset UK
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

serial monitor debug output

Start Up
Load Main Menu
Auto Selected
Auto Mode
CountDown2 Start
-8
CountDown2 end
Load Main Menu
« Last Edit: June 17, 2012, 03:05:26 pm by lowerstoford » Logged

Sometimes, it's not as easy as you think.....

Offline Offline
Full Member
***
Karma: 0
Posts: 162
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I searched all the code in your zip file.  I do not see any Serial.print statements that include "CountDown2", as in your "CountDown2 Start" or "CountDown2 end"
Perhaps that is not the same code.  Also, better indentation would make the code much more readable.
Logged

California
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, for the wireless you could use the xbee wireless module, or for cheaper, you could use a wireless adapter, and a router running openwrt. For the buttons and stuff I think you should break them down into pieces. ex: make that keypad so it can take the input and convert to a countdown, save that code and setup once you get it working. Next make a button that starts a timer, and save that code and get it working, then go to your fail-safe key etc. and when  you are done combine all the code. It's easier to troubleshoot when it's in small bits so you can locate the problem easier.
Logged

Proud Member of the Nighthawk Robotics Club Team 569 B

Somerset UK
Offline Offline
Newbie
*
Karma: 1
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

***********UPDATE***********

I thought I would update you all on this project

Firstly I would like to thank everyone for all the help and advice and I would also like to apologise for not replying and updating this post.

This project has been very slow to progress, due to the massive learning curve I have encountered and work/family commitments

The Controller unit is now 98% complete, the knowledge I have gained through the development of this part of the system should hopefully allow me to complete the Pad unit very quickly.

I will add some images of the project over the next few days/weeks and I hope to be preforming live tests on the system in early November.
I am planning to use the system to launch our new rocket (Armstrong 2012) before the end of the year.

More updates to follow...............
Logged

Sometimes, it's not as easy as you think.....

Pages: 1 2 3 [4]   Go Up
Jump to: