Arduino phone controlled curtains

Hello,

I made this topic to show you my latest project. It is already funtional, but isn't completely finished.
In this video you can see the curtains move :slight_smile:

Click the image to watch -->

... or click this link -->

I'm using a L298N motor driver to control the motor and to supply 5V to the Arduino Nano (clone). This way I only need one 12V power source. With the easy to use app RoboRemoFree, I can send 's', '1' or '2' to stop, close and open the curtains. To save space I made a pcb (first time soldering :smiley: ) which contains the JY-MCU bluetooth module and the Nano.

I made a wooden box that sits on the curtain rail and it contains all the electronic components. When the curtains are operated, a yellow LED will blink.

Here is the code

//Movement

int state = 0;
int flag = 0;// make sure that you return the state only once
int enA = 6;
int in1 = 5;
int in2 = 4;

//Blinking led
const int ledPin = 11;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated
const long interval = 600;           // interval at which to blink (milliseconds)



void setup() {

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 255);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin,HIGH);
  delay(3000);
  digitalWrite(ledPin,LOW);

  Serial.begin(9600); // Default connection rate for my BT module
}

void loop() {

  //if some data is sent, read it and save it in the state variable
  if (Serial.available() > 0) {
    state = Serial.read();
    flag = 0;


  }


  // if the state is 0 the led will turn off
  if (state == 's') {
    digitalWrite(in2, LOW);
    digitalWrite(in1, LOW);
    digitalWrite(ledPin,LOW); //make sure LED is turned of when 's' is pressed (otherwise the led can stay on when state was changed from
                             // '1' or '2' to 's'

    if (flag == 0) {
      Serial.println("Curtain: stop");
      flag = 1;
    }
  }

  else if (state == '1') { //curtain moves to the right and led will blink

    digitalWrite(in2, HIGH);
    digitalWrite(in1, LOW);

    if (flag == 0) {
      Serial.println("Curtain: moving to the right");
      flag = 1;
    }



  }
  else if (state == '2') {

    digitalWrite(in2, LOW);
    digitalWrite(in1, HIGH);


    if (flag == 0) {
      Serial.println("Curtain: moving to the left");
      flag = 1;
    }
  }

  if (digitalRead(in1)!=digitalRead(in2)) {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      }
      else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
}

Next step will be also controlling the other curtains and eventually adding limit switches ( I bought some reed switches and 2 round magnets that fit on the rope, will this work? )
Please let me know what you think, and if you have ideas for an upgrade or tweaks in the code (I'm a beginner with Arduino), share them with me :smiley:

Have a nice day,

Louis