Hello all!
(This code does function.)
So I have been trying to get a servo door lock with an LCD screen and 4 buttons. I WAS running into quite a bit of trouble with the keypad of the lock. I needed it to provide me with at least 6 keys. I thought I could get it to press two keys at once but it wasn't working whatever I tried. I was ending up with either only the two button combo or the two button combo doesn't exist. I came up with what I thought would be an easy and ingenious way to convince the Arduino to print the correct information using the 4 buttons and a trimpot on an analog pin, which would give me a value from 0-1023. I did not need to press two buttons at once, I just had a technical limitation of 4 physical buttons
I couldn't get it to work but typing it out here made me realize I had put my "return" clause in the wrong spot, so thank you to all of you for being my rubber duck.
In terms of the breadboard, it's just the 4 buttons on their own circuits, not as a matrix, a potentiometer on its own, analog circuit, and an LCD. I couldn't get mine working in a way it was readable with pin 3 of the LCD being either full or no voltage, but one 330 ohm resistor inserted between the track the power is on to the track pin 3 of the LCD made it legible and I'm assuming two 330 ohm resistors would be perfect. I'd show my breadboard but its a disaster zone because this is one small part of a project.
Here's my code (togglekeys and togglekey are from the troubleshooting phase): THE DELAYS ARE LEFTOVERS FROM A TROUBLESHOOTING STEP
#include <LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);
int potPosition;
int key[] = {3,4,5,6};
int type;
void setup() {
potPosition = analogRead(A0);
Serial.begin(9600);
lcd.begin(16,2);
lcd.clear();
pinMode(key[0], INPUT_PULLUP);
pinMode(key[1], INPUT_PULLUP);
pinMode(key[2], INPUT_PULLUP);
pinMode(key[3], INPUT_PULLUP);
}
void loop() {
potPosition = analogRead(A0);
//togglekey();
togglekeys();
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
delay(1000);
}
//-----toggle----
void togglekey (){
if ((digitalRead(key[0] == LOW)) && (potPosition <= 512)){
type = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 1;
}
else if ((digitalRead(key[0] == LOW)) && (potPosition > 512)){
type = 5;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 5;
}
else if ((digitalRead(key[1] == LOW)) && (potPosition <= 512)){
type = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 2;
}
else if ((digitalRead(key[1] == LOW)) && (potPosition > 512)){
type = 6;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 6;
}
else if ((digitalRead(key[2] == LOW)) && (potPosition <= 511)){
type = 3;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 3;
}
else if ((digitalRead(key[2] == LOW)) && (potPosition >= 512)){
type = 7;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 7;
}
else if ((digitalRead(key[3] == LOW)) && (potPosition <= 511)){
type = 4;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 4;
}
else if ((digitalRead(key[3] == LOW)) && (potPosition >= 512)){
type = 8;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 8;
}
else {
delay(1000);
}
}
//-----
void togglekeys (){
if (analogRead(A0) <= 511){
if (digitalRead(key[0]) == LOW){
type = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 1;
}
else if (digitalRead(key[1]) == LOW){
type = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 2;
}
else if (digitalRead(key[2]) == LOW){
type = 3;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 3;
}
else if (digitalRead(key[3]) == LOW){
type = 4;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 4;
}
}
else if (analogRead(A0) >= 512){
if (digitalRead(key[0]) == LOW){
type = 5;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 5;
}
else if (digitalRead(key[1]) == LOW){
type = 6;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 6;
}
else if (digitalRead(key[2]) == LOW){
type = 7;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 7;
}
else if (digitalRead(key[3]) == LOW){
type = 8;
lcd.clear();
lcd.setCursor(0,0);
lcd.println(type);
Serial.println(type);
return 8;
}
}
else {
delay(1000);
}
}


