Hi guys! I need some help with how to make my code. This is my first time for writing a "bigger" project on my own and it´s starting to become something.
I´m aiming to make a "Simulated Bomb" for AirSoft games. I´m going to use a Arduino Uno and a Adafruit RGB LCD Shield to keep it as simple as I can. So far I have written code for setting the time with Up and Down buttons and "arming the bomb" whit the Select button.
But now comes my problem. To make it fair on the enemies they should have a chance to disarm the bomb. I want to use the Up, Down, Left and Right buttons for this, but Up and Down are already in use for the function of setting the time. So how do I make to be able to use them again?
I also want to make it random witch button that disarm the bomb, this code I have written but I don´t know if its the best way the I have made.
I would like to get some guiding in how I can fix this or where I can read on how to make something like this.
Beware of ugly like hell coding
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
int timer = 30;
long randNumber;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Status = Unarmed");
lcd.setBacklight(GREEN);
lcd.setCursor(0, 1);
lcd.print(timer);
randomSeed(analogRead(0));
randNumber = random(0, 5);
}
uint8_t i=0;
void loop()
{
//while (Serial.available() == 0);
//int val = Serial.read() - '0';
// lcd.setCursor(0, 1);
// lcd.print(millis()/100);
//uint8_t buttons = lcd.readButtons();
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
if (buttons & BUTTON_SELECT)
{
Serial.println("1");
lcd.setCursor(0, 0);
lcd.print("Status = Armed");
lcd.setBacklight(RED);
for(;timer > 0; --timer)
{
delay(1000);
lcd.setCursor(0, 1);
lcd.print(timer);
if (timer < 10)
{
lcd.setCursor(1, 1);
lcd.print(" ");
}
if (timer < 100)
{
lcd.setCursor(2, 1);
lcd.print(" ");
}
//randNumber = random(0, 5);
lcd.setCursor(15, 1); // as you can see i have made an bad attempt to make the disarm function but this did not work well...
lcd.print(randNumber);
if (randNumber = 0)
{
if (buttons & BUTTON_UP)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Peew! ");
lcd.setCursor(0, 1);
lcd.print(" You made it! ");
}
if (buttons & BUTTON_DOWN & BUTTON_LEFT & BUTTON_RIGHT)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("You better run!!");
lcd.setCursor(0, 1);
lcd.print("Now 10s left ;)");
delay(2000);
timer = 10;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Status = Armed");
lcd.setBacklight(RED);
for(;timer > 0; --timer)
{
delay(1000);
lcd.setCursor(0, 1);
lcd.print(timer);
if (timer < 10)
{
lcd.setCursor(1, 1);
lcd.print(" ");
}
}
} // here ends my failure.
}
}
if (buttons & BUTTON_UP)
{
timer = timer ++;
delay(200);
lcd.print("Status = Unarmed");
lcd.setCursor(0, 1);
lcd.print(timer);
}
if (buttons & BUTTON_DOWN)
{
timer = timer --;
delay(200);
lcd.print("Status = Unarmed");
lcd.setCursor(0, 1);
lcd.print(timer);
}
}
if (timer < 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" BOOOOM!! ");
lcd.setCursor(0, 1);
lcd.print("You got smacked!");
delay(10000);
}
}
}
//Richard