Published

# C and P's Automatic Blinds

Curtains are such a hassle to move. Here is an easy way to save yourself all that energy.

BeginnerFull instructions provided1,564

## Things used in this project

### Hardware components

 Particle Photon
×2
 SparkFun Stepper motor driver board A4988
×1
×2
 OpenBuilds NEMA 17 Stepper Motor
×1
 SparkFun Makey Makey - Standard Kit
×1

### Software apps and online services

 Particle Pi

### Hand tools and fabrication machines

 Soldering iron (generic)

## Code

### Patricle 1

C/C++
Uses the digital pins 4 and 5 to drive the stepper motor from the driver. The eqution to calculate steps on line 7 is talked about in the story. The open and close functions get called on by Particle 2, the third part of the subscribe code has PRIVATE to protect our particles.
```int DIR=D4;                                       // logic input pin to direction of driver
int STEP=D5;                                    // logic input pin to step of driver                                 // 1 for open, 0 for close

//float LinearVelocity=1;                              // input velocity of belt, m/s
float LinearDistance=900;                          // input distance in mm
//float DELAY=1/(9.455*LinearVelocity);               // calculation of delay in ms
float Steps=LinearDistance*(200/42.34866);          // calculation of steps needed for linear motion of belt

int i;

void setup() {
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);

Particle.subscribe("CandPsAutoCurtains_Open",Open,"PRIVATE");
Particle.subscribe("CandPsAutoCurtains_Close",Close,"PRIVATE");

}

void Close(const char *event, const char *data) {

digitalWrite(DIR, LOW);          // Low will close the curtains
for (i = 0; i < Steps; i++)  {
digitalWrite(STEP, HIGH);
delay(1);
digitalWrite(STEP, LOW);
delay(1);
}
}

void Open(const char *event, const char *data) {

digitalWrite(DIR, HIGH);          // HIGH will open
for (i = 0; i < Steps; i++)  {
digitalWrite(STEP, HIGH);
delay(1);
digitalWrite(STEP, LOW);
delay(1);
}
}
```

### Particle 2

C/C++
This code detects the signal from the sound sensor. Line 12 is the digitalRead, the integer Action is defined at this point. The "Big Sound" Do pin is always outputting a HIGH signal, once triggered it outputs a LOW, making the code go into the first if statement on line 17. The first action that is made in the if statement is to light the LED, just so we get a visual conformation that we have detected a signal from the sound sensor. Then the program will either publish an open command, or a close command. The variables "Position" and "moved", are altered to make the command switch every time the sensor is triggered.
```int Dout=D0;
int Position=0;             //0 For closed curtains
int Action;
int moved;
int LED=D6;

void setup() {
pinMode(Dout, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, LOW);
moved=0;

if (Action==0){
digitalWrite(LED, HIGH);

if (Position==1){
Particle.publish( "CandPsAutoCurtains_Open", "Open");
Position=0;
moved=1;
}

if (Position==0 && moved==0){
Particle.publish( "CandPsAutoCurtains_Close", "Close");
Position=1;
}

delay(3000);
}

}
```

## Credits

### Patrick McKenna Guarente

1 project • 0 followers

### Christopher Roberts

1 project • 0 followers
Thanks to John Robert McAlpine V.