Instructables

Automatic blind hooked up to existing projector screen

Step 9: Arduino Code

// Pins
int dir = 2;
int power = 3;
int captor = 4;

// Reed switch
int captorVal = 0;

// Analog pin to read screen DOWN
int screenDown = 0;
int screenDownVal = 0;

// Analog pin to read screen UP
int screenUp = 1;
int screenUpVal = 0;

String screenState; // Projector screen state
String blindState; // Blind state
boolean busy = false;

void setup() {

  // initialize pins
  pinMode(power, OUTPUT);  
  pinMode(dir, OUTPUT);
  pinMode(captor, INPUT);

  // Read reed switch value at setup
  delay(100);
  captorVal = digitalRead(captor);
  delay(100);

  // Set the blindState on setup
  if ( captorVal == LOW ) {
    blindState = "up";
  } else {
    blindState = "down";
  }

  // Logger
  Serial.begin(9600);
}

void loop() {

  // Read projector screen values
  screenDownVal = analogRead(screenDown);
  screenUpVal = analogRead(screenUp);

  if ( screenUpVal > 1000 && screenDownVal > 1000 ) {
      Serial.println("Screen module is OFF !");
      screenState = "off";
  } else if ( screenUpVal < 200 && screenDownVal < 200 ) {
      screenState = "stop";
  } else if ( screenUpVal > 1000 && screenDownVal < 200 ) {
      screenState = "up";
  } else if ( screenDownVal > 1000 && screenUpVal < 200 ) {
      screenState = "down";
  }

  refreshBlindMotor(screenState);
  delay(100);

}

void refreshBlindMotor() {

  Serial.println(blindState);

  // Screen is going UP and blind is DOWN ( and blind is not busy )
  if ( screenState == "up" && blindState == "down" && busy == false) {
  
    // UP the blind !
    busy = true;
    blindUp();
  
  // Screen is going DOWN and blind is UP ( and blind is not busy )
  } else if ( screenState == "down" && blindState == "up" && busy == false ) {
  
    // DOWN the blind !
    busy = true;
    blindDown();
  
  } else if ( screenState == "stop" ) {
    // TODO : stop the blind ?
  }

}


void blindUp() {

  // Motor ON + UP
  digitalWrite(power, HIGH);
  digitalWrite(dir, HIGH);

  // While reed switch not engaged
  captorVal = digitalRead(captor);
  while (captorVal == HIGH) {
    captorVal = digitalRead(captor);
    Serial.println("Waiting for the blind to reach the far UP position...");
    delay(100);
  }

  // We got out of the while loop,
  // We can stop the blind
  halt();
  blindState = "up";
  busy = false;

  Serial.println("Blind is UP !");

}

void blindDown() {

  // Motor ON + DOWN
  digitalWrite(power, HIGH);
  digitalWrite(dir, LOW);

  // Time is takes for the blind to go DOWN
  delay(18000);

  // Stop the blind
  halt();
  blindState = "down";
  busy = false;

  Serial.println("Blind is DOWN !");

}

void halt() {

  // Cut power to blind motor
  digitalWrite(power, LOW);

  Serial.println("Blind is STOPPED !");

}
 
Remove these adsRemove these ads by Signing Up
bdesigned1 year ago
Very nice instructable. I hope you will follow up with the code, I want to use this to operate the gate for my animals. Thanks.