Arduino LCD column race game

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();
}

Sorry, still not sure what you want to randomize.

I want to randomize the column generating process. The LCD is 16x2. So there will be 2 columns (top and bottom).
By randomizing I mean how many block symbols will be added at once and on which column (top or bottom). Basically it should be completely random which column reaches to the end. It should continuously keep adding blocks until one of the two columns reaches the end.

Right now the top column reaches the end first because it adds 1 symbol to the top column and then switches to the bottom one and adds 1 symbol there, and so on. Columns are generated from left to right.

I hope you understand

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.