New to arduino: trying to create a hurdle jump game with 16x2 LCD

Hi i’m having some trouble with a hurdle jump game i’m trying to create.

I’m able to create moving hurdles with this loop that minuses the y position of the cursor each time the hurdle (’<’) is printed:

void loop() {
for(int i=16; i>-1; i–){
lcd.clear();
lcd.setCursor(i,1);
lcd.print("<");
delay(1000);
}
}

And i’ve also been able to create a character using a byte, with a push button that sets the cursor up one x position whenever the buttonState == HIGH:

void loop() {

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
lcd.clear();
lcd.write(byte(0));

} else {
lcd.clear();
lcd.setCursor(0,position+1);
lcd.write(byte(0));
}

}
}
lastButtonState = buttonState;
}

The issue lies in when I try to combine them, I’ve been trying to do this by pasting the hurdle code into the if and else statement, although when i do this I have to wait until the hurdle reaches the end before I’m able to press the button again which ends up clearing the display and restarting the hurdle animation rather than allowing it to continue.

is there anyway to run both of these loops at the same time and leave the hurdles unaffected by my push button, so that the character can actually jump over these hurdles?

You need to get rid of the "for" loop, and the "delay()".
have a look at the blink without delay example to get some clues.

Please get into the habit of using code tags when posting code.

Thanks for the tips. I'm having a look into the blink without delay example now, although how do I make "i" reduce by 1 each time the loop is run without using the "for" loop?

and sorry about the format of the code, i'll make sure to use code tags in the future :slight_smile:

Jgreensie:
although how do I make "i" reduce by 1 each time the loop is run without using the "for" loop?

Exactly the same way you do it now - by subtracting 1.

Ok so i’ve tried to integrate the use of long and millis functions to replace my delay, and have changed the way it minuses 1 from “i” but its still unable to show both the character and the “<” hurdle at the same time. I must’ve missed something important in that example because i’m completely stuck once again.

Here is the code that i have atm:

#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20, 16, 2); // Set LCD’s address to be 0x20. Let it display 2 lines with 16 characters per line
dht11 DHT;
#define DHT11_PIN 4
int position = 0;

byte blob[8]={
  0b00000,
  0b00000,
  0b00100,
  0b01110,
  0b10101,
  0b11111,
  0b01110,
  0b00000,
};


const int buttonPin = 2;
unsigned long previousMillis = 0;
const long interval = 1000;
int i = 16;

//variables will change
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int lcd_width = 16;

  

void setup() {
  pinMode(buttonPin, INPUT);
  lcd.init(); //LCD initial setup
  lcd.backlight(); // open LCD’s backlight
  Serial.begin(9600); // Set serial port’s baud rate to be 9600\
  // serial port prints ”Type, status, Humidity(%), Temperature(C)”
  lcd.clear();
  lcd.createChar(0,blob);
  
  
  lcd.clear();
  lcd.print("Welcome!");
  delay(2000);
  
  lcd.clear();
  lcd.print("Hurdles");
  lcd.setCursor(0,1);
  lcd.print("Beta version");
  delay(2000);
  
  lcd.clear();
  lcd.print("Press to play");
  delay(1000);
  
  
}

void loop() { 
  
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      lcd.clear();
      lcd.write(byte(0));
      
    } else {
      lcd.clear();
      lcd.setCursor(0,position+1);
      lcd.write(byte(0));
      
    }
  }
  lastButtonState = buttonState; 
  
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;
    
    lcd.clear();
    lcd.setCursor(i,1);
    lcd.print("<");
    i=i-1;
  }
}

It seems to me that you're clearing the LCD too often.
If you want to erase a character, try printing a space in the place that it used to be.

I replaced my lcd.clear with lcd.setCursor and lcd.print(" "), and it now works perfectly! THANK YOU SO MUCH for your help.

Just one other question i have: how would i make it so you lose the game when “<” hits the blob? I’d like it to be able to display “game over” and print the number of milliseconds/seconds the player lasted for.

You know the positions of both objects, so if the positions are equal, then you've hit something.