WiFi Seven Segment Timer
Pada tugas berikutnya kelompok kami membuat sebuah timer seven segment nirkabel yang menggunakan sinyal WiFi. Controller ini dapat mengatur seven segment pada board Arduino untuk berfungsi sebagai timer yang dapat dikendalikan dari jarak jauh
Komponen yang diperlukan:
- WeMos D1
- Seven Segment
Gambaran konfigurasi pin pada Arduino UNO adalah sebagai berikut:
Tantangan dalam membuat modul kali ini adalah pin GPIO pada board WeMos D1 berbeda konfigurasinya dengan Arduino UNO sehingga kami harus melakukan banyak penyesuaian. Alhamdulillah, semua pin untuk seven segment dapat terpasang dengan benar sehingga timer dapat berfungsi.
Source code beserta konfigurasi pin dapat dilihat di bawah ini:Pada tugas berikutnya saya membuat sebuah timer seven segment nirkabel yang menggunakan sinyal WiFi. Controller ini dapat mengatur seven segment pada board Arduino untuk berfungsi sebagai timer yang dapat dikendalikan dari jarak jauh
/*
* ESP8266 NodeMCU AJAX Demo
* Updates and Gets data from webpage without page refresh
* https://circuits4you.com
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
int leftnumber;
int rightnumber;
int timer = 100;
void lightUpDigit(int DisplayNumber) {
switch (DisplayNumber){
case 0:
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 1);
digitalWrite(D8, 0);
digitalWrite(D9, 0);
break;
case 1:
digitalWrite(D2, 1);
digitalWrite(D3, 1);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 1);
digitalWrite(D8, 1);
digitalWrite(D9, 1);
break;
case 2:
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 1);
digitalWrite(D5, 0);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 1);
break;
case 3:
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 1);
break;
case 4:
digitalWrite(D2, 1);
digitalWrite(D3, 1);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 0);
digitalWrite(D8, 1);
digitalWrite(D9, 0);
break;
case 5:
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 0);
break;
case 6:
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 1);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 0);
break;
case 7:
digitalWrite(D2, 1);
digitalWrite(D3, 1);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 1);
digitalWrite(D8, 0);
digitalWrite(D9, 1);
break;
case 8:
digitalWrite(D2, 0);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 0);
break;
case 9:
digitalWrite(D2, 1);
digitalWrite(D3, 0);
digitalWrite(D4, 0);
digitalWrite(D5, 0);
digitalWrite(D7, 0);
digitalWrite(D8, 0);
digitalWrite(D9, 0);
break;
}
}
void start(int a, int b)
{
digitalWrite(LED_BUILTIN, LOW);
for (int i=0; i<timer; i++){
//leftnumber
digitalWrite(D1, 1);
digitalWrite(D6, 0);
lightUpDigit(a);
delay(10);
//rightnumber
digitalWrite(D1, 0);
digitalWrite(D6, 1);
lightUpDigit(0);
delay(10);
}
for (int i = 0; i < a; i++) {
// Counts down the left digit by 3
leftnumber = a - 1 - i;
for (int x = 0; x < 10; x++) {
// Counts down the right digit.
// Since this is inside the other loop,
// it counts down by 10 five times
rightnumber = 9 - x;
for (int y = 0; y < 50; y) {
//leftnumber
digitalWrite(D1, 1);
digitalWrite(D6, 0);
lightUpDigit(leftnumber);
delay(10);
//rightnumber
digitalWrite(D1, 0);
digitalWrite(D6, 1);
lightUpDigit(rightnumber);
delay(10);
y = y + 1;
}
}
}
digitalWrite(LED_BUILTIN, HIGH);
}
const char INDEX_HTML[] =
"<!DOCTYPE html>"
"<html>"
"<body style='font-size: 5rem; font-family: Arial;' >"
"Timer <input style='font-size: 5rem; color: red;' id='timer' type='number' name='timer' min='1' max='99'>"
"<button style='padding: 10px; font-size: 2rem; background-color: green; color:white;' id onclick='sendData()'>START</button>"
"<br>"
"<br>"
"<div style="color:red;">Time left <span id="time"></span></div>"
""
"var value;"
"function startTimer(duration, display) {"
"var timer = duration, seconds;"
"setInterval(function () {"
"seconds = parseInt(timer % 60, 10);"
"seconds = seconds "
"</body>"
"</html>";
//SSID and Password of your WiFi router
const char* ssid = "Timer7segment";
const char* password = "sevensegment";
ESP8266WebServer server(80); //Server on port 80
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() { //Read HTML contents
server.send(200, "text/html", INDEX_HTML); //Send web page
}
void handleTimer() {
int timeval = 0;
int t_state = server.arg("time").toInt();
timeval = t_state;
int left= timeval/10;
int right= timeval%10;
start(left, right);
//Send web page
}
//==============================================================
// SETUP
//==============================================================
void setup(void){
// Set 7-segement outputs
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(D11, OUTPUT);
pinMode(D9, OUTPUT);
pinMode(D12, OUTPUT);
pinMode(D13, OUTPUT);
pinMode(D14, OUTPUT);
pinMode(D15, OUTPUT);
WiFi.mode(WIFI_AP); //Only Access point
WiFi.softAP(ssid, password); //Start HOTspot removing password will disable security
IPAddress myIP = WiFi.softAPIP(); //Get IP address
//Onboard LED port Direction output
pinMode(LED_BUILTIN, OUTPUT);
server.on("/", handleRoot); //Which routine to handle at root location
server.on("/setTimer", handleTimer);
server.begin(); //Start server
}
//==============================================================
// LOOP
//==============================================================
void loop(void){
server.handleClient(); //Handle client requests
}
Ketika berhasil dijalankan dan diakses akan muncul tampilan di browser seperti ini
Comments
Post a Comment