When there are many different things going on, it may simplify coding if we can encapsulate the operation of each object with a simple command. This encapsulation is accomplished through object-oriented programming. In OO programming we create our own class to encapsulate a set of properties and behaviors. Here we will create an LED object called "Flasher". The properties are the pin number, the on time and the off time. The behavior is to turn an LED on and off. Classes are always put together like this:
Class declaration, ie class Flasher.
A list of class member variables, ie int ledPin.
A constructor public: or private:, private members can only be accessed from with the class. We'll use public constructors so that we can access the member from outside of the class.The constructor shows how variables used in "calling" the object will be used internally. Our constructor shows that the object is created using 3 variables, an int and 2 longs. The variable names used in our example are descriptive but in reality the names can be anything. Descriptive names like pin, on and off make more sense than using purple, blue and ice! Be careful however, you can't use reserved words for variables. Reserved words are functions and commands already in use in the language. We can use "on" but we can't use "ON".
One or more methods. These are where the code goes. A method defines the actions of our object. Classes may have one or more methods. Our example method is called update. It is "void" meaning it does not return any variable to the code calling it.
In our example on the left, we can easily create any number of LED objects using the constructor. Flasher led73(55, 300, 600); This creates a Flasher object called led73 connected to pin 55. It will flash for 300ms on and 600ms off.
The update method is invoked using the dot notation, led73.Update(), in the main loop to operate the Flasher object. Again, the word Update is descriptive. We could just as easily have called it the zebraShoes method. led73.zebraShoes() would work just as well - it just doesn't make any sense.
In our example on the right we can easily create any number of touch objects using the constructor. touch 73skin (12) creates a touch object called 73skin connected to pin 12. In this case the update method "returns" an integer so it is not "void". Instead, it is defined as int Update() indicating that it will "return" an integer. We need to set some variable equal to the update method call to make it work..
/* Flasher class creates a flashing LED.
* The Flasher object needs to know the pin number,
* the on time and the off time. The Update method
* operates the LED.
* M Druiven, March 10, 2021
* https://sites.google.com/view/sparboticsesp32
*/
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
previousMillis = 0;
ledState = LOW;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (millis() - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = millis(); // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (millis() - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = millis(); // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
Flasher led1(12, 300, 500);
Flasher led2(14, 350, 350);
void setup()
{
}
void loop()
{
led1.Update();
led2.Update();
}
// ESP32 Touch Test OO
// M Druiven, May 3, 2021
class touch {
int touchPin;
public:
touch(int pin) {
touchPin = pin;
}
int Update() {
return touchRead(touchPin);
}
};
touch myTouch(15);
void setup() {
Serial.begin(115200);
delay(1000); // allow time to bring up serial monitor
Serial.println("ESP32 Touch Test");
}
void loop() {
int check = myTouch.Update();
delay(500);
Serial.println(check);
}