Help me please :(

Can you help me to fix this code. The problem is that it doesn't receive the commands. It is a game of an arrow dodging random obstacles.

#include <LiquidCrystal.h>

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

// Pulsadores
int UP = 11;
int DOWN = 12;
int score;
String flech= "->";
int posiflech;
signed char pos1;
signed char pos2;
int a;

// Definición de los mensajes a mostrar en el LCD
const char* mensaje1 = "Homework Number 5. Student: Hamira Huaccho";
const char* mensaje2 = "Pulse any button";

void setup()
{
Serial.begin(9600);
pinMode(UP, INPUT);
pinMode(DOWN, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(mensaje1);
lcd.setCursor(0, 1);
lcd.print(mensaje2);
}

void displayTextSequence() {
// Mostrar el mensaje en movimiento en la primera fila
for (int i = 0; i < strlen(mensaje1) + 16; i++) {
lcd.clear();

// Imprimir el mensaje en movimiento en la primera fila
lcd.setCursor(0, 0);
if (i < strlen(mensaje1))
  lcd.print(mensaje1 + i);
else
  lcd.print(mensaje1);

// Imprimir el mensaje estático en la segunda fila
lcd.setCursor(0, 1);
lcd.print(mensaje2);

delay(250);

}
}
void loop(){
if (digitalRead(UP) == 0 || digitalRead(DOWN) == 0){
lcd.clear();
pos1= random(3,16);
pos2= random(3,16);

while (abs(pos1 - pos2) <= 2){
  pos1 = random(3, 16);
  pos2 = random(3, 16);}

 lcd.setCursor(pos1, 0);
 lcd.print("|");
 lcd.setCursor(pos2, 1);
 lcd.print("|");
 for (int lug=0; lug<16; lug++){
   delay(800);
   lcd.setCursor(lug,a);
   lcd.print("->");
   lcd.setCursor(lug-1,a);
   lcd.print(" ");
   
   if(digitalRead(DOWN) == 0 || digitalRead(UP)== 0){ //Condicional para el pulsador DOWN
     lcd.setCursor(lug-1,a);
     lcd.print("   ");   
     if(digitalRead(UP) == 0){
      a=0;}
     if(digitalRead(DOWN) == 0){
      a=1;}
    }
    if(a==0 && lug == pos1 + 1 || a==1 && lug == pos2 + 1){
     lcd.clear();
     lcd.setCursor(4, 0);
     lcd.print("You lose!");
     delay(800);
     resetJug();
   break;}
   
    if (lug==15){
      lcd.clear();
      lcd.setCursor(1, 0);
      score++;
      lcd.print("Score:");
      lcd.setCursor(7, 0);
      lcd.print(score);}

}
}
}
void resetJug(){
score= 0;
}

1 Like

To be as short as your question:
check the receiving .

I'm very sure that this short sentence does not help.
Same with your posting. You are unloading all the work to your potential helpers instead of showing own effort by describing as detailed as you can what you expect the code to and what you observe instead.

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

first thing to do is to add code tags to your post and ensure the code is indented

Edit your post using the :pencil2: in the tool bar just below your post

select the code part and press the <code/> icon in the tool bar to mark it as code. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlt on a PC or cmdt on a Mac)

➜ do yourself a favour and please read How to get the best out of this forum

The first message looks a bit large for a 16 character LCD. Your teacher may not be able to identify your work from the contents of the screen.
But I guess by now the assignment is already long overdue.

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