Page 5 of 16
Page 98
void ISR(void){
digitalWrite(2, !digitalRead(2));
}
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
}
void loop() {}
Page 99 Note Change GPIO 16 to GPIO 18 for the NANO ESP32
void IRAM_ATTR ISR(void) {
digitalWrite(2, !digitalRead(2));
}
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
pinMode(16, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(16), ISR, CHANGE);
}
void loop() {}
Page 101
volatile unsigned long t = 0;
volatile int state = 0;
unsigned long tick_us(unsigned long offset) {
return micros() + offset;
}
void ISR(void) {
t = tick_us(-t);
state++;
digitalWrite(2, !digitalRead(2));
}
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
Serial.begin(9600);
}
void loop() {
while (state != 2) {};
detachInterrupt(digitalPinToInterrupt(4));
Serial.print(t);
Serial.print(" ");
Serial.println(state);
state = 0;
t = 0;
delay(1000);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
}
Page 102
volatile char data[3];
void ISR(void) {
for (int i = 0; i < 3; i++) data[i] = 'A';
}
void setup() {
pinMode(4, INPUT);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 3; i++) data[i] = 'B';
if (data[0] != data[1] || data[1] != data[2]
|| data[2] != data[0]) {
Serial.print(data[0]);
Serial.print(data[1]);
Serial.println(data[2]);
}
}
Page 103
volatile char data[3];
void ISR(void) {
for (int i = 0; i < 3; i++) data[i] = 'A';
}
void setup() {
pinMode(4, INPUT);
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
Serial.begin(9600);
}
void loop() {
detachInterrupt(digitalPinToInterrupt(4));
for (int i = 0; i < 3; i++) data[i] = 'B';
if (data[0] != data[1] || data[1] != data[2]
|| data[2] != data[0]) {
Serial.print(data[0]);
Serial.print(data[1]);
Serial.println(data[2]);
}
attachInterrupt(digitalPinToInterrupt(4), ISR, CHANGE);
}
Page 104
Note NANO ESP32 currently uses a different timerBegin
that demands that you set the timer number, divider and count up
hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp)
To make the program work change the line timer = timerBegin(1221); to read
timer = timerBegin(0,65535,true);
hw_timer_t *timer;
uint64_t count;
void setup() {
timer = timerBegin(1221);
Serial.begin(9600);
}
void loop() {
do {
count = timerRead(timer);
} while (count < 1000);
timerStop(timer);
Serial.println(count);
timerStart(timer);
}
Page 105
Note NANO ESP32 currently uses a different timerBegin
that demands that you set the timer number, divider and count up
hw_timer_t * timerBegin(uint8_t num, uint16_t divider, bool countUp)
To make the program work change the line return timerBegin(1000000); to read
return timerBegin(0,80,1);
hw_timer_t *timer;
hw_timer_t *tick_us_start(void) {
return timerBegin(1000000);
}
int64_t tick_us(hw_timer_t *timer, int64_t offset) {
return timerRead(timer) + offset;
}
void setup() {
timer = tick_us_start();
Serial.begin(9600);
}
void loop() {
int64_t tick = tick_us(timer, 1000000);
while (tick_us(timer, 0) < tick) {};
Serial.println(tick_us(timer, 0));
}
Page 106 Currently not supported on the NANO ESP32
hw_timer_t* timer;
void ISR(void* user_data) {
digitalWrite(2, !digitalRead(2));
}
void setup() {
pinMode(2, OUTPUT);
timer = timerBegin(0,8000,true);
timerAlarm(timer, 1000, true, 0);
timerAttachInterruptArg(timer, ISR, NULL);
}