Hey!
I need help with my code. I'm making a column race game, but I couldn't figure out the solution how to make it random. Right now it displays "Press the button to play". After you press the button it show's the game name "Column race" and starts to count down 3...2...1 and then the 2 columns should start racing from left to right.
How could I make it random? For example print x amount of blocks on the top column and then x amount of blocks on the bottom column.
I tried to use random(); but couldn't really figure it out how to use it properly.
Here's my code.
Help is really appriciated.
#include <LiquidCrystal.h>
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte block[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
int buttonPin = 2;
int buttonState = 0;
int col = 0;
int row = 0;
int run;
void setup(){
lcd.begin(16, 2);
lcd.createChar(0, block);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
randomSeed(analogRead(0));
run = 0;
}
void(*resetFunc)(void)=0;
void loop(){
lcd.setCursor(0, 0);
lcd.print("Press the button");
delay(300);
lcd.setCursor(4, 1);
lcd.print("to play");
delay(1500);
lcd.clear();
delay(500);
if (digitalRead(buttonPin) == LOW) {
buttonState = digitalRead(buttonPin);
lcd.setCursor(2, 0);
lcd.print("COLUMN RACE!");
delay(1500);
lcd.clear();
if(run == 0) {
run = 255;
} else {
run = 0;
}
}
if(run > 0) {
if (digitalRead(buttonPin) == HIGH) {
lcd.setCursor(7, 0);
lcd.print("3");
delay(1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("2");
delay(1000);
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("1");
delay(1000);
lcd.clear();
}
//int j = random(10);
for (int col = 0; col < 16; col++) {
for (int row = 0; row < 2; row++) {
lcd.setCursor(col, row);
lcd.write(byte(0));
delay(150);
}
}
}
resetFunc();
}
