The idea for my project is that, using a joystick, you can input a series of integers into a password. Depending on the position of the joystick, there will be a different value for the integer. (Up and Left is 1, Up is 2, Up and right is 3, etc.)
The function for reading the joystick works fine, however the function that takes the reading and inputs it into the array is giving me trouble.
Here is my code:
#define joyX A0 //Joystick X value as analog pin 0
#define joyY A1 //Joystick Y value as analog pin 1
#define pB 2 //Push button value as digital pin 2
int xValue, yValue, pButton, Press;
bool Left, Right, Up, Down;
int PW[4];
void setup() {
Serial.begin(9600);
pinMode(pB, INPUT);
digitalWrite(pB, HIGH);
pinMode(joyX, INPUT);
pinMode(joyY, INPUT);
}
void loop() {
checkJoystick();
printDirections();
while (Press >= 1) {
inputPassword();
}
printPassword();
}
//Custom Functions
void checkJoystick() {
xValue = analogRead(joyX);
yValue = analogRead(joyY);
pButton = digitalRead(pB);
if (yValue < 500) {
Down = false;
Up = true;
} else if (yValue > 510) {
Up = false;
Down = true;
} else {
Up = false;
Down = false;
}
if (xValue < 500) {
Left = true;
Right = false;
} else if (xValue > 510) {
Right = true;
Left = false;
} else {
Right = false;
Left = false;
}
if (pButton == 0) {
Press++;
}
}
void inputPassword() {
for (int i = 0; i < 4; i++) {
Serial.print("Awaiting input for digit #");
Serial.println(PW[i + 1]);
if (Left && Up && !Right && !Down) { //IF UP AND LEFT
PW[i] = 1;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("1.");
} else if (!Left && Up && !Right && !Down) { //IF UP
PW[i] = 2;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("2.");
} else if (!Left && Up && Right && !Down) { //IF UP AND RIGHT
PW[i] = 3;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("3.");
} else if (Left && !Up && !Right && !Down) { //IF LEFT
PW[i] = 4;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("4.");
} else if (!Left && !Up && Right && !Down) { //IF RIGHT
PW[i] = 5;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("5.");
} else if (Left && !Up && !Right && Down) { //IF LEFT AND DOWN
PW[i] = 6;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("6.");
} else if (!Left && !Up && !Right && Down) { //IF DOWN
PW[i] = 7;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("7.");
} else if (!Left && !Up && Right && Down) { //IF RIGHT AND DOWN
PW[i] = 8;
Serial.print("Input ");
Serial.print(PW[i + 1]);
Serial.print(":");
Serial.println("8.");
}
}
}
void printDirections() {
Serial.print("UP:");
Serial.print(Up);
Serial.print(" DOWN:");
Serial.print(Down);
Serial.print(" LEFT:");
Serial.print(Left);
Serial.print(" RIGHT:");
Serial.print(Right);
Serial.print(" PRESS:");
Serial.println(Press);
}
void printPassword() {
for (int j = 0; j < 4; j++) {
Serial.println(PW[j]);
}
}
//void checkPassword() {
//void playBuzzer() {
//void flashLED() {
I would appreciate any advice/guidance. For some reason my college decided that a class where basically all we do is arduino should be taught by a guy who cant code ![]()