so i try to capture the states of input pins PIND and PINB in a byte array, and i'm trying to filter the results with a switch/case. the problem i'm having is that the byte containing PIND's value is only returning 6 bits, and the byte containing PINB's value is only returning 4 bits. when i serial.print (PIND, BIN) directly, i receive all 8 bits. i don't get it... what am i doing wrong?
byte test[2];
char* returned[2];
void setup()
{
DDRD = B00001000; //set all port d to input, leaving serial ports alone.
DDRF = B00001100; //set port f to input
PORTD = PORTD | 11010011; //enable pullups on portD. PD5 missing?
PORTF = PORTF | 11110011; //ditto port F pullups. PF2, PF3 missing?
Serial.begin(9600);
}
//blah, blah, blah
void loop ()
{
test[1] = (PIND, BIN);
returned [1] = pinD();
test[2] = (PINF, BIN);
returned [2] = pinF();
Serial.println(test[2], BIN);
Serial.println(test[1], BIN);
delay(1000);
}
//blah blah blah
char* pinD ()
{
test [1] = (test[1], BIN) | B00101100; //set values of rx/tx to 0 for easier processing
switch (test[1]) //process button presses
{
case B11111111: //nothing pressed
return ("n");
break;
//...
default:
return ("?");
break;
}
}
char* pinF ()
{
test [2] |= B00001100; //set values of rx/tx to 1 for easier processing
switch (test[2]) //process button presses
{
case B11111111: //nothing pressed
return ("n");
break;
//...
default:
return ("?");
break;
}
}
tooandrew:
i was under the impression that it would store the reading of port d input's states in the first position in my byte array, in binary format
ALL data in an Arduino is stored in binary. There is no need to convert it.
The comma ',' has various uses in C/C++, one being the separation of parameters
to a function call or definition, another being separating array elements in an
array initializer list, but otherwise the comma is an operator that discards its
left hand side value after evaluating it for side-effects. So what
(PIND, BIN)
does is read the PIND register, discard that value, then return the
value of BIN, which is an enumerated or #define constant whose actual value is
uninteresting.
When you see
Serial.print (PIND, BIN) ;
You have a function call to the Serial print method passing two arguments, since the
argument list is "PIND, BIN". Do this:
Serial.print ((PIND, BIN)) ;
And you'll be using the comma operator, passing a single value, BIN, to Serial.print.