Salve, ho realizzato questa libreria ed ho già fatto 22 sketch con essa. Mi dite se va bene o no la struttura. si richiama con #include <lib.h> ed sta dentro alla cartella di arduino e nella sotto cartella libraries, ho fatto una cartella src e lo messa lì.
consigli?
Grazie mille e buona fine settimana.
#ifndef LIB_H
#define LIB_H
#include <Arduino.h>
#include <Servo.h>
class Led {
private:
int pin;
bool state;
public:
Led(int pin) : pin(pin) {
pinMode(pin, OUTPUT);
state = false;
}
void on() {
digitalWrite(pin, HIGH);
state = true;
}
void off() {
digitalWrite(pin, LOW);
state = false;
}
void setValue(int value) {
analogWrite(pin, value);
}
bool getState() {
return state;
}
};
class Button {
private:
int pin;
int mode;
unsigned long debounceTime;
unsigned long lastDebounceTime;
bool lastButtonState;
bool buttonState;
public:
Button(int pin, int mode) : pin(pin), mode(mode) {
pinMode(pin, mode);
debounceTime = 50;
lastButtonState = false;
buttonState = false;
// Lettura iniziale dello stato
if (mode == INPUT_PULLUP) {
lastButtonState = !digitalRead(pin);
} else {
lastButtonState = digitalRead(pin);
}
}
void setDebounceTime(unsigned long time) {
debounceTime = time;
}
bool getState() {
bool reading;
if (mode == INPUT_PULLUP) {
reading = !digitalRead(pin);
} else {
reading = digitalRead(pin);
}
if ((millis() - lastDebounceTime) > debounceTime) {
if (reading != lastButtonState) {
lastDebounceTime = millis();
buttonState = reading;
lastButtonState = reading;
}
}
return buttonState;
}
};
class Potentiometer {
private:
int pin;
int minSensor;
int maxSensor;
public:
Potentiometer(int pin, int minSensor, int maxSensor) :
pin(pin), minSensor(minSensor), maxSensor(maxSensor) {
pinMode(pin, INPUT);
}
int read() {
return analogRead(pin);
}
int readMapped(int minOutput, int maxOutput) {
return map(read(), minSensor, maxSensor, minOutput, maxOutput);
}
};
class ServoControl : public Servo {
public:
void attachServo(int pin) {
attach(pin);
}
void setAngle(int angle) {
write(angle);
}
};
class LedRgb {
private:
int redPin;
int greenPin;
int bluePin;
public:
LedRgb(int red, int green, int blue) :
redPin(red),
greenPin(green),
bluePin(blue)
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void isRed() {
setColor(255, 0, 0);
}
void isGreen() {
setColor(0, 255, 0);
}
void isBlue() {
setColor(0, 0, 255);
}
void off() {
setColor(0, 0, 0);
}
};
#endif