Chickendoor lightsensor

Hi All,

i'm pretty new to all of this. But with codes find in on this forum i was almost able to create a working code.
5v DC motor
MKR wifi 1010
DHT22 sensor

All is working expect of the motor protocol.
This needs to be if its hits light higher then 200 door needs to open (motorUp)
light lower then 100 door closing (motordown)
only the motor only needs to turn 1 time event and not doing this every 5 a 10 seconds.

What is the best way of doing this?

here is my current code:

Download latest Blynk library here:

Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.

Downloads, docs, tutorials:
Sketch generator: 
Blynk community:  
Follow us:        

Blynk library is licensed under MIT license
This example code is in public domain.

This example shows how to use Arduino MKR 1010
to connect your project to Blynk.

Note: This requires WiFiNINA library
from http://librarymanager/all#WiFiNINA

Feel free to apply it to any other example. It's simple!

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <DHT.h>

int photocellPin = A0;
int l = 0;
#define MotorDown 8
#define MotorUp 9
#define BLYNK_PRINT Serial
#define DHTPIN 6
#define DHTTYPE DHT22
BlynkTimer timer;

bool light = true; // day (1) & Night (2) status

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxx";
char pass[] = "xxxxxxxx";

void sendSensor()
l = analogRead(photocellPin);
Serial.print("Light = ");

if (l > 200){
light = true;
Serial.println("status: Day");
else if (l <100){
light = false;
Serial.println("status: Night");

delay(10000); // delay 10 sec between readings

float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);

void setup()
// Debug console
pinMode(MotorDown, OUTPUT);
pinMode(MotorUp, OUTPUT);
sendSensor(); // Get a light reading and set status

Blynk.begin(auth, ssid, pass);


// Setup a function to be called every 10 second
timer.setInterval(10000L, sendSensor);

void opendoor()
digitalWrite(MotorUp, HIGH);
digitalWrite(MotorDown, LOW);
light = true;
Serial.println("door: open");
// stop motor loop
while (light == true){
digitalWrite(MotorUp, LOW);
digitalWrite(MotorDown, LOW);
Serial.println("Motor Stopped: Day");


void closedoor()
digitalWrite(MotorUp, LOW);
digitalWrite(MotorDown, HIGH);
light = false;
Serial.println("door: Closed");
// stop motor loop
while (light == false){
digitalWrite(MotorUp, LOW);
digitalWrite(MotorDown, LOW);
Serial.println("Motor Stopped: Night");

void loop()
if (light == 1){ // If the light status changes execute these
opendoor(); // Open
else if (light == 2);{
closedoor(); // Close


Hello and good moring
I did a low level flight via your sketch. I guess the usage of the delay() function is disturbing the process flow. It might be useful to think about the implementation about a FSM.

hello and welcome to the forum

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

If you don't know much about coding now you are facing the problem of not (yet) knowing how to adapt your code.

a careful build system with such a door has two feedback-sensors for the endpositions

If the motor shall run only once in the morning and only once in the evening you need to detect the stateCHANGE.

If you don't find a code that fullfills your requirements 110% there is no way around learning coding.

Arduino Programming - State change ⋆ Kasper Kamperman.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.