ESP32: Function&Interrupt

update: 02.07.2019

 

Function & Interrupt: Project 1.

Circuit

No.

Program

//Program: ESP32_Function_Int_01.ino
int count = 1;

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

void loop() {
dotDisplay();
delay(1000);
}

void dotDisplay() {
Serial.print(“CountCallFunction: “);
Serial.println(count);
count++;
Serial.println(“…..”);
}

Result

Press Reset and see Arduino IDE Serial Monitor

Function_Interrupt01

Description

Function_Interrupt01b


Function & Interrupt: Project 2.

Circuit

No.

Program

//Program: ESP32_Function_Int_02.ino
//Function Call with Parameters:
//1-Pass variable

int count = 1;
int x;

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

void loop() {
x = 10;
dotDisplay(x);
delay(1000);
}

void dotDisplay(int i) {
Serial.print(“CountCallFunction: “);
Serial.println(count);
count++;

while(i>0){
Serial.print(“.”);
i–;
}
Serial.println();
}

Result

Press Reset and see Arduino IDE Serial Monitor

Function_Interrupt02

Description

Function_Interrupt02b


Function & Interrupt: Project 3.

Circuit

No.

Program

//Program: ESP32_Function_Int_03.ino
//Function Call with Parameters:
//Multi-variables pass

int count = 1;
int x;
int y;

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

void loop() {
x = 2;
y = 3;
dotDisplay(x, y);
delay(1000);
}

void dotDisplay(int i, int j) {
int k = i*j;
Serial.print(“CountCallFunction: “);
Serial.println(count);
count++;

while(k>0){
Serial.print(“.”);
k–;
}
Serial.println();
}

Result

Press Reset and see Arduino IDE Serial Monitor

Function_Interrupt03

Description

Function_Interrupt03b


Function & Interrupt: Project 4.

Circuit

Function_Interrupt04

Function_Interrupt05

Program

/* Program: ESP32_Function_Int_04.ino,
* Function Call with Parameters: return a variable
* A11 -> Voltage Sensor: 100 V/3 V
* A12 -> Current Sensor: 10 A/3 V
* Creat function to estimate Power = V*I
! */

byte vMea = A11;
byte iMea = A12;

float power;

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

void loop() {

power = readSensor();
Serial.print(“Power Measure = “);
Serial.print(power);
Serial.println(” [Watt].”);
Serial.println(“”);
delay(3000);
}

float readSensor() {
//ADC:12 bits 2^12 = 4096
float Power_Estimate;
float Vq = analogRead(vMea);
float V_A = (Vq*3.3/4096);
float V = (V_A*100)/3; //Voltage Sensor: 100 V/3 V
Serial.print(“Vdigital = “);
Serial.print(Vq);
Serial.print(“=> Vanalog = “);
Serial.print(V_A);
Serial.print(“=> Vactual = “);
Serial.print(V);
Serial.println(” [V].”);

float Iq = analogRead(iMea);
float I_A = (Iq*3.3/4096);
float I = (I_A*10)/3; //Current Sensor: 10 A/3 V
Serial.print(“Idigital = “);
Serial.print(Iq);
Serial.print(“=> Ianalog = “);
Serial.print(I_A);
Serial.print(“=> Iactual = “);
Serial.print(I);
Serial.println(” [A].”);

Power_Estimate = V*I;
return Power_Estimate;
}

Result

Press Reset and see Arduino IDE Serial Monitor, and then adjusting adjustable resistors at A11 and A12.

Function_Interrupt06

Description

Function_Interrupt07.jpg


Function & Interrupt: Project 5. Polling

Circuit

Function_Interrupt08

Program

/* Program: ESP32_Function_Int_05polling.ino,
* Reading Sensor at GPIO26 in Polling Mode
* Sensor simulated by Serial Monitor, and then
* sensing signal to GPIO15
*/

byte inputSensor = 26;
byte simulateSensor = 15;
byte LED0 = 25;

char incomingByte = 0; //for incoming serial data

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

pinMode(simulateSensor, OUTPUT);
digitalWrite(simulateSensor, LOW);

pinMode(inputSensor, INPUT_PULLUP);

pinMode(LED0, OUTPUT);
digitalWrite(LED0, LOW);

}

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

if(incomingByte != 13){ //Enter Key =13;

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

if(incomingByte == ‘1’) {
digitalWrite(simulateSensor, HIGH);
Serial.println(“Sensor = HIGH”);
}
if(incomingByte == ‘0’) {
digitalWrite(simulateSensor, LOW);
Serial.println(“Sensor = LOW”);
}
}
}

//Polling here
if(digitalRead(inputSensor) == HIGH) {
digitalWrite(LED0, HIGH);
}
else {
digitalWrite(LED0, LOW);
}

}

Result

Press Reset and see Arduino IDE Serial Monitor and LED0 by typing at Serial Monitor 0 | 1.

Function_Interrupt09

Description

Looking for the program above, in Polling mode!!!

Function_Interrupt10


Function & Interrupt: Project 6. Interrupt

Circuit

Function_Interrupt11

Program

/* Program: ESP32_Function_Int_06interrupt.ino,
* Sensor at GPIO26 in Interrupt Signal
* Sensor simulated by Serial Monitor, and then
* sensing signal to GPIO15
*/

byte inputSensor = 26;
byte simulateSensor = 15;
byte LED0 = 25;
byte vChannel = A10;

char incomingByte = 0; //for incoming serial data

unsigned long previousMillis = 0;
unsigned long interval = 1000; //1 second

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

pinMode(simulateSensor, OUTPUT);
digitalWrite(simulateSensor, LOW);

pinMode(inputSensor, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
//mode: LOW | CHANGE | RISING | FALLING
attachInterrupt(digitalPinToInterrupt(inputSensor), alarm, FALLING);

pinMode(LED0, OUTPUT);
digitalWrite(LED0, LOW);
}

void loop() {
unsigned long currentMillis = millis();
if(currentMillis – previousMillis >= interval) {
int vA = analogRead(vChannel);
Serial.print(“Sensor = “);
Serial.println(vA);
Serial.println(“”);
previousMillis = currentMillis;
}

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

if(incomingByte != 13){ //Enter Key =13;

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

if(incomingByte == ‘1’) {
digitalWrite(simulateSensor, HIGH);
Serial.println(“Sensor = HIGH”);
}
if(incomingByte == ‘0’) {
digitalWrite(simulateSensor, LOW);
Serial.println(“Sensor = LOW”);
}
}
}
}

/* interrupt function toggle the LED0 */
void alarm() {
digitalWrite(LED0, !digitalRead(LED0));
}

Result

Press Reset and see Arduino IDE Serial Monitor and LED0 by typing at Serial Monitor 0 | 1 and adjust resistor at A10.

Function_Interrupt12.jpg

Description

Function_Interrupt13.jpg