I am trying to read from an encoder and I am afraid that the digitalRead method is too slow and I am missing data.
After reading about this a bit, I found some code to read the input on a digital port the direct way. However my code is not working.
Can anyone help me out with this:
void setup() {
//Clear the bit for Pin 2
DDRB &= ~_BV(DDB2);
Serial.begin(9600);
}
void loop() {
// read the value on Pin 2
Serial.println(PINB & _BV(PB2));
}
The message I get is PB2 is not defined, anyone have any ideas?
use 0x04 for the bit pattern - bit 2 high, no need for symbolic names here,
bit 2 is bit 2.
void setup() {
//Clear the bit for Pin 2
DDRB &= ~0x04; // not needed, all pins are INPUT at reset.
Serial.begin(9600);
}
void loop() {
// read the value on Pin 2
Serial.println(PINB & 0x04);
}
If you do want to name the pin, give it a meaningful name like
#define encoder_pin 2
#define encoder_mask 0x04
You can use binary constants, ie 0b00000100 instead of 0x04,
but try not to, they are usually sources of error since 5 zeroes looks the same as 6
zeroes really, hexadecimal is much more human friendly. You can also define
masks in terms of the bit number like (1<<2)
Nothing except that its just bit 2, no different from PORTC2, PORTD2, no need
to name bit 2 (except perhaps BIT2). Symbollic names are to convey meaning
where that is not obvious from context - here the context is obvious.
If you are on linux the cause of your problem (no Pxn stuff defined) is that the portpins.h (avrlibc) file in the linux tarball is broken. You can download the avrlibc source tarball and get a good copy from that.