#define BLYNK_TEMPLATE_ID "TMPL6X0dNyxfX"
#define BLYNK_TEMPLATE_NAME "Rain Amount"
#define BLYNK_AUTH_TOKEN "rGMdTMxqpfC2oJpTWNYrwN-bzBS1RFee"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define LIGHT_SENSOR_PIN 34 // ESP32 pin GIOP36 (ADC0)
//stepper pin
int directionPin = 2;
int stepPin = 4;
int numberOfSteps = 200; // full rotion = 200 steps
int millisbetweenSteps = 10; // milliseconds - or try 100 for slower steps
int n = 0;
char ssid[] = "Wokwi-GUEST"; //wifi user name
char pass[] = ""; //wifi password
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
}
void loop() {
// reads the input on analog pin (value between 0 and 4095)
int sunlight = analogRead(LIGHT_SENSOR_PIN);
Blynk.virtualWrite(V0, sunlight); // sending sensor values to Blynk cloud
Serial.print("Analog Value = ");
Serial.print(sunlight); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (sunlight < 40) {
Serial.println(" => Dark");
openRoof();
} else if (sunlight < 800) {
Serial.println(" => Dim");
openRoof();
} else if (sunlight < 2000) {
Serial.println(" => Light");
openRoof();
} else if (sunlight < 3200) {
Serial.println(" => Bright");
closeRoof();
} else {
Serial.println(" => Very bright");
closeRoof();
}
delay(500);
}
void openRoof(){
digitalWrite(directionPin, HIGH);
if (n != 200){
for(;n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
}
}
}
void closeRoof(){
digitalWrite(directionPin, LOW);
if(n != 0){
for( ;n > 0; n--) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
}
}
}