failboy no color miniprojekt lcd spel!
här är ett miniprojekt som jag har gjort, men även fått lite hjälp med programmeringen från en mkt mer erfaren programmerar i klassen efter som jag hade mkt problem när jag försökte få en bana som scrollar mot en över skärmen få kollisioner att fungera.
koden är skriven i c++/c arduino platform.
koden!
/*
The circuit:
* LCD RS pin to digital pin 2
* LCD Enable pin to digital pin 3
* LCD D4 pin to digital pin 4
* LCD D5 pin to digital pin 5
* LCD D6 pin to digital pin 6
* LCD D7 pin to digital pin 7
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//int potpin = 0;
int button = 8;
byte p100[8] = {
B01110,
B01110,
B00100,
B11111,
B00100,
B01010,
B11011,
};
byte p120[8] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
};
byte block[8] = {
B11111,
B10001,
B10001,
B10001,
B10001,
B10001,
B11111,
};
#define BOARD_HEIGHT 2
byte board[][BOARD_HEIGHT] =
{ {5, 5},
{5, 5},
{5, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 5},
{5, 5},
{5, 6},
{5, 5},
{5, 5},
{6, 5},
{5, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 6},
{5, 5},
{5, 5},
{6, 5},
{5, 5},
{6, 5},
{5, 5},
{5, 5},
{5, 6},
{5, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 5},
{6, 5},
{6, 5},
{5, 5},
{5, 5},
{5, 6},
{5, 5},
{5, 6},
{5, 5},
{6, 5},
{5, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 5},
{6, 5},
{6, 5},
{6, 5},
{5, 5},
{5, 6},
{5, 6},
{5, 6},
{5, 5},
{5, 5},
{6, 5} };
#define BOARD_WIDTH (sizeof(board) / (sizeof(byte) * 2))
#define SCREEN_WIDTH 16
#define SCREEN_HEIGHT 2
byte getFromBoard(int xPos, int yPos)
{
if (xPos < BOARD_WIDTH && yPos < BOARD_HEIGHT)
{
return board[xPos][!yPos];
}
else
{
return 5;
}
}
void draw(int xPos, int yPos)
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
lcd.setCursor(x, y);
byte obj = getFromBoard(x + xPos, y + yPos);
lcd.write(obj);
}
}
}
void drawGubben(int yPos)
{
lcd.setCursor(0, yPos);
lcd.write(4);
}
void clearScreen()
{
for (int x = 0; x < SCREEN_WIDTH; ++x)
{
for (int y = 0; y < SCREEN_HEIGHT; ++y)
{
lcd.setCursor(x, y);
lcd.write(5);
}
}
}
void printMessage(char* top, char* bottom)
{
lcd.setCursor(0, 0);
lcd.print(top);
lcd.setCursor(0, 1);
lcd.print(bottom);
}
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
pinMode(button, INPUT);
Serial.begin(9600);
//Make progress characters
lcd.createChar(4, p100);
lcd.createChar(5, p120);
lcd.createChar(6, block);
printMessage("FAILBOY NO COLOR", "PRESS B TO MOVE");
delay(5000);
}
int gubbensXPos = 0;
void loop()
{
// int gubbensYPos = map(analogRead(potpin), 0, 1023, 0, 1);
int gubbensYPos = digitalRead(button);
delay(5);
Serial.println(gubbensYPos);
if (getFromBoard(gubbensXPos, gubbensYPos) != 5)
{
clearScreen();
printMessage("GAME OVER!", "YOU NOOB!");
delay(5000);
gubbensXPos = 0;
}
else if (gubbensXPos == BOARD_WIDTH)
{
clearScreen();
printMessage("YOU WIN!!!", "YOU R BEST!");
delay(5000);
gubbensXPos = 0;
}
else
{
draw(gubbensXPos++, 0);
drawGubben(gubbensYPos);
delay(500);
}
}