Skip to content

Add second lab #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 75 additions & 52 deletions mc_labs/mc_lab_01/Dubyk_Yura_Lab_01/Dubyk_Yura_Lab_01.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

#define ESP_WIFI_MODE 2 // WIFI_STA // WIFI_AP //WIFI_AP_STA
#define ESP_WIFI_MODE 2 // WIFI_STA // WIFI_AP //WIFI_AP_STA
#define DELAY_BETWEEN_BUTTONS 300
#define DEBOUNCE_DELAY 40

Expand All @@ -14,14 +14,13 @@ const uint8_t LED2 = LED_BUILTIN;
const uint8_t LED3 = D6;
const uint8_t btnGPIO = D7;

uint8_t ledsArray[] = { LED1, LED2, LED3 };
uint8_t reverseLedsArray[] = { LED3, LED2, LED1 };
uint8_t ledsArray[] = {LED1, LED2, LED3};
uint8_t reverseLedsArray[] = {LED3, LED2, LED1};
uint8_t currentLedsArray[3];
uint8_t ledLen = sizeof(ledsArray) / sizeof(ledsArray[0]);
uint32_t timestamp;
uint32_t lastDebounceTime = 0;
uint8_t currentLed = 0;
uint8_t prevLed;
uint32_t buttonCounter = 0;
uint32_t prevButtonCounter = 0;

Expand Down Expand Up @@ -190,17 +189,21 @@ const char index_html[] PROGMEM = R"rawliteral(
</html>
)rawliteral";

void notFound(AsyncWebServerRequest *request) {
void notFound(AsyncWebServerRequest *request)
{
request->send(404, "text/plain", "Not found");
}

void initWiFi() {
void initWiFi()
{

if (ESP_WIFI_MODE == 1) {
if (ESP_WIFI_MODE == 1)
{
WiFi.mode(WIFI_STA);
// Connect to Wi-Fi network with SSID and password
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi..");
}
Expand All @@ -210,7 +213,9 @@ void initWiFi() {

Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
} else { // (ESP_WIFI_MODE == WIFI_AP)
}
else
{ // (ESP_WIFI_MODE == WIFI_AP)
WiFi.mode(WIFI_AP);
Serial.println("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
Expand All @@ -221,115 +226,133 @@ void initWiFi() {
Serial.println(IP);
}
// Send web page to client
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", index_html);
});
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send_P(200, "text/html", index_html); });

server.on("/on_alg1", HTTP_GET, [](AsyncWebServerRequest *request) {
server.on("/on_alg1", HTTP_GET, [](AsyncWebServerRequest *request)
{
siteBtnPressed = true;
request->send(200, "text/plain", "ok");
});
request->send(200, "text/plain", "ok"); });

server.on("/off_alg1", HTTP_GET, [](AsyncWebServerRequest *request) {
server.on("/off_alg1", HTTP_GET, [](AsyncWebServerRequest *request)
{
siteBtnPressed = false;
request->send(200, "text/plain", "ok");
});
request->send(200, "text/plain", "ok"); });

server.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(digitalRead(LED1)).c_str());
});
server.on("/status_led_1", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", String(digitalRead(LED1)).c_str()); });

server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(digitalRead(LED2)).c_str());
});
server.on("/status_led_2", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", String(digitalRead(LED2)).c_str()); });

server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(digitalRead(LED3)).c_str());
});
server.on("/status_led_3", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(200, "text/plain", String(digitalRead(LED3)).c_str()); });

server.onNotFound(notFound);
server.begin();
}

void pinsSetup() {
void pinsSetup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(btnGPIO, INPUT);
}

IRAM_ATTR void ISRbtnChange() {
IRAM_ATTR void ISRbtnChange()
{
buttonCounter++;
}

void btnChange() {
if (buttonCounter > prevButtonCounter) {
if (millis() - lastDebounceTime >= DEBOUNCE_DELAY) {
void btnChange()
{
if (buttonCounter > prevButtonCounter)
{
if (millis() - lastDebounceTime >= DEBOUNCE_DELAY)
{
lastDebounceTime = millis();
prevButtonCounter = buttonCounter;
presentState = digitalRead(btnGPIO);
if (presentState == LOW) {
if (presentState == LOW)
{
btnPressed = true;
} else {
}
else
{
btnPressed = false;
}

if (buttonCounter > 10000) {
if (buttonCounter > 10000)
{
prevButtonCounter = 0;
buttonCounter = 0;
}
}
}
}

void chechSiteButton() {
if (siteBtnPressed) {
void chechSiteButton()
{
if (siteBtnPressed)
{
btnPressed = true;
msgAboutButtonSended = true;
} else if (!siteBtnPressed && msgAboutButtonSended) {
}
else if (!siteBtnPressed && msgAboutButtonSended)
{
btnPressed = false;
msgAboutButtonSended = false;
}
}

void setup() {
void setup()
{
Serial.begin(115200);
pinsSetup();
attachInterrupt(digitalPinToInterrupt(btnGPIO), ISRbtnChange, CHANGE);
timestamp = millis();
initWiFi();
}

void choose_led_array() {
if (btnPressed) {
for (uint8_t i = 0; i < ledLen; i++) {
void choose_led_array()
{
if (btnPressed)
{
for (uint8_t i = 0; i < ledLen; i++)
{
currentLedsArray[i] = reverseLedsArray[i];
}
} else {
for (uint8_t i = 0; i < ledLen; i++) {
}
else
{
for (uint8_t i = 0; i < ledLen; i++)
{
currentLedsArray[i] = ledsArray[i];
}
}
}

void do_algorithm() {
void do_algorithm()
{
choose_led_array();
if (prevLed != 999) {
prevLed = currentLed;
} else prevLed = 0;
digitalWrite(currentLedsArray[currentLed], HIGH);
currentLed++;
if (currentLed >= ledLen) {
if (currentLed >= ledLen)
{
currentLed = 0;
}
}

void loop() {
void loop()
{
chechSiteButton();
btnChange();
if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS) {
if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS)
{
timestamp = millis();
for (int i = 0; i < ledLen; i++) {
for (int i = 0; i < ledLen; i++)
{
digitalWrite(ledsArray[i], LOW);
}
do_algorithm();
Expand Down
Loading