Hey guys and gals,
I've been slowly working through some code this morning which is an implementation of the piezo knock pattern sensor, but instead I am using an IR emitter and collector and attempting to measure the gaps between the sensor being blocked, checking those gap intervals against a master key, and either providing a success action or failure action. I have only gotten as far as trying to get the gap values stored properly into an array.
The code works fairly well up to this point except for two problems:
1. The '6' (last) slot on my array (gapArray) changes with the counter I set up (arrayCounter).
a. All other array slots populate correctly with the time (in ms) of the gap between one sensor block to the next
b. The incrementation for arrayCounter is in "gapTimer()" near the bottom of the code
2. When all 7 slots are filled and I intend for everything to reset back to start (for another entry) the program jumps straight into gapTimer() and not switchTrack().
a. I have tried resetting timerSwitchA and timerSwitchB back to 0 in the final function without luck
I tried to explain this as best I can. After several hours of trying to debug and trouble shoot, I decided to see if anyone could look at this with a fresh pair of eyes. This could easily be replicated with a simple push button (pin 7) and LED (pin 6) if anyone is willing to physically see what the problem is. Thanks!
int ledPin = 6;
int irPin = 7;
int val = 0;
int val2 = 0;
int gapArray[6];
int arrayCounter = 0;
int timerSwitchA = 0;
int timerSwitchB = 0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(irPin, INPUT);
}
void loop()
{
val = digitalRead(7); // Reading value from IR collector
Serial.print(val); // Some serial readouts to verify values
Serial.print(" ");
Serial.print(timerSwitchA);
Serial.print(" ");
Serial.print(timerSwitchB);
Serial.print(" A0:");
Serial.print(gapArray[0]);
Serial.print(" A1:");
Serial.print(gapArray[1]);
Serial.print(" A2:");
Serial.print(gapArray[2]);
Serial.print(" A3:");
Serial.print(gapArray[3]);
Serial.print(" A4:");
Serial.print(gapArray[4]);
Serial.print(" A5:");
Serial.print(gapArray[5]);
Serial.print(" A6:");
Serial.print(gapArray[6]);
Serial.print(" AC:");
Serial.println(arrayCounter);
switchTrack(); //Where the business really starts
}
void switchTrack()
{
if (arrayCounter == 7) // Used to check pattern after all 7 array slots are filled
{
checkPattern();
timerSwitchA = 0;
timerSwitchB = 0;
}
if (val == 1) // If the collector is not receiving IR from emitter
{
digitalWrite(ledPin, HIGH);
timerSwitchA = 1;
}
if (timerSwitchA == 1 && val == 0)
{
timerSwitchB = 2;
}
if (timerSwitchA == 1 && timerSwitchB == 2) // Used to check for when the sensor becomes unblocked to move into gapTimer()
{
gapTimer();
}
}
void gapTimer() {
digitalWrite(ledPin, LOW);
for (int x = 0; x < 2000; x++) // Counts up to 2 seconds at max for each gap
{
val2 = digitalRead(7);
Serial.println(x);
if (val2 == 1) // When sensor is blocked again, stores current x value in array slot incremented by arrayCounter
{
Serial.print(x);
Serial.print(" ");
gapArray[arrayCounter] = x;
arrayCounter++;
break;
}
timerSwitchA = 0; // Resets
timerSwitchB = 0;
}
}
void checkPattern() { // Will eventually be used to check array values against preprogrammed pattern
timerSwitchA = 0;
timerSwitchB = 0;
arrayCounter = 0;
Serial.print("CHECK PATTERN RAN");
delay(2000);
for (int y = 0; y < 7; y++) // Used to rest all array values back to 0
{
gapArray[y] = 0;
}
}