Pages: [1]   Go Down
Author Topic: Airsoft time bomb code  (Read 1900 times)
0 Members and 1 Guest are viewing this topic.
Spain
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody,

First of all I've got to say that I've never used Arduino and I'm totally green in terms of programming, but I'm very interested in this project, I'm an airsoft player and I've been looking for a timed bomb for a long time.

Bassically, what the bomb should do is this:

-When you turn up the power, the device asks you for a password, that will be set as the defuse password. When you enter it, the count down starts.
-The detonation timing should be in the code, so players can't change it.
-Then bomb asks you for the defuse password.
-If you enter the right password before the time is out, bomb tells "DEFUSED" or somethign like that and then restarts itself.
-If time count gets to 0, a relay is activated(This in turn activates a horn) during 15 seconds or so and then the device restarts itself.


I've found a project that suits my requirements: http://www.instructables.com/id/Prop-bomb (You can see I commented there)

Then, after investigating a bit more, in that same post, I found a link that drove me to this code:

 
Code:
/*Airsoft TIMER
 Version 1.5
 Creators:
 Chase Cooley
 Joey Meyer
 Turned into usable code, 6/16/2011:
 Matt Falcon*/

//#define DEBUG
//#include "DebugUtils.h"
#include <Keypad.h>
#include <LiquidCrystal.h>
#define pound 14
#define LED_RED 6
#define LED_GREEN 5
#define KP_ROWS 3
#define KP_COLS 4




unsigned int timerSeconds = 300; // start with 5 minutes on the clock
byte password[4];
byte entered[4];
int currentLength = 0;
byte i = 0;

LiquidCrystal lcd(8,7, 10, 11, 12, 13);


//Keypad
char keys[KP_ROWS][KP_COLS] = {
  {
    '1', '2', '3', '4'     }
  , {
    '5', '6', '7', '8'     }
  , {
    '*', '9', '0', '#'     }
};
byte rowPins[KP_ROWS] = {
  2,14,15}; //connect to the row pinouts of the keypad
byte colPins[KP_COLS] = {
  19,18,17,16 }; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KP_ROWS, KP_COLS);

char key;

//LCD setup
void setup() {
  Serial.begin(9600);
  pinMode(LED_RED, OUTPUT); // sets the digital pin as output
  pinMode(LED_GREEN, OUTPUT); // sets the digital pin as output
  lcd.begin(16, 2);
  lcd.print("Enter Code: ");
  lcd.cursor();
  lcd.setCursor(6, 1);
  while (currentLength < 4) {
    key = keypad.getKey();
    if (key != NO_KEY) {
      lcd.print(key);
      password[currentLength] = key;
      currentLength++;
      lcd.setCursor(currentLength + 6, 1);
    }
    else delay(10);
  }
  lcd.noCursor();
  lcd.clear();
  lcd.print("You've Entered: ");
  lcd.setCursor(6, 1);
  lcd.print(password[0]);
  lcd.print(password[1]);
  lcd.print(password[2]);
  lcd.print(password[3]);
  delay(2500);
  lcd.clear();
  currentLength = 0;
}

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 ");
      tone(9,1200, 4000); //
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Bomb Rebooting");
      lcd.setCursor(0,1 );
      lcd.print("Have a nice day!");
      currentLength = 0;
      delay(4000); // hold that on screen for 2.5 seconds
      lcd.clear();
      currentLength = 0;
      setup();
    }
    else {
      lcd.noCursor();
      lcd.clear();
      lcd.print(" Wrong ");
      tone(9,110, 2500); 
      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
      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 green 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();
}
void bombomb() {
  // routine for making ze BOMB GOEZ OFF!!!1 :D
  lcd.noCursor();
  lcd.clear();
  lcd.print(" !BoOo0o0o0om! ");
  lcd.setCursor(0, 1);
  lcd.print("you LoSe Sucker!");
  tone(9,110, 100);
  digitalWrite(LED_RED, HIGH); // sets the red LED on
  delay(500); // waits half a second
  tone(9,110, 100);
  delay(500); // waits half a second
  digitalWrite(LED_RED, LOW); // sets the red LED off
  digitalWrite(LED_GREEN, HIGH); // sets the green LED on
  tone(9,110, 100);
  delay(500); // waits half a second
  digitalWrite(LED_RED, HIGH); // sets the red LED on
  digitalWrite(LED_GREEN, LOW); // sets the green LED off
  tone(9,110, 100);
  delay(500); // waits half a second
  digitalWrite(LED_RED, LOW); // sets the red LED off
  digitalWrite(LED_GREEN, HIGH); // sets the green LED on
  tone(9,110, 100);
  delay(500); // waits half a second

  key = NO_KEY; // enter the while() loop, otherwise it'll skip. i could use do { } while();, but I'm lazy.
  {
    while (key == NO_KEY) {
      key = keypad.getKey();
      if (key == '#')
      {
        lcd.clear();
        lcd.print("Resetting:");
        lcd.setCursor(6,1);
        lcd.print("the Bomb!");
        delay(3000); //  3 sec delay
        void (*softReset) (void) = 0; //declare reset function @ address 0
        softReset();
        //timerSeconds = 300; // put 5 minutes on the clock
        // and we break the loop and go back to timer.
      }
      else  key = NO_KEY; // continue looking for pound key
    }
  }
}


Could you tell me if this new code is right or wrong?  If it is right, what pins Am I suposed to connect to the to the keypad and the LCD display? The same as in the original project postes on Instructables? I'll be using the same hardware setup.


Thanks everybody, and sorry for my poor english.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

LCD pins (from code above):

Code:
LiquidCrystal lcd(8,7, 10, 11, 12, 13);

In this order by the looks of the LiquidCrystal reference page:

Code:
LiquidCrystal(rs, enable, d4, d5, d6, d7)

Keypad row pins:

Code:
byte rowPins[KP_ROWS] = {  2,14,15};

Keypad column pins:

Code:
byte colPins[KP_COLS] = {  19,18,17,16 }; //connect to the column pinouts of the keypad

Quote
Could you tell me if this new code is right or wrong?

Try it and see? It's hard to generally say code is right or wrong.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you should have read step 4?

http://www.instructables.com/id/Prop-bomb/step4/Wiring-and-such/

Code:
You'll have to just follow the schematic we unfortunately didn't make a step by step on this.   
Heres a quick outline to go along with the schematic
Arduino pins :

DPin 0 = Nothing
DPin 1 = Nothing
DPin 2 = Pin 8 on keypad
DPin 3 = Red LED
DPin 4 = Green LED
DPin 5 = Nothing
DPin 6 = Nothing
DPin 7 = Pin 4 LCD
DPin 8 = Pin 6 LCD
DPin 9 = Speaker +
DPin 10 = Pin 11 LCD
DPin 11 = Pin 12 LCD
DPin 12 = Pin 13 LCD
DPin 13 = Pin 14 LCD
APin 0 = Pin 2 Keypad
APin 1 = Resistor to Pin 3 Keypad
APin 2 = Pin 4 Keypad
APin 3 = Resistor to Pin 5 Keypad
APin 4 = Resistor to Pin 6 Keypad
APin 5 = Resistor to Pin 7 Keypad

LCD Screen Pins:

Heres the missing pins on the LCD
LCD pin1 = Potentiometer 5v
LCD pin 2 = Potentiometer GND
LCD pin 3 = Potentiometer Middle
LCD pin 5 = GND
LCD pin 15 = 5v
LCD pin 16 = GND
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18811
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I feel a great disturbance in the Force when I read the code:

http://www.instructables.com/id/Prop-bomb/step5/Uploading-the-code/
Logged


Spain
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe you should have read step 4?
Lots of thanks for the help, Nick.

I read that step, but the codes aren't the same, so I didn't know if the pin connections still the same.

I'll buy all the hardware I need and get all the parts together, load the code and report the results.

Thanks again.
Logged

Pages: [1]   Go Up
Jump to: