Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/angelof-exe/esp8266-projects
https://github.com/angelof-exe/esp8266-projects
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/angelof-exe/esp8266-projects
- Owner: angelof-exe
- Created: 2023-12-21T10:15:44.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-10T10:17:51.000Z (12 months ago)
- Last Synced: 2024-02-18T18:26:25.595Z (10 months ago)
- Language: C++
- Size: 11.3 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ESP8266 Projects
Some littles projects for ESP8266 made with PlatformIO.## Appendix
Table of contents:
- [Led Button](https://github.com/Gangelo99/ESP8266-projects#led_buttoncpp)
- [Multiple_Led_Button.cpp](https://github.com/Gangelo99/ESP8266-projects/tree/main#multiple_led_buttoncpp)
- [Multipled Led Button OOP](https://github.com/Gangelo99/ESP8266-projects/tree/main#multipled-led-button-oop)## Led_Button.cpp
First project in which a led blink when a button is pressed.In this project I decided to avoid the use of the `delay()` function to start practicing through `millis()` functions.
```
#define buttonPin D1 // D1 is used for input signal
#define ledPin D2 // D2 is used for output signal
```
D1 is used for input signal, while the D2 is for output.#### How I used the millis function:
```
unsigned long debounceDelay = 100; // debounce time in milliseconds
unsigned long lastDebounceTime = 0; // last time the button state was toggled
int buttonState = LOW; // current state of the button
int lastButtonState = LOW; // previous state of the button
```
These variables are used to avoid false positives caused by rebounds of the button signal. `debounceDelay`is the debounce time, `lastDebounceTime` store the value in milliseconds from the last time the `buttonState` was toggled.```
if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonState) {
buttonState = reading; //If the buttonState change state (from ON to OFF or viceversa)
// get the new state of buttonPinif (buttonState == HIGH) { //If button is pressed
digitalWrite(ledPin, HIGH);
Serial.println("Bottone ON");
}
else { //If button is not pressed
digitalWrite(ledPin, LOW);
Serial.println("Bottone OFF");
}
}lastButtonState = reading;
}
```
`if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonState)`This if statement is executed when the difference between `millis()` _(current time of the program's execution)_ and `lastDebounceTime` is greather than `debounceDelay` and the reading state is different of the current button State _`(buttonState)`_.
## Multiple_Led_Button.cpp
Project in which buttons of certain color light the LED of that specific color.
## Multipled Led Button OOP
Improved version of the multiple_led_button.cpp with the OOP (Object Oriented Programming).### Button.h
```
class Button{
private:
byte pin;
byte state;
byte lastReading;
unsigned long debounceDelay = 100; // debounce time in milliseconds
unsigned long lastDebounceTime = 0; // last time the button state was toggledpublic:
Button(byte pin);void init();
void update();byte getState();
bool isPressed();
};
```
In the Button class I used the `millis()` function and the two variables `debounceDelay` & `lastDebounceTime` to avoid false positives caused by rebounds of the button(s) signal.
```
void Button::update(){
byte newReading = digitalRead(pin);
if(newReading != lastReading)
lastDebounceTime = millis();
if(millis() - lastDebounceTime > debounceDelay)
state = newReading;
lastReading = newReading;
}
```