system
December 23, 2018, 5:16pm
#1
Hello
As usual, run out of flash memory >:(
Changed my PinMode statements to DDRB, DDRC, and DDRD statements. All fine there.
I have Googled for an hour, trying to find the correct syntax to read an individual pin using PIND.
Rather than using all my digitalRead statements, I wanted to read the state of the INDIVIDUAL pins using the port manipulation command.
edit… OK
if (PIND & (1<<PD5)) {Serial.println(“LOW”);} //LOW state
This detects low state on pin D5.
How do I change this statement to detect a high state?
Thanks
Never used it. This seems to do the trick though:
void setup()
{
Serial.begin(115200);
Serial.print("Pin 3: ");
Serial.println((PIND & 8)>1);
}
void loop()
{
}
Are you sure that 1<<PD5 means LOW??
if (PIND & (1<<PD5)) {Serial.println("LOW");} //LOW state
That will print LOW when pin D5 (digital pin 5) is HIGH, probably not what you want.
To detect LOW, use
if (PIND & (1<<PD5) == 0) {Serial.println("LOW");} //LOW state
or
if (!(PIND & (1<<PD5))) {Serial.println("LOW");} //LOW state
system
December 23, 2018, 6:28pm
#5
Or:
Serial.println(bitRead(PIND,PD5) ? "HIGH" : "LOW");
jremington:
To detect LOW, use
if (PIND & (1<<PD5) == 0) {Serial.println("LOW");} //LOW state
This will not work, == has higher precedence than &.
Needs mode ().
if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");} //LOW state
MarkT
December 23, 2018, 9:39pm
#7
jremington:
To detect LOW, use
if (PIND & (1<<PD5) == 0) {Serial.println("LOW");} //LOW state
No, no, you clearly haven’t used C much! ‘&’ binds less tightly than ‘==’, you must use
parantheses:
if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");} //LOW state
[/quote]
Its one of the many quirks (ie mistakes) in the design of C that ‘&’, ‘|’, ‘^’ have completely
dumb precedences.
system
December 24, 2018, 7:57am
#8
:o :o :o
Eh...
OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers
system
December 24, 2018, 8:06am
#9
Eh...
OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers
Steve-SFX:
Eh…
OK, I will have to look into this further. I thought it was a simple command.
That is the trouble now with Google, you find all kinds of right and wrong answers
if (PIND & (1<<PD5) == 0) {Serial.println(“LOW”);} //LOW state
This doesn’t appear to work.
See comment in reply #5 . Extra set of parenthesis should do
if ((PIND & (1<<PD5)) == 0) {Serial.println("LOW");} //LOW state[/i]
Rather have too many than too little
system
December 24, 2018, 8:14am
#11
if (!(PIND & (1<<PD5))) {Serial.println(“LOW”);} //LOW state
This works
if (PIND & (1<<PD5) == 0) {Serial.println(“LOW”);} //LOW state
This doesn’t
Sorry for my moment of inattention!
As you noticed, my second suggestion does work, and is the construction that I actually use:
if (!(PIND & (1<<PD5))) {Serial.println("LOW");} //LOW state