Automatic curtain elevator using stepper motor

Hi all,

I am involved onto my first project with Arduino, after doing some tutorials found in youtube and in here.

The idea of my project is to automatize the rising of a curtain if I do a clap near the device, and close the curtain if I do another clap. I am using a stepper 28BYJ-48 5V, and sound sensor KY-038.

My problem is that using stepper code and “if” condition (trigger when I clap), the stepper always go forward and automatically goes backward. Looking the serial monitor, I can see the value of the sound sensor is “blocked” in the last value when the trigger started, and I don’t know how to make the sensor read again before starting the backward movement.

int sensorAnalogPin = A0;    // Select the Arduino input pin to accept the Sound Sensor's analog output 
int analogValue = 0;         // Define variable to store the analog value coming from the Sound Sensor.


#include "Stepper.h"

#define STEPS 200 

Stepper stepper(STEPS, 8, 10, 9, 11);

void setup()
{
  Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed

stepper.setSpeed(100);
}

void loop(){
  analogValue = analogRead(sensorAnalogPin);
  Serial.println(analogValue); 

  if (analogValue>550 || analogValue<530) {  //stepper go forward when I clap
  stepper.step(1000);
  }

  if (analogValue>550 || analogValue<530) //stepper SHOULD go backward if I clap again
  {
  stepper.step(-1000);
  }
  }

I have the suspect I would need a different condition architecture, but not sure where shoud I start.

Any ideas?

Many thanks!

The way you have this coded it will move forward and backward:

  if (analogValue>550 || analogValue<530) {  //stepper go forward when I clap
    stepper.step(1000);
  }

  if (analogValue>550 || analogValue<530) //stepper SHOULD go backward if I clap again
  {
    stepper.step(-1000);
  }

Think about it…you read the analog once and then using the same conditions to move the stepper forward and backwards. What you are doing is equivalent to this:

  if (analogValue>550 || analogValue<530) {
    stepper.step(1000);
    stepper.step(-1000);
  }

What you really want is:

If I clap I want the stepper motor to move the opposite direction than it did previously

Below is a solution based on the condition you provided for the analog value:

#include "Stepper.h"
#define STEPS 200

Stepper stepper(STEPS, 8, 10, 9, 11);
const int sensorAnalogPin = A0;    // Select the Arduino input pin to accept the Sound Sensor's analog output
int analogValue = 0;         // Define variable to store the analog value coming from the Sound Sensor.
int stepsToMove = 1000;

void setup()
{
  Serial.begin(9600);               // The IDE settings for Serial Monitor/Plotter (preferred) must match this speed
  stepper.setSpeed(100);
}

void loop(){
  analogValue = analogRead(sensorAnalogPin);
  Serial.println(analogValue);

  if (analogValue > 550 || analogValue < 530) {
    stepper.step(stepsToMove );  // move stepper
    stepsToMove = -stepsToMove;  // change direction for next time
  }
}

Many thanks!! It worked perfectly, and with a very easy solution.

Thank you again for your help.

Cheers!

jocajife:
Many thanks!! It worked perfectly, and with a very easy solution.

Thank you again for your help.

Cheers!

Glad it worked out!

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