Skip to content
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
14 changes: 14 additions & 0 deletions mc_labs/mc_lab_01/Kudyba_Diana_Lab_01/mc-lab1/btn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;

bool readButton() {
bool buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && lastButtonState == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
lastDebounceTime = millis();
lastButtonState = buttonState;
return true;
}
lastButtonState = buttonState;
return false;
}
56 changes: 56 additions & 0 deletions mc_labs/mc_lab_01/Kudyba_Diana_Lab_01/mc-lab1/indexHtml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const char webPage[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LED Control</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f4f4f4;
padding: 20px;
}
.container {
max-width: 400px;
margin: auto;
background: white;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
color: #333;
}
button {
background: #28a745;
color: white;
border: none;
padding: 15px;
font-size: 18px;
cursor: pointer;
margin: 10px;
width: 100%;
border-radius: 5px;
}
button:hover {
background: #218838;
}
</style>
</head>
<body>
<div class="container">
<h1>ESP8266 LED Control</h1>
<button onclick="toggleLED()">Toggle LEDs</button>
</div>

<script>
function toggleLED() {
fetch("/toggle").then(response => console.log("Toggled"));
}
</script>
</body>
</html>
)rawliteral";

67 changes: 67 additions & 0 deletions mc_labs/mc_lab_01/Kudyba_Diana_Lab_01/mc-lab1/mc-lab1.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "wifi.h"
#define BUTTON_PIN 14 // Визначаємо кнопку перед підключенням btn.h
#include "btn.h"
#include "indexHtml.h"

#define LED_PINS {0, 2, 12}

ESP8266WebServer server(80);
int leds[] = LED_PINS;
int numLeds = sizeof(leds) / sizeof(leds[0]);
bool direction = true;
int ledIndex = 0;

int ledPatterns[3][2] = {{2, 12}, {2, 0}, {0, 12}};
int patternIndex = 0;

void handleButtonClick() {
direction = !direction; // Зміна напрямку
server.send(200, "text/plain", "Button Clicked");
}

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected!");
Serial.println(WiFi.localIP());

pinMode(BUTTON_PIN, INPUT_PULLUP);
for (int i = 0; i < numLeds; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
}

server.on("/", []() { server.send(200, "text/html", webPage); });
server.on("/toggle", handleButtonClick);
server.begin();
}

void loop() {
server.handleClient();

if (readButton()) {
direction = !direction;
}

// Вимикаємо всі світлодіоди
for (int i = 0; i < numLeds; i++) {
digitalWrite(leds[i], LOW);
}

digitalWrite(ledPatterns[patternIndex][0], HIGH);
digitalWrite(ledPatterns[patternIndex][1], HIGH);

if (direction) {
patternIndex = (patternIndex + 1) % 3;
} else {
patternIndex = (patternIndex - 1 + 3) % 3;
}

delay(300);
}
2 changes: 2 additions & 0 deletions mc_labs/mc_lab_01/Kudyba_Diana_Lab_01/mc-lab1/wifi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const char* ssid = "Готель Шкло";
const char* password = "11111111";