I apologize in advance if I'm not specific enough. I have a chunk of the code my project needs figured out, there's just some parts that are escaping me.
I want to send a digital signal out on one of 8 output pins, then scan input pins, expecting results in a fashion similar to:
HIGH LOW LOW LOW LOW LOW LOW LOW (signal on pin 1)
After the program has cycled through all 8 output pins, I will have a pattern of 64 (if my sleep deprived, over caffeinated math is correct) HIGH/LOW readings. Here's what I would like to be able to do:
Store each HIGH/LOW reading in a single array (we'll call it "test"), and then compare this array to one of 3 or 4 (maybe more) pre determined arrays and generate a pass/fail indication, then clear the "test" array with a reset buttin which would cause the program to restart.
There is more to my program, and I'll add that to my thread as needed, but this is the main portion that I will need help with.
AWOL:
If you've only got eight pins, why do you need an array?
Wouldn't a single byte work just as well?
Let's say that output pin 1 goes HIGH, each of the 8 input pins can be HIGH or LOW. Multiply that by 8 pins and you'd end up with something like this (random in this case as it's just an example)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 1)
HIGH LOW LOW HIGH LOW LOW LOW LOW (pin 2)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 3)
HIGH LOW LOW LOW HIGH LOW LOW LOW (pin 4)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 5)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 6)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 7)
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 8)
I wanted to compare this to an array where :
HIGH LOW LOW LOW LOW LOW LOW LOW (pin 1)
LOW HIGH LOW LOW LOW LOW LOW LOW (pin 2)
And so on...
Looks like might be able to adapt it for my needs and it should compact program size a bit from the way I was originally doing it. Each pin would result in a byte representing the state of each of the 8 pinput pins. AWOL, looks like you were right. If each output pin, and the switch states for each input pin (8 input states for each output state) results in a case, could I do an if/then type statement combining them (if case1, case2...case 8, then the test passes as X)?
// this code assumes that pull-down resistors are used - invert the state if using pull-ups.
int pins[] = {2,3,5,7,8,9}; // array holding the pins connected to switches
int number_of_pins = sizeof(pins)/ sizeof(pins[0]); // determines how many pins in the above array
int switchStates = 0; // this will hold a bitmask of switch states
void setup()
{
Serial.begin(9600);
}
void loop()
{
// save the switch states as bits in the variable switchStates
for(int i=0; i < number_of_pins; i++)
{
// set bits in switchState to represent the state of the switches
int state = digitalRead( pins[i]);
bitWrite(switchStates, i, state);
}
switch (switchStates)
{
// test for some combinations of switches we care about
case B000001: Serial.println("the first switch (pin 2) is pressed"); break;
case B000101: Serial.println("switches on pins 2 and 5 are pressed"); break;
case B111111: Serial.println("all switches are pressed"); break;
}
showFlags();
delay(1000);
}
// reports flags that are set to see how the switchStates variable is used
void showFlags()
{
Serial.println(switchStates, BIN);
for(int sw = 0; sw < number_of_pins; sw++)
{
Serial.print("Switch ");
Serial.print(sw);
if( bitRead(switchStates, sw) == true)
Serial.println(" is pressed ");
else
Serial.println(" is not pressed");
}
Serial.println();
}
You've got eight bits of data.
Eight bits allows 256 unique combinations, so an array of 32 bytes represents the 256 combinations, with each bit representing PASS/FAIL.
Your eight input bits reference a single bit in the array,and the bit represents either PASS or FAIL.
Simple arithmetic will give you which byte/bit to access.