Teaching Microcontroller

Project Interrupt:

// ESP32_Interrupt_1polling.ino
/* Connections:
[ESP32 GIO0 – Switch ./. – GND]
*/

byte ledPin = 16; //LED_BUILTIN
byte SW0 = 0; //Switch Pin
byte state = HIGH;

unsigned long previousMillis = 0;

unsigned long interval = 1000; //1 second

void setup() {
Serial.begin(115200);
Serial.println(“.”);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, state);

pinMode(SW0, INPUT_PULLUP);

}

void loop() {
//Polling
bool SW_State = digitalRead(SW0);

if (SW_State == HIGH) {
state = HIGH;
} else {
state = LOW;
}

digitalWrite(ledPin, state);

unsigned long currentMillis = millis();
if(currentMillis – previousMillis >= interval) {
Serial.print(“LED state: “);
Serial.println(state);
previousMillis = currentMillis;
}
}

// ############# END  ESP32_Interrupt_1polling.ino

// ESP32_Interrupt_2simple.ino

byte ledPin = 16; //LED_BUILTIN
byte interruptPin = 0;
byte state = HIGH;

unsigned long previousMillis = 0;

unsigned long interval = 1000; //1 second

void setup() {
Serial.begin(115200);
Serial.println(“.”);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, state);

pinMode(interruptPin, INPUT_PULLUP);

//attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
//mode: LOW | CHANGE | RISING | FALLING
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, RISING);
}

void loop() {
unsigned long currentMillis = millis();
if(currentMillis – previousMillis >= interval) {
Serial.print(“LED state: “);
Serial.println(state);
previousMillis = currentMillis;
}
}

/* interrupt function toggle the LED */
void blink() {
state = !state;
digitalWrite(ledPin, state);
}

// ############# END ESP32_Interrupt_2simple.ino

// ESP32_Interrupt_3SerialControl.ino
/* Connections:
[ESP32 GIO26 – GIO0]
[ESP32 GIO2 – LED – GND]
*/

byte ledPin = 2; //LED
byte ControlPin = 26; //Sensor simulate
byte interruptPin = 0;
byte state = LOW;
int incomingByte = 0; //for incoming serial data

unsigned long previousMillis = 0;

unsigned long interval = 1000; //1 second

void setup() {
Serial.begin(115200);
Serial.println(“.”);

pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, state);
pinMode(ControlPin, OUTPUT);

pinMode(interruptPin, INPUT_PULLUP);

//attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
//mode: LOW | CHANGE | RISING | FALLING
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);

if(incomingByte == ‘1’) {
digitalWrite(ControlPin, HIGH);
}
if(incomingByte == ‘0’) {
digitalWrite(ControlPin, LOW);
}
}

// unsigned long currentMillis = millis();
// if(currentMillis – previousMillis >= interval) {
// Serial.print(“LED state: “);
// Serial.println(state);
// previousMillis = currentMillis;
// }
}

/* interrupt function toggle the LED */
void blink() {
state = !state;
digitalWrite(ledPin, state);
}

// ############# END ESP32_Interrupt_3SerialControl.ino