Hi,
A simple code that works fine on the old arduino nano is not working on the nano 33 ble. It seems to be a kind of problem with the interrupt pins.
I'm working with the pololu magnetic encoder attached to the digital 2 and 3 pins.
Anyone could help?
#define ENCESQ 2
#define ENCDIR 3
long PULSOESQ = 0;
long PULSODIR = 0;
void encoderesq()
{
PULSOESQ++;
Serial.println(PULSOESQ);
}
void encoderdir()
{
PULSODIR++;
Serial.println(PULSODIR);
}
void setup() {
Serial.begin(9600);
pinMode(ENCESQ, INPUT_PULLUP);
digitalWrite(ENCESQ, HIGH); //turn pullup resistor on
attachInterrupt(digitalPinToInterrupt(ENCESQ), encoderesq, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENCDIR), encoderdir, CHANGE);
}
void loop() {
}
I guess mbedOS does not like you to use the Serial.print in the ISRs. Its a bad idea anyways, ISR should be as short as possible, especially with an OS. Try this.
#define ENCESQ 2
#define ENCDIR 3
long PULSOESQ = 0;
long PULSODIR = 0;
void encoderesq()
{
PULSOESQ++;
}
void encoderdir()
{
PULSODIR++;
}
void setup()
{
Serial.begin( 9600 );
while ( !Serial );
Serial.println( "Pin Interrupt Demo" );
pinMode( ENCESQ, INPUT_PULLUP );
pinMode( ENCDIR, INPUT_PULLUP );
//digitalWrite(ENCESQ, HIGH); //turn pullup resistor on
attachInterrupt( digitalPinToInterrupt( ENCESQ ), encoderesq, CHANGE );
attachInterrupt( digitalPinToInterrupt( ENCDIR ), encoderdir, CHANGE );
}
void loop()
{
Serial.print( "PULSOESQ: " );
Serial.println( PULSOESQ );
Serial.print( "PULSODIR: " );
Serial.println( PULSODIR );
delay( 1000 );
}
Additionally, I do not think you need to do the digitalWrite to enable the pull ups. I just used a button module I have that has a pull up on board. So, you need to test this with your setup.