Skip to content

Commit

Permalink
keypads, lab1,2 minor fixes, added terminal app zip.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladVanyuk committed Apr 28, 2024
1 parent 2628aab commit 979304d
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 93 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"tomoki1207.pdf",
"kisstkondoros.vscode-gutter-preview",
"ritwickdey.liveserver",
"adpyke.codesnap",
"parthr2031.colorful-comments",
"shahilkumar.docxreader",
"mateuszchudyk.hexinspector"
Expand Down
207 changes: 115 additions & 92 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,30 +2,50 @@
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

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

const char *ssid = "WEMOS_ESP8266_Yura";
const char *password = "IoT";
const char *ssid = "AsusLyra";
const char *password = "123456qwerty";

const uint8_t LED1 = D5;
const uint8_t LED2 = LED_BUILTIN;
const uint8_t LED3 = D6;
const uint8_t btnGPIO = D7;
const uint8_t btnGPIO = D8;

typedef struct LED_STRUCT
{
uint8_t GPIO_PIN;

LED_STRUCT *nextLed;
LED_STRUCT *prevLed;
} LED_STRUCT_t;

LED_STRUCT_t *currentLedPointer = NULL;
LED_STRUCT_t LED1 = {D5, NULL, NULL};
LED_STRUCT_t LED2 = {D6, NULL, NULL};
LED_STRUCT_t LED3 = {D7, NULL, NULL};

void initLedsArray()
{
LED1.nextLed = &LED2;
LED1.prevLed = &LED3;

LED2.nextLed = &LED3;
LED2.prevLed = &LED1;

LED3.nextLed = &LED1;
LED3.prevLed = &LED2;

currentLedPointer = &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;

bool presentState;
bool lastState = LOW;
bool btnPressed = false;
bool siteBtnPressed = false;
bool msgAboutButtonSended = true;
Expand Down Expand Up @@ -190,17 +210,28 @@ 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 pinsSetup()
{
pinMode(LED1.GPIO_PIN, OUTPUT);
pinMode(LED2.GPIO_PIN, OUTPUT);
pinMode(LED3.GPIO_PIN, OUTPUT);
pinMode(btnGPIO, INPUT);
}

if (ESP_WIFI_MODE == 1) {
uint8_t initWiFi()
{
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 +241,9 @@ void initWiFi() {

Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
} else { // (ESP_WIFI_MODE == WIFI_AP)
}
else if (ESP_WIFI_MODE == 2)
{
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 @@ -220,118 +253,108 @@ void initWiFi() {
Serial.print("AP IP address: ");
Serial.println(IP);
}
else
{
WiFi.mode(WIFI_OFF);
Serial.println("Wifi of");
return -1;
}
// 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.GPIO_PIN)).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.GPIO_PIN)).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.GPIO_PIN)).c_str()); });

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

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

IRAM_ATTR void ISRbtnChange() {
buttonCounter++;

IRAM_ATTR void ISRbtnChange()
{
btnPressed = true;
}

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

if (buttonCounter > 10000) {
prevButtonCounter = 0;
buttonCounter = 0;
void btnChange()
{
if (btnPressed)
{
if (millis() - lastDebounceTime >= DEBOUNCE_DELAY)
{
lastDebounceTime = millis();
bool presentState = digitalRead(btnGPIO);
if (lastState != presentState)
{
lastState = presentState;
direction = !direction;
}
}
}
}

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();

initLedsArray();
attachInterrupt(digitalPinToInterrupt(btnGPIO), ISRbtnChange, CHANGE);
timestamp = millis();
initWiFi();
}

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++) {
currentLedsArray[i] = ledsArray[i];
}
}
}
void do_algorithm()
{
if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS)
{
timestamp = millis();

digitalWrite(currentLedPointer->prevLed->GPIO_PIN, LOW);
digitalWrite(currentLedPointer->GPIO_PIN, HIGH);
digitalWrite(currentLedPointer->prevLed->GPIO_PIN, LOW);

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

void loop() {
void loop()
{
chechSiteButton();
btnChange();
if (millis() - timestamp >= DELAY_BETWEEN_BUTTONS) {
timestamp = millis();
for (int i = 0; i < ledLen; i++) {
digitalWrite(ledsArray[i], LOW);
}
do_algorithm();
}
do_algorithm();
}
1 change: 1 addition & 0 deletions mc_labs/mc_lab_01/mc_lab1_esp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ESP8266WiFi.h"
#include "ESPAsyncWebServer.h"


// Set to true to define Relay as Normally Open (NO)
#define LED_NO false
#define BUTTON_PULLUP true
Expand Down
Binary file added mc_labs/mc_lab_02/Terminal20141030.zip
Binary file not shown.
27 changes: 27 additions & 0 deletions mc_labs/mc_lab_02/mc_lab_02.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ void setup() {
DDRG |= 1 << 5;
}

void webSerialRecvMsg(uint8_t* data, size_t len) {

WebSerial.println("Received Data");
WebSerial.printf("Millis=%lu\n", millis());

String dataStr = "";
for (int i = 0; i < len; i++) {
dataStr += char(data[i]);
}

switch (dataStr) {
case "IP":
case "ip":
WebSerial.print(F("IP address: "));
WebSerial.println(WiFi.localIP());
break;
case "freeheap":
case "heap":
WebSerial.printf("Free heap=[%u]\n", ESP.getFreeHeap());
break;
default:
WebSerial.println(dataStr);
break;
}

Serial.println(dataStr);
}

void loop() {
// read from port 0:
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions mc_labs/mc_lab_03/doc/Keypad/diagram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 200, "left": 20, "attrs": {} },
{ "type": "wokwi-membrane-keypad", "id": "keypad", "top": 142, "left": 360.8, "attrs": {"columns": "3" } },
{ "type": "wokwi-lcd1602", "id": "lcd", "top": 8, "left": 20, "attrs": {} },
{ "type": "wokwi-resistor", "id": "r1", "top": 140, "left": 220, "attrs": { "value": "220" } }
],
"connections": [
[ "uno:GND.1", "lcd:VSS", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "uno:GND.1", "lcd:K", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "uno:GND.1", "lcd:RW", "black", [ "v-51", "*", "h0", "v18" ] ],
[ "uno:5V", "lcd:VDD", "red", [ "v16", "h-16" ] ],
[ "uno:5V", "r1:2", "red", [ "v16", "h-118", "v-244", "h50" ] ],
[ "r1:1", "lcd:A", "pink", [] ],
[ "uno:12", "lcd:RS", "blue", [ "v-16", "*", "h0", "v20" ] ],
[ "uno:11", "lcd:E", "purple", [ "v-20", "*", "h0", "v20" ] ],
[ "uno:10", "lcd:D4", "green", [ "v-24", "*", "h0", "v20" ] ],
[ "uno:9", "lcd:D5", "brown", [ "v-28", "*", "h0", "v20" ] ],
[ "uno:8", "lcd:D6", "gold", [ "v-32", "*", "h0", "v20" ] ],
[ "uno:7", "lcd:D7", "gray", [ "v-36", "*", "h0", "v20" ] ],
[ "uno:A3", "keypad:C1", "brown", [ "v116", "*", "h0", "v0" ] ],
[ "uno:A2", "keypad:C2", "gray", [ "v120", "*", "h0", "v0" ] ],
[ "uno:A1", "keypad:C3", "orange", [ "v124", "*", "h0", "v0" ] ],
[ "uno:A0", "keypad:C4", "pink", [ "v128", "*", "h0", "v0" ] ],
[ "uno:5", "keypad:R1", "blue", [ "v-34", "h96", "*", "v12" ] ],
[ "uno:4", "keypad:R2", "green", [ "v-30", "h80", "*", "v16" ] ],
[ "uno:3", "keypad:R3", "purple", [ "v-26", "h64", "*", "v20" ] ],
[ "uno:2", "keypad:R4", "gold", [ "v-22", "h48", "*", "v24" ] ]
],
"dependencies": {}
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 979304d

Please sign in to comment.