// Stepper motor on Wokwi!
#include <Stepper.h>
// change this to fit the number of steps per revolution
// for your motor
const int stepsPerRevolution = 200;
const int stepperSpeed = 60;
const int flapOpenTime = 10000; // 10 seconds
const int loopDelay = 2000; // 2 seconds
// initialize the stepper library on pins 8 through 11
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int inputPinPir1 = 2; // choose the input pin (for PIR sensor)
int inputPinPir2 = 4; // choose the input pin (for PIR sensor)
int statePir1 = LOW; // we start, assuming no motion detected
int statePir2 = LOW; // we start, assuming no motion detected
int val1 = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status
int flap = 0;
void setup() {
// set the speed at 60 rpm
myStepper.setSpeed(stepperSpeed);
// declare sensor as input
pinMode(inputPinPir1, INPUT);
// initialize the serial port
Serial.begin(9600);
}
void loop() {
// read input value
val1 = digitalRead(inputPinPir1);
Serial.println("**********");
Serial.print("val1: ");
Serial.print(val1);
Serial.print(" val2: ");
Serial.print(val2);
Serial.print(" flap: ");
Serial.println(flap);
Serial.println("**********");
// check if the input is HIGH
if (val1 == HIGH && flap == 0) {
if (statePir1 == LOW) {
// we have just turned on
Serial.println("Motion detected inside!");
Serial.println("Flap is closed so open the flap.");
// we only want to print on the output change, not state
statePir1 = HIGH;
// step one revolution in one direction
Serial.println("clockwise inside...");
myStepper.step(stepsPerRevolution * 2);
Serial.println("Set flap status to open.");
flap = 1;
Serial.print("Leave the flap open for ");
Serial.print(flapOpenTime);
Serial.println(" milliseconds.");
delay(flapOpenTime);
Serial.println("Close the flap.");
// step one revolution in the other direction
Serial.println("counterclockwise inside...");
myStepper.step(-stepsPerRevolution * 2);
Serial.println("Set flap status to cloded.");
flap = 0;
}
} else {
if (statePir1 == HIGH) {
// we have just turned off
Serial.println("Motion ended inside!");
// we only want to print on the output change, not state
statePir1 = LOW;
}
}
// read input value
val2 = digitalRead(inputPinPir2);
Serial.println("**********");
Serial.print("val1: ");
Serial.print(val1);
Serial.print(" val2: ");
Serial.print(val2);
Serial.print(" flap: ");
Serial.println(flap);
Serial.println("**********");
// check if the input is HIGH
if (val2 == HIGH && flap == 0) {
if (statePir2 == LOW) {
// we have just turned on
Serial.println("Motion detected outside!");
Serial.println("Flap is closed so open the flap.");
// We only want to print on the output change, not state
statePir2 = HIGH;
// step one revolution in one direction
Serial.println("clockwise outside...");
myStepper.step(stepsPerRevolution * 2);
Serial.println("Set flap status to open.");
flap = 1;
Serial.print("Leave the flap open for ");
Serial.print(flapOpenTime);
Serial.println(" milliseconds.");
delay(10000);
Serial.println("Close the flap.");
// step one revolution in the other direction:
Serial.println("counterclockwise outside...");
myStepper.step(-stepsPerRevolution * 2);
Serial.println("Set flap status to cloded.");
flap = 0;
}
} else {
if (statePir2 == HIGH) {
// we have just turned of
Serial.println("Motion ended outside!");
// We only want to print on the output change, not state
statePir2 = LOW;
}
}
Serial.print("Delay loop for ");
Serial.print(loopDelay);
Serial.println(" milliseconds.");
delay(loopDelay);
}