준호씨의 블로그

Arduino - LCD Keypad Shield - Move character 본문

메이커

Arduino - LCD Keypad Shield - Move character

준호씨 2017. 8. 3. 00:34
반응형

Movie

Source Code

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // for lcd keypad shield
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
byte smiley[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b00000,
0b10001,
0b01110,
0b00000
};
byte armsDown[8] = {
0b00100,
0b01010,
0b00100,
0b00100,
0b01110,
0b10101,
0b00100,
0b01010
};
byte armsUp[8] = {
0b00100,
0b01010,
0b00100,
0b10101,
0b01110,
0b00100,
0b00100,
0b01010
};
int x = 0;
int y = 0;
int pre_key = -1;
// read the buttons
int read_LCD_buttons() {
int adc_key_in = analogRead(0); // read the value from the sensor
if (adc_key_in > 1000) return btnNONE; // about 1023
if (adc_key_in < 50) return btnRIGHT; // about 0
if (adc_key_in < 195) return btnUP; // about 99
if (adc_key_in < 380) return btnDOWN; // about 255
if (adc_key_in < 555) return btnLEFT; // about 409
if (adc_key_in < 790) return btnSELECT; // about 640
return btnNONE; // when all others fail, return this...
}
void setup() {
// initialize LCD and set up the number of columns and rows:
lcd.begin(16, 2);
lcd.createChar(0, heart);
lcd.createChar(1, smiley);
lcd.createChar(3, armsDown);
lcd.createChar(4, armsUp);
// set the cursor to the top left
lcd.setCursor(0, 0);
// Print a message to the lcd.
lcd.print("I ");
lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte
lcd.print(" Arduino! ");
lcd.write((byte)1);
delay(2000);
}
void loop() {
int lcd_key = read_LCD_buttons(); // read the buttons
if (lcd_key != pre_key) {
pre_key = lcd_key;
switch (lcd_key) { // depending on which button was pushed, we perform an action
case btnRIGHT:
x++;
break;
case btnLEFT:
x--;
break;
case btnUP:
y--;
break;
case btnDOWN:
y++;
break;
}
if (x < 0) x = 0;
if (x > 15) x = 15;
if (y < 0) y = 0;
if (y > 1) y = 1;
}
lcd.clear();
lcd.setCursor(x, y);
if ((millis() / 1000) % 2 == 0) {
lcd.write(3);
} else {
lcd.write(4);
}
delay(100);
}
반응형
Comments