-->
Page 1 of 1

Trouble Printing boolean values

PostPosted: Thu Oct 18, 2018 2:53 pm
by SupDoc
I was trying to learn more about Interrupts, so I down loaded the simple Sketch, with the intention of playing with it by making some changes. So I added the print statements for diagnostics. To my surprise I am not printing the Boolean values for pins 13 & 2. (Yes I know I should have debounced the switch - not the issue here.)

Here is my code:

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
delay(5000);
Serial.print("state = ");
Serial.println(state);
digitalWrite(ledPin, state);
}

void blink() {
Serial.print(" interruptPin = ");
Serial.println(interruptPin);
Serial.print("ledPin = ");
Serial.println(ledPin);
state = !state;
}

Here is my serial monitor output:

state = 0
state = 0
interruptPin = 2
ledPin = 13
state = 1
interruptPin = 2
ledPin = 13
state = 0
state = 0
state = 0

Can someone tell me how I can print the Boolean values for Pins 13 and 2 instead of the pin numbers.

Re: Trouble Printing boolean values

PostPosted: Thu Oct 18, 2018 4:05 pm
by btidey
If by boolean values you mean the value at the GPIO pin then you need to use digitalRead(pin) to get that. That works even on pins set to output mode.

Serial.print(ledPin); will do exactly what is supposed to do, i.e. print the value of the variable ledPin which is 13.

Note that interrupt routines must be kept simple and quick to execute else they risk disrupting normal operation. They should normally be declared with void ICACHE_FLASH_ATTR isr() as this ensures the code is cached in fast memory. Also it is not a good idea to include Serial.print statements in the isr. They take a long time to execute.