Last edited by Moti Barski on Sun Apr 30, 2023 5:20 am; edited 2 times in total
Code:
#ifndef LED_H
#define LED_H
#endif
Code:
class code skeleton:
#ifndef LED_H
#define LED_H
class Led{
private:
public:
};
#endif
Code:
#ifndef LED_H
#define LED_H
// using Arduino hardware codes outside main:
#include <Arduino.h>
class Led{
private:
byte pin;
public:
Led(){this->pin = 13;}
Led(byte pin){
this->pin = pin;
}
void init(){
pinMode(pin, OUTPUT);
}
void on(){
digitalWrite(pin, HIGH);
}
void off(){
digitalWrite(pin, LOW);
}
};
#endif
Code:
#define LED_PIN 13
// import the C++ file with the example Led class:
#include "Led.h"
Led led1(LED_PIN);
void setup()
{
led1.init();
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
led1.on();
delay(1000); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
led1.off();
delay(1000); // Wait for 1000 millisecond(s)
}
Code:
#ifndef LED_H
#define LED_H
#include <Arduino.h>
class Led
{
private:
byte pin;
public:
Led() {} // do not use
Led(byte pin);
// init the pin for the LED
// call this in setup()
void init();
void init(byte defaultState);
// power on the LED
void on();
// power off the LED
void off();
};
#endif
Code:
#include "Led.h"
Led::Led(byte pin)
{
this->pin = pin;
}
void Led::init()
{
pinMode(pin, OUTPUT);
}
void Led::init(byte defaultState)
{
init();
if (defaultState == HIGH) {
on();
}
else {
off();
}
}
void Led::on()
{
digitalWrite(pin, HIGH);
}
void Led::off()
{
digitalWrite(pin, LOW);
}
Code:
class PushButton
{
private:
byte pin;
byte state;
bool isPullUp;
bool internalPullUpActivated;
unsigned long lastTimeStateChanged;
unsigned long debounceDelay;
bool btnWasReleased = true;
public:
PushButton() {} // do not use
PushButton(byte pin, bool isPullUp, bool internalPullUpActivated)
{
this->pin = pin;
this->isPullUp = isPullUp;
this->internalPullUpActivated = internalPullUpActivated;
lastTimeStateChanged = millis();
debounceDelay = 50;
}
void init(){
if (isPullUp && internalPullUpActivated) {
// use built in pull up resistor
// and its state is read via the pushBtn 2nd connector
pinMode(pin, INPUT_PULLUP);
}
else {
// btn is connected to an actual resistor
// and its state is read via the pushBtn 3rd connector
pinMode(pin, INPUT);
}
state = digitalRead(pin);
}
void readState()
{
unsigned long timeNow = millis();
if (timeNow - lastTimeStateChanged > debounceDelay) {
byte newState = digitalRead(pin);
if (newState != state) {
state = newState;
lastTimeStateChanged = timeNow;
}
}
}
bool isPressed(){
readState();
if (isPullUp) {
return (state == LOW);
}
else {
return (state == HIGH);
}
}
bool isEngaged()/*returns true once per press*/{
bool temp = isPressed();
bool temp2 = btnWasReleased;
btnWasReleased = not temp;
return temp && temp2;
}
};
// global variables
#define BUTTON_PIN 2
PushButton button(BUTTON_PIN, true, true);
void setup() {
Serial.begin(9600);
button.init();
}
void loop() {
if (button.isEngaged()) {
Serial.println("Button is pressed");
}
else {
Serial.println("Button is not pressed");
}
delay(100);
}
Code:
#ifndef LED_BLINKER_H
#define LED_BLINKER_H
#include <Arduino.h>
#include "Led.h"
class LedBlinker
{
private:
Led led;
unsigned long lastTimeBlinked;
unsigned long blinkDelay;
void toggleLed();
public:
LedBlinker() {} // the default c'tor, no need to use in this case
LedBlinker(Led &led); // & is added for byref params instead of by val
LedBlinker(Led &led, unsigned long blinkDelay);
void initLed();
void update();
unsigned long getBlinkDelay();
void setBlinkDelay(unsigned long blinkDelay);
};
#endif
Code:
#include "LedBlinker.h"
LedBlinker::LedBlinker(Led &led)
{
this->led = led;
lastTimeBlinked = millis();
blinkDelay = 500;
}
LedBlinker::LedBlinker(Led &led, unsigned long blinkDelay)
{
this->led = led;
lastTimeBlinked = millis();
this->blinkDelay = blinkDelay;
}
void LedBlinker::initLed()
{
led.init();
}
void LedBlinker::toggleLed()
{
led.toggle();
}
void LedBlinker::update()
{
unsigned long timeNow = millis();
// use millis instead of delay to run a command per time interval:
if (timeNow - lastTimeBlinked > blinkDelay) {
lastTimeBlinked = timeNow;
toggleLed();
}
}
unsigned long LedBlinker::getBlinkDelay()
{
return blinkDelay;
}
void LedBlinker::setBlinkDelay(unsigned long blinkDelay)
{
this->blinkDelay = blinkDelay;
}
Code:
enum blinkStates {
BLINK_DIS, // blink disable
BLINK_EN, // blink enable
LED_ON, // we want the led to be on for interval
LED_OFF // we want the led to be off for interval
};
Serial.begin(9600);
enum blinkStates ledState;
ledState = LED_OFF;
Serial.println(ledState); // 3
Code:
class A {
public:
void f() { Serial.println("A"); }
void f2() { Serial.println("C"); }
};
class B : public A {
public:
void f() { Serial.println("B"); }
};
void setup() {
A obj; // base-class pointer
Serial.begin(115200);
obj.f(); //A
B obj2;
obj2.f2(); //C
obj2.f(); //B
}
void loop() {
delay(100);
}