I think your problem is that you cannot read the digital value that you previously
sent to pin 13. You always read a low level since an LED is connected to ground.
Solution: Have a copy of the logic level you send to pin 13 stored in a variable.
This variable I name mem
in my code below. You don't need to read pin 13
because now you have a copy in mem
. This copy you invert before sending it to pin 13
as you have done in your code example.
Your problem is usually described as a warning when performing read-modify-write operations on port pins—refer to the datasheet taken from the microcontroller manufacturer.
int pinnum = 13;
int mem = 0;
void setup() {
pinMode(pinnum, OUTPUT); // use on-board LED
}
void loop() {
digitalWrite(pinnum, mem);
mem=!mem;
delay(1000); // wait around for 1 sec (1000 ms)
}