Pages: [1]   Go Down
Author Topic: Timer Program  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
Newfoundland,Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
This Arduino stuff is great and as poke my interest after reading a lot of stuff that people as done with, one of them is a fellow in Australia who as done some coding for flightsim. I'm working on this for my cockpit stuff.
My son is in paintball and airsoft stuff and I came across this code for the airbomb. I know this is an old code but I just need a little help to add or fix part of the code as I'm also a newbie.
I got the code to work the way I want and with the hardware that I have, screen, lights, sound and keypad.
Only flaw that I cant seem to be able to fix is when the timer is counting down and I press a key the timer stop and if I only press 1 to 3 keys it stays like that forever until I press 4 keys, then it will show either it is wrong or defused the way it is supposed to.
In a game this is not good as one only have to push a key and the bomb is frozen in time.
The timer should either keep on going or if no inputs is keyed in a set of time like (4 or 5 sec) then the timer should resume automatically.
In the code it mention about pause the timer effectively I think it's too effective!! lol smiley-grin
Anyway here is what I got as far as code goes (REMEMBER I'M ONLY A NEWBIE) lol
The full file is attached.


void loop() {
  lcd.clear();
  lcd.print("Enter Code: ");
  lcd.cursor();
  key = keypad.getKey(); // get the key once per this loop
  if (key != NO_KEY) {
    lcd.setCursor(0, 1);
    lcd.print("           ");
    lcd.setCursor(6, 1);
    while (currentLength < 4) {
      // here, we take the key that broke us into this "if" statement, instead of discarding it. this also pauses the timer, effectively.
      if (key != NO_KEY)
      { // redundant for the first loop, but necessary for every next character
        lcd.setCursor(currentLength + 5, 1);
        lcd.print(key);
        entered[currentLength] = key;
        currentLength++;
      }
      else delay(10);
      key = keypad.getKey(); // get the key every time "while" loops
    }

    if (memcmp(entered,password,4) == 0) { // shortcut to compare an array of bytes, use memcmp(A, B, length), will return 0 if match.
      lcd.noCursor();
      lcd.clear();
      lcd.print(" Defused ");
      digitalWrite(LED_RED, HIGH); // sets the red LED on
      digitalWrite(LED_GREEN, HIGH); // sets the green LED on
      delay(4000);
      tone(9,1200, 2000); //
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Bomb Rebooting");
      lcd.setCursor(0,1 );
      lcd.print("Have a nice day!");
      currentLength = 0;
      digitalWrite(LED_RED, LOW); // sets the red LED off
      digitalWrite(LED_GREEN, LOW); // sets the green LED off
      delay(4000); // hold that on screen for 2.5 seconds
      lcd.clear();
      currentLength = 0;
      void (*softReset) (void) = 0; //declare reset function @ address 0
        softReset();
      setup();
      
    }
    else {
      lcd.noCursor();
      lcd.clear();
      lcd.print(" Wrong ");
      tone(9,110, 2500);
      digitalWrite(LED_RED, HIGH); // sets the red LED on
      digitalWrite(LED_GREEN, HIGH); // sets the green LED on
      lcd.setCursor(0, 1);
      lcd.print("Penalty -2:00"); // Display time penalty
      if (timerSeconds > 120) timerSeconds -= 120;
      else timerSeconds = 1; // will trigger BOOM next cycle
      currentLength = 0;
      delay(2500); // hold that on screen for 2.5 seconds
      digitalWrite(LED_RED, LOW); // sets the red LED off
      digitalWrite(LED_GREEN, LOW); // sets the green LED off
      lcd.clear();
    }
  }
  timer(); // prints current time on screen, checks for "go off" time, holds loop for 1 second
}

void timer() {
  lcd.setCursor(0, 1); // sets cursor to 2nd line
  lcd.print("Timer: ");
  lcd.print(timerSeconds / 60); // integer number of minutes on clock
  lcd.print(":");
  lcd.print(timerSeconds % 60); // mod 60; gives us number of seconds outside of 60sec/min
  delay(950); // hold us here for 0.950 seconds
  {
    digitalWrite(LED_RED,!digitalRead(LED_RED)); // toggle the red LED once a second
  }
  tone(9,800, 250); // play bleep for 50ms
  delay(50); // and there's the rest of that 1 second (not counting LCD delay)

  timerSeconds--;
  if (timerSeconds == 0) bombomb();
}

* AirBomb2.ino (11.4 KB - downloaded 12 times.)
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3644
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you using the arduino itself to do the timer, and not a RTC? Your in a while loop and you will remain in there until the condition is met. So you will only get out when you have all 4 digits. If you want the counter to continue counting during this while loop, you will need to get a RTC.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Newfoundland,Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At the moment I'm using an Arduino mega but I plan to use a UNO later to many inputs with the Mega for this little project and I will use it for my flight stuff later when I puts it together.
How much problem will it be to add the Real Time Clock to it if it's needed, i'm guessing that the arduino only carry out one task at a time is that why the RTC is needed?
Thanks for the fast response!!!
Mario
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3644
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It shouldn't be too much of a problem, but it depends on the number on pins your using. I cant open your code file here at work, so I don't know exactly what pins are in use. My advise would be to do some research on RTC's, and try to see if it will work with what you have already.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Newfoundland,Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, Hazard
right now these are the pin's I'm using on the Arduino,

LCD = pin 7,8,10,11,12,13,GND,5v
Buzzer = pin 9,GND
Leds = pin 5,6,5v
Keypad = pin 2,14,15,16,17,18,19,5v

so only pin 0,1,3,4,20,21 and analogin is not being used. (if that can help!! lol)
Mario
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26334
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One particular moderator put a lot of time and effort into writing and posting the sticky threads at the top of each section of the forum.

Please show your appreciation by reading them before posting
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Queens, New York
Offline Offline
Faraday Member
**
Karma: 101
Posts: 3644
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Give this a look at. http://playground.arduino.cc/Main/DS1302
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Theres no need for an RTC. In timer() remove the delays and use millis() to see if a second has passed and code should be ran or not, like THIS. Then put a call to timer() in the while loop. Ideally you should get rid of that while loop and just set a variable that decides which part of the code to run since loop() is already called from a while loop.
« Last Edit: March 03, 2013, 10:59:19 am by Cookies » Logged

Pages: [1]   Go Up
Jump to: