Hello-
I'm struggling to use DDRD, PORTD and PIND wiht my Arduino Micro. I have a need to toggle a couple pins simultaneously, however, I can't seem to get even 1 pin (LED_BUILTIN) to toggle using the PORTD function.
Here is a snippet of the code I'm using to test PORTD with:
void setup() {
DDRD = DDRD | B11110100; //Pins 0,1,3 are digital outputs. Pins 2,4,5,6,7 are digital inputs.
//PIN2 has an active low pushbutton attached to it. Pushing the button pulls pin2 low
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); // initialize serial communication at 9600 bits per second
}
void loop() {
PORTD = B11111100; //Set initial state of PORTD
Serial.println(PIND); // print out the state of the button
delay(5); // delay in between reads for stability
if (PIND = B11111100) {
//If the button state is High (ON), light the switch LED
digitalWrite(LED_BUILTIN, HIGH);
}
if (PIND = B11111000) {
//If button state is LOW (OFF), keep switch LED off
digitalWrite(LED_BUILTIN, LOW);
}
}
With the print statement, I see that with no button pushed, PIND value is 250. With the button pushed, the pin value is 248. I would have expected to see the PIND value = 252 based off the PORTD setting of B11111100. This is a bit awkward, since 250 = b111110100 and 248 = 11111000. Essentially PIN2/3 are toggling according to the PRINT
Via the Print statement I see the state of the pushbutton pulling PIN2 low, and than we see PIN3 go high (not sure why). I physically measure PIN3, expecting it to be HIGH during the event, but it is not. I'm not totally sure where physical pin, PIN3 is than, as I see no other pins toggling during the push-button event.
The next step is to successfully toggle pins other than the LED_BUILTIN... but want to get LED_BUILTIN going first.
Any suggestions?
Thanks!