/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/786e64ec-4e76-4125-a455-be548bb760aa
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
CloudLight led;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
const int servoPin = 18;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
#define led_pin 23
byte status = 0;
int ledbar[]={13,12,14,27,26,25,33,32,15,2};
Servo servo;
int l_pos=0;
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
if (status == 1){
LCD.clear();
LCD.setCursor(0, 0);
LCD.println(&timeinfo, "L.Up: %d/%m/%Y");
LCD.setCursor(0, 1);
LCD.println("Led acik");
}
else if(status == 2){
LCD.clear();
LCD.setCursor(0, 0);
LCD.println(&timeinfo, "L.Up: %d/%m/%Y");
LCD.setCursor(0, 1);
LCD.println("Led kapali");
}
else{
LCD.setCursor(0, 0);
LCD.println(&timeinfo, "L.Up: %d/%m/%Y");
LCD.setCursor(0, 1);
LCD.println("drm Guncelleniyor");
}
}
void led_bar_write(){
for (int i = 0; i < (sizeof(ledbar)/sizeof(ledbar[0])); i++) {
digitalWrite(ledbar[i], HIGH);
}
for (int i = 0; i < slider/18; i++) {
digitalWrite(ledbar[i], LOW);
}
}
void setup() {
// Initialize serial and wait for port to open:
for (int i = 0; i < sizeof(ledbar); i++) {
pinMode(ledbar[i], OUTPUT);
}
led_bar_write();
pinMode(led_pin, OUTPUT);
pinMode(servoPin, OUTPUT);
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
servo.attach(servoPin, 500, 2400);
}
void loop() {
ArduinoCloud.update();
//printLocalTime();
delay(250);
}
/*
Since Led is READ_WRITE variable, onLedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLedChange() {
// Add your code here to act upon Led change
if (led == true) {
status = 1;
digitalWrite(led_pin, HIGH);
}
else{
status = 2;
digitalWrite(led_pin, LOW);
}
printLocalTime();
}
void onSliderChange() {
led_bar_write();
LCD.clear();
LCD.setCursor(0, 0);
LCD.print(sizeof(ledbar)/sizeof(ledbar[0]));
LCD.setCursor(0, 1);
LCD.print(slider);
if (l_pos < slider) {
for (int pos = l_pos; pos <= slider; pos += 1) {
servo.write(pos);
delay(15);
}
}
if (l_pos > slider) {
for (int pos = l_pos; pos >= slider; pos -= 1) {
servo.write(pos);
delay(15);
}
}
l_pos = slider;
}