Ship Game [Solved]

The purpose of this program is an basis for an game that I will be developing that needs ship movement. This will be a block, of my later program. If the rButton is pushed, it should move the ship one to the right and vise versa, except if it is at the ends of the LCD. What is wrong with the program. When I push one of the buttons, it does nothing. Are the buttons setup wrong, or is there somthing wrong with the program itself. Thanks-Ben :wink:

#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { 
  B00000,
  B00000,
  B00100,
  B00100,
  B01010,
  B01010,
  B11111,
  B00000 
};
int lButton = 13;
int rButton = 6;
int lState = 0;
int rState = 0;
int shipPlacement=0;
boolean lTrue= false;
boolean rTrue = false;

void setup() { 
pinMode(lButton, INPUT);
pinMode(rButton, INPUT); 
lcd.createChar(0, Ship);
lcd.begin(16, 2);
lState=digitalRead(lButton);
rState=digitalRead(rButton);
}
void loop() {

 



if(rState==HIGH){
  if(shipPlacement=15){
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
   delay(250);
  }
 else if(shipPlacement<15){
    shipPlacement++;
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
    delay(250);
}
}
else if(lState==HIGH){
  if(shipPlacement=0){
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
   delay(250);
  }
 else if(shipPlacement>0){
    shipPlacement--;
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
    delay(250);
 }

}
else{
  lcd.setCursor(shipPlacement,1);
  lcd.write((byte)0);
}
}

shipPlacement=15
shipPlacement=0

These seem to be missing an = somewhere.

HazardsMind:
shipPlacement=15
shipPlacement=0

These seem to be missing an = somewhere.

Thanks I will try. If not I will let you know

I tried the solution, and the “ship” icon, will not move when the button is pressed. Thank you though :wink:

#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { 
  B00000,
  B00000,
  B00100,
  B00100,
  B01010,
  B01010,
  B11111,
  B00000 
};
int lButton = 13;
int rButton = 6;
int lState = 0;
int rState = 0;
int shipPlacement=0;
boolean lTrue= false;
boolean rTrue = false;

void setup() { 
pinMode(lButton, INPUT);
pinMode(rButton, INPUT); 
lcd.createChar(0, Ship);
lcd.begin(16, 2);
lState=digitalRead(lButton);
rState=digitalRead(rButton);
}
void loop() {

 



if(rState==HIGH){
  if(shipPlacement==15){
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
   delay(250);
  }
 else if(shipPlacement<15){
    shipPlacement++;
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
    delay(250);
}
}
else if(lState==HIGH){
  if(shipPlacement==0){
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
   delay(250);
  }
 else if(shipPlacement>0){
    shipPlacement--;
    lcd.setCursor(shipPlacement,1);
    lcd.write((byte)0);
    delay(250);
 }

}
else{
  lcd.setCursor(shipPlacement,1);
  lcd.write((byte)0);
}
}

–Fixed Syntax Code

lState=digitalRead(lButton);
rState=digitalRead(rButton);

I would put these two lines of code

in the beginning of your main loop, too.

because

void setup() {
pinMode(lButton, INPUT);
pinMode(rButton, INPUT);
lcd.createChar(0, Ship);
lcd.begin(16, 2);
lState=digitalRead(lButton);
rState=digitalRead(rButton);
}

the code inside the setup will only run once.

Thank you!!! It works, I knew it had to be an syntax error!! :smiley:

Congrats! :slight_smile: