Dec hex binary game - Arduino Uno code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define left_ar 8
#define right_ar 9
#define up_ar 10
#define down_ar 11
#define enter_bt 12
#define restart_bt 13
#define green 2
#define red 3
#define analogPin A1
#define slow 200
bool up_once = true;
bool down_once = true;
bool left_once = true;
bool right_once = true;
bool enter_once = true;
bool start_once = true;
int randnr =0;
int dec =0;
int tmp =0;
int Xpos; // cursor position
int Ypos;
int input_pos[8] = {0,0,0,0,0,0,0,0};
const char char_range[17]={'\0','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; // 0 to 16 (17 chars)
char user_input[9]= {'\0','\0','\0','\0','\0','\0','\0','\0','\0'};
char bin[9];
char hex[3];
char decStr[4];
void setup() {
/* 6 inputs: 4 arrows, 1 enter, 1 start
* Data Direction Register: DDRB
* PORT: PORTB
*/
pinMode(left_ar, INPUT);
pinMode(right_ar, INPUT);
pinMode(up_ar, INPUT);
pinMode(down_ar, INPUT);
pinMode(enter_bt, INPUT);
pinMode(restart_bt, INPUT);
pinMode(green, OUTPUT);
pinMode(red, OUTPUT);
lcd.init();
lcd.backlight();
lcd.cursor();
do {
tmp = analogRead(analogPin);
} while ((tmp < 100) || (tmp > 1000));
randomSeed(tmp);
lcd.print("Press Restart");
}
void loop() {
if ((digitalRead(restart_bt) == HIGH) and (start_once)){
start_once = false;
digitalWrite(red, LOW);
digitalWrite(green, LOW);
Xpos = 0;
Ypos = 0;
for (int i=0; i < 8; i++) {
user_input[i] = '\0';
input_pos[i] = 0;
bin[i] = '0';
}
user_input[8]='\0';
bin[8]='\0';
randnr = random(0,6);
dec = random(5,255);
tmp=dec;
if (dec/100 > 0) {
decStr[0] = (dec /100) + 48;
decStr[1] = ((dec % 100) / 10) + 48;
decStr[2] = (dec % 10) + 48;
decStr[3] = '\0';
} else if (dec/10 > 0) {
decStr[0] = (dec / 10) + 48;
decStr[1] = (dec % 10) + 48;
decStr[2] = '\0';
} else {
decStr[0] = dec + 48;
decStr[1] = '\0';
}
for (int i =7; i>=0; i--) {
if (dec % 2 == 1) {
bin[i] = '1';
}
dec = dec / 2;
}
bin[8]='\0';
dec = tmp;
tmp = dec % 16;
if (tmp < 10) {
hex[1] = tmp + 48;
} else {
hex[1] = tmp + 55;
}
tmp = dec / 16;
if (tmp < 10) {
hex[0] = tmp + 48;
} else {
hex[0] = tmp + 55;
}
hex[2]='\0';
lcd.clear();
lcd.setCursor(0,0);
switch(randnr) {
case 0: lcd.print(decStr); // dec - bin
lcd.print(" Bin?");
break;
case 1: lcd.print(decStr); // dec - hex
lcd.print(" Hex?");
break;
case 2: lcd.print("0x");
lcd.print(hex); // hex - bin
lcd.print(" Bin?");
break;
case 3: lcd.print("0x");
lcd.print(hex); // hex -dec
lcd.print(" Dec?");
break;
case 4: lcd.print(bin); // bin - dec
lcd.print(" Dec?");
break;
case 5: lcd.print(bin); // bin - hex
lcd.print(" Hex?");
break;
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if ((digitalRead(down_ar) == HIGH) and (down_once)) {
down_once = false;
Ypos = input_pos[Xpos];
Ypos = Ypos -1;
if (Ypos < 0) {
Ypos = 16;
}
input_pos[Xpos] = Ypos;
user_input[Xpos]=char_range[Ypos];
lcd.setCursor(Xpos,1);
if (Ypos == 0 ) { // null terminating
lcd.print(" ");
} else {
lcd.print(char_range[Ypos]);
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if ((digitalRead(up_ar) == HIGH) and (up_once)) {
// Xpos = 0,1,2,3,4,5,6,or 7
// Ypos = 0 -> 16 -> 0
up_once = false;
Ypos = input_pos[Xpos];
Ypos = Ypos + 1;
if (Ypos > 16) {
Ypos = 0;
}
input_pos[Xpos] = Ypos;
user_input[Xpos]=char_range[Ypos];
lcd.setCursor(Xpos,1);
if (Ypos == 0 ) { // null terminating
lcd.print(" ");
} else {
lcd.print(char_range[Ypos]);
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if ((digitalRead(left_ar) == HIGH) and (left_once)) {
left_once = false;
Xpos -= 1;
if (Xpos < 0) {
Xpos = 7;
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if ((digitalRead(right_ar) == HIGH) and (right_once)) {
right_once = false;
Xpos += 1;
if (Xpos > 7) {
Xpos = 0;
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if ((digitalRead(enter_bt) == HIGH) and (enter_once)) {
enter_once = false;
if ((randnr == 0) || (randnr == 2)) {
tmp = strcmp(bin,user_input);
if (tmp == 0) {
lcd.setCursor(8,0);
lcd.print(bin);
}
} else if ((randnr == 1) || (randnr == 5)) {
tmp = strcmp(hex,user_input);
if (tmp == 0) {
lcd.setCursor(9,0);
lcd.print(hex);lcd.print(" ");
}
} else if ((randnr == 3) || (randnr == 4)) {
tmp = strcmp(decStr,user_input);
if (tmp == 0) {
lcd.setCursor(9,0);
lcd.print(decStr);
lcd.print(" ");
}
}
if (tmp == 0) {
digitalWrite(green, HIGH);
lcd.setCursor(9,1);
lcd.print("Restart");
} else {
digitalWrite(red, HIGH);
}
lcd.setCursor(Xpos,1);
delay(slow);
}
if (digitalRead(up_ar) == LOW) {
delay(20);
if (digitalRead(up_ar) == LOW) {
up_once = true;
}
}
if (digitalRead(down_ar) == LOW) {
delay(20);
if (digitalRead(down_ar) == LOW) {
down_once = true;
}
}
if (digitalRead(left_ar) == LOW) {
delay(20);
if (digitalRead(left_ar) == LOW) {
left_once = true;
}
}
if (digitalRead(right_ar) == LOW) {
delay(20);
if (digitalRead(right_ar) == LOW) {
right_once = true;
}
}
if (digitalRead(enter_bt) == LOW) {
enter_once = true;
}
if (digitalRead(restart_bt) == LOW) {
start_once = true;
}
}