Hello, I have been working on a card sorter for the game Magic - The Gathering. My arduino works fine transmitting to the SD card and the LCD screen, but I have not been able to use my previous keyboard. I made a keyboard for my arduino calculator but it doesn't work anymore if I plug it into my new arduino. I have a function that enables typing and it can find out which column the key is in via the shift register but cannot find out the row. I think that this is a bunch of short-circuits inside the arduino because it randomly selects a key and uses that key to print on the LCD screen. I have everything working except for the keyboard. I can attach the files on the SD card and the operating system below:
CODE IN THE MICROCONTROLLER:
#include <SD.h>
#include <LiquidCrystal.h>
File myFile;
int keys=0;
int a=0;
int c=0;
int d=0;
int addon=0;
int STEP=1;
int shift=0;
int keyShift=0;
int key=0;
String addDir="";
String text="";
String prev="";
String value="";
String directory="";
String thingy="";
String folder="";
long int ID=0;
long int deckID=0;
const char keyboard[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','~','~','~','~','~','~',' ','-',',','\'','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~'};
const char shifts[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~','~'};
LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
void setup(){
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
lcd.begin(20,4);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MTG Card Sorter by");
lcd.setCursor(0,1);
lcd.print("Pierre Erard,");
lcd.setCursor(0,2);
lcd.print("reading SD card...");
delay(1000);
while(!SD.begin(10)){
delayMicroseconds(10);
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SD card ready.");
delay(1000);
}
void loop(){
STEP=1;
folder="";
directory="/OS/STEP1.TXT";
while(SD.exists(directory)){
directory=(String)"/OS"+folder+"/STEP"+String(STEP)+".TXT";
myFile=SD.open(directory);
text="";
while(myFile.available()){
text=text+(char)myFile.read();
}
myFile.close();
value=text;
if(text=="print"){
printLCD(prev);
}
if(text=="resetFolder"){
folder="";
}
if(text=="switch"){
folder=(String)"/"+addDir+String(key);
}
thingy=(String)text[0]+text[1]+text[2]+text[3];
if(thingy.equals("get:")){
convert(String(text[4]));
d=c;
convert(String(text[5]));
key=0;
while(key<d or key>c){
detectButtons();
}
}
thingy=(String)text[0];
if(thingy=="+"){
addDir=(String)text[1];
}
if(text=="enableTyping"){
value="";
lcd.clear();
lcd.setCursor(0,0);
lcd.print(prev);
lcd.setCursor(0,1);
while(key!=28 or keyShift==0){
key=0;
while(key==0){
detectButtons();
}
if(value.length()==20){
lcd.setCursor(0,2);
}
if(value.length()==40){
lcd.setCursor(0,3);
}
if(value.length()<60){
if(keyShift==1){
if(shifts[key]!='~'){
value=value+shifts[key-1];
lcd.print((String)value[value.length()-1]);
}
}else{
if(keyboard[key]!='~'){
value=value+keyboard[key-1];
lcd.print((String)value[value.length()-1]);
}
}
}
}
}
prev=value;
STEP=STEP+1;
}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("You are logged out.");
delay(1000);
}
void printLCD(String text){
lcd.clear();
lcd.setCursor(0,0);
for(int i=0; i<text.length(); i++){
if(i%20==0){
lcd.setCursor(0,i/20);
}
lcd.print(text);
}
}
void convert(String integer){
c=0;
for(int b=0;b<integer.length();b++){
c=c*10;
switch(integer){
case'1':
c=c+1;
break;
case'2':
c=c+2;
break;
case'3':
c=c+3;
break;
case'4':
c=c+4;
break;
case'5':
c=c+5;
break;
case'6':
c=c+6;
break;
case'7':
c=c+7;
break;
case'8':
c=c+8;
break;
case'9':
c=c+9;
break;
case'A':
c=c+10;
break;
case'B':
c=c+11;
break;
case'C':
c=c+12;
break;
case'D':
c=c+13;
break;
case'E':
c=c+14;
break;
case'F':
c=c+15;
break;
case'G':
c=c+16;
break;
case'H':
c=c+17;
break;
case'I':
c=c+18;
break;
case'J':
c=c+19;
break;
case'k':
c=c+20;
break;
}
}
}
void detectButtons(){
keys=0;
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,1);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=1;
}
if(digitalRead(3)){
keys=8;
}
if(digitalRead(4)){
keys=15;
}
if(digitalRead(5)){
keys=22;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,2);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=2;
}
if(digitalRead(3)){
keys=9;
}
if(digitalRead(4)){
keys=16;
}
if(digitalRead(5)){
keys=23;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,4);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=3;
}
if(digitalRead(3)){
keys=10;
}
if(digitalRead(4)){
keys=17;
}
if(digitalRead(5)){
keys=24;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,8);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=4;
}
if(digitalRead(3)){
keys=11;
}
if(digitalRead(4)){
keys=18;
}
if(digitalRead(5)){
keys=25;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,16);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=5;
}
if(digitalRead(3)){
keys=12;
}
if(digitalRead(4)){
keys=19;
}
if(digitalRead(5)){
keys=26;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,32);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=6;
}
if(digitalRead(3)){
keys=13;
}
if(digitalRead(4)){
keys=20;
}
if(digitalRead(5)){
keys=27;
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,64);
digitalWrite(7,HIGH);
delayMicroseconds(10);
if(digitalRead(2)){
keys=7;
}
if(digitalRead(3)){
keys=14;
}
if(digitalRead(4)){
keys=21;
}
if(digitalRead(5)){
keys=28;
}
keyShift=shift;
key=keys+addon;
if(keys!=0){
if(keys==27){
shift=1-shift;
addon=0;
}else if(keys==28&&shift==0){
addon=28-addon;
}else{
shift=0;
addon=0;
}
}
digitalWrite(7,LOW);
shiftOut(8,9,MSBFIRST,127);
digitalWrite(7,HIGH);
delayMicroseconds(10);
while(digitalRead(2)or digitalRead(3)or digitalRead(4)or digitalRead(5)){
delayMicroseconds(10);
}
}
FILES ON THE SD CARD:
/OS/STEP1.TXT: Press 1 to login or 2 to make an account
/OS/STEP2.TXT: print
/OS/STEP3.TXT: get:12
/OS/STEP4.TXT: +A
/OS/STEP5.TXT: switch
/OS/STEP6.TXT:
/OS/A1/STEP6.TXT: Login with username:
/OS/A1/STEP7.TXT: enableTyping
DIRECTORIES:
E:
OS
A1
TOOLS
USERS
EXPLANATION:
The arduino starts up and reads "Press 1 to login or 2 to make an account" off the SD card. The next command prints it on the screen. Then, this is where the problem comes in: It will think that you are pressing keys like "V", "H", etc... because they are in the same row. This can also occur with the previous step if you are trying to press a key and it won't allow you to login. Although there are no users, I tried to test it but the enableTyping function doesn't work on my arduino. The circuit diagram is:
SD card standard connection, LCD screen connected to analog input pins (Yes that does actually work, it displays fine), shift register in pins 7, 8, and 9. Button return pins are 2, 3, 4, and 5. Diodes are used to prevent circuit from powering all wires at once. (I don't know if they are working because I keep getting that problem...) The keys are connected in an array: The shift register outputs a row, and waits to see if there is any input before moving on. If a key is pressed, the arduino can use the output and input via row and collumn method to find the key.
NONE OF THIS WORKED!!! OK: The arduino malfunctioned on the enableTyping and get:12 functions because the pins are broken. Tell me if I need to buy a new arduino or if I am not using my arduino correctly. When I transfered my calculator keyboard and added a few keys, it completely malfunctioned and I had to take it apart. Eventually, I figured out that the problem is inside the arduino itself. I removed the pins and the serial monitor was still giving output on the digitalRead. I got things like 0110, 0111, 0101, and 1110. I think that the arduino needs to be recalibrated because the sensitivity is off. Please help and thanks for reading the whole thing.
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.