I'm sure I'm missing something very simple here, but I've hit a wall with my first project.
What I'm trying to achieve:
Set pins A, B, C, D, and E as inputs
Have arduino read the state of 3 digital pins: A, B, and C
Listen for a total of 5 separate inputs on those 3 pins that can be either a single pin, or two pins simultaneously, store order of inputs
After 5 inputs are received, set pins A, B, and C to outputs
Play back sequence on pins A, B, and C upon receiving an input to pin D, flash LED upon input to pin E
I have global variables to "store" the inputs and assign the possible output types:
int firstInput;
int secondInput;
int thirdInput;
int fourthInput;
int fifthInput;
int currentInput;
int inputCount;
int combo1() {
digitalWrite(A, HIGH);
press();
off();
wait();
}
int combo2() {
digitalWrite(A, HIGH);
digitalWrite(C, HIGH);
press();
off();
wait();
}
int combo3() {
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
press();
off();
wait();
}
int combo4() {
digitalWrite(B, HIGH);
press();
off();
wait();
}
int combo5() {
digitalWrite(C, HIGH);
press();
off();
wait();
}
const int press() { // HOW LONG OUTPUT IS ON
delay(100);
}
const int wait() { // HOW LONG TO WAIT BETWEEN OUTPUTS
delay(50);
}
const int off() { // RETURN OUTPUTS TO "OFF" STATE
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
here is my setup and main loop (and where I think part of my problem is)
void setup() {
inputCount = 0;
pinMode(D, INPUT);
pinMode(E, INPUT);
digitalWrite(D, HIGH); // TURN ON PULL UPS
digitalWrite(E, HIGH);
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
}
void loop() {
while (inputCount < 5) {
digitalWrite(LED, HIGH); // TURN ON AN LED TO LET ME KNOW IT'S STILL IN LEARN ROUTINE
currentInputLearn(); // CALL INPUT LEARN FUNCTION
}
if (inputCount == 5) { // IF LEARN ROUTINE IS COMPLETE CARRY ON MAIN LOOP
digitalRead(D);
digitalRead(E);
if (digitalRead(D) == LOW) { // IF D IS LOW, OUTPUT LEARNED COMBINATION, (THIS IS WHERE I THINK PART OF MY PROBLEM IS)
firstInput;
secondInput;
thirdInput;
fourthInput;
fifthInput;
}
else {
off();
}
if (digitalRead(E) == LOW) {
digitalWrite(LED, HIGH)
}
}
}
here is my input learning function
int currentInputLearn() { // ASSIGN WHICH POSITION IS CURRENTLY BEING LEARNED
digitalWrite(A, LOW); // TURN ON PULL DOWNS
digitalWrite(B, LOW);
digitalWrite(C, LOW);
pinMode(A, INPUT); // SET A, B, AND C TO INPUTS TO LEARN CODE
pinMode(B, INPUT);
pinMode(C, INPUT);
if (inputCount == 0) {
currentInput = firstInput;
learn();
}
else if (inputCount == 1) {
currentInput = secondInput;
learn();
}
else if (inputCount == 2) {
currentInput = thirdInput;
learn();
}
else if (inputCount == 3) {
currentInput = fourthInput;
learn();
}
else if (inputCount == 4) {
currentInput = fifthInput;
learn();
}
else if (inputCount == 5) { // IF INPUT LEARN ROUTINE IS DONE, SET PINS A, B, AND C BACK TO OUTPUTS
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
}
}
int learn() { // MAIN INPUT LEARN FUNCTION
unsigned long startPress = 0; // THIS IS RELATED TO MAKING SURE AN INPUT IS ONLY ACCEPTED IF IT IS HELD FOR A CERTAIN TIME
while (inputCount < 5) {
digitalRead(A);
digitalRead(B);
digitalRead(C);
if (digitalRead(A) == LOW && digitalRead(B) == LOW && digitalRead(C) == LOW) {
startPress = 0;
}
else {
if (startPress == 0) {
startPress = millis();
}
else if (millis() - startPress > 1000) {
if (digitalRead(A) == HIGH && digitalRead(B) == LOW && digitalRead(C) == LOW) {
currentInput = combo1();
learnBlink(); // BLINKS THE LEARN LED AS A VISUAL INDICATOR THAT INPUT WAS ACCEPTED, AMOUNT OF BLINKS INDICATES WHAT INPUT WAS READ
delay(1000);
inputCount++;
}
if (digitalRead(A) == HIGH && digitalRead(B) == LOW && digitalRead(C) == HIGH) {
currentInput = combo2();
learnBlink();
learnBlink();
delay(1000);
inputCount++;
}
if (digitalRead(A) == HIGH && digitalRead(B) == HIGH && digitalRead(C) == LOW) {
currentInput = combo3();
learnBlink();
learnBlink();
learnBlink();
delay(1000);
inputCount++;
}
if (digitalRead(A) == LOW && digitalRead(B) == HIGH && digitalRead(C) == LOW) {
currentInput = combo4();
learnBlink();
learnBlink();
learnBlink();
learnBlink();
delay(1000);
inputCount++;
}
if (digitalRead(A) == LOW && digitalRead(B) == LOW && digitalRead(C) == HIGH) {
currentInput = combo5();
learnBlink();
learnBlink();
learnBlink();
learnBlink();
learnBlink();
delay(1000);
inputCount++;
}
}
}
}
if (inputCount == 5) {
digitalWrite(13, LOW); // TURN LED OFF AS VISUAL INDICATOR LEARN ROUTINE IS OVER
}
}
After uploading it to my arduino, the learn routine LED comes on and blinks accordingly to indicate the input combinations (INPUT A, INPUTS A&C, etc.)
Once 5 inputs are accepted, the LED turns off
If I set input E to low, the LED comes on as expected, however if I set pin D to low, nothing happens.
My thoughts are that I have something screwed up in the main loop as to how it's calling the "recorded" inputs to playback or I am not correctly associating the inputs to a variable when I'm "recording" them in the learn() function.
It likely isn't relevant, but I can get the arduino to output a correct combination of A, B and C if I manually write the sequence in the code, however I want the arduino to learn the sequence from the user, then play it back in the same order. The playback output durations will be fixed, so only the order is necessary to be "learned/recorded".
I appreciate any help.
