Simple Coding needed: home position and ON position for automated roller blind

Hi there,

I want to make an automated roller curtain for my home, but i never use arduino before, nor I’m familiar with any kind of mirocontroller or coding on any language. After researching and learning as much as i could for the past few days, i found some code online and try to replicate and edit it as much as i could, but it isnt perfect

so i need you help, to provide me with the correct coding for my little project (please :smiley: )

I’m currently using:

  • Nema 16 12vDC Stepper Motor
  • Arduino uno board (which i would switch to NodeMCU and setup with HomeAssistant if this experiment work)
  • Easydriver v4.4 board
  • 2 tactile button for control

Currently:

  • If i pressed button 1, it will spin to some position, but i can press it again and it just do the same thing, so does button 2

What i’m trying to accomplish:

  • To set up button 1 as OPEN BLIND or HOME POSITION.
  • and button 2 as CLOSE BLIND (and if possible point out which number i should change to adjust this :stuck_out_tongue: )

This is the code I use:

/*********
 * Coding done by Idayu Sabri-MYBOTIC
 * Press Button 1, 2 and 3 to see rotation direction of the stepper motor
*********/

//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN  6

//Declare variables for functions
char user_input;
int x;
int y;
int state;

void setup() {
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);
  pinMode (8, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);

  resetEDPins(); //Set step, direction, microstep and enable pins to default states 
}

//Main loop
void loop() {
  if(digitalRead (8) == LOW){
    //Button one is pressed!
    digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
    StepForwardDefault();
  }
  
  else if (digitalRead (9) == LOW) {
    //Button two is pressed!
    digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
    ReverseStepDefault();
  }


  resetEDPins();

//delay(100);

}


//Reset Easy Driver pins to default states
void resetEDPins()
{
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
}

//Default microstep mode function
void StepForwardDefault()
{
  digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
  for(x= 1; x<2000; x++)  //Loop the forward stepping enough times for motion to be visible
  {
    digitalWrite(stp,HIGH); //Trigger one step forward
    delay(1);
    digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
  Serial.println("Enter new option");
  Serial.println();
}

//Reverse default microstep mode function
void ReverseStepDefault()
{
  Serial.println("Moving in reverse at default step mode.");
  digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
  for(x= 1; x<2000; x++)  //Loop the stepping enough times for motion to be visible
  {
    digitalWrite(stp,HIGH); //Trigger one step
    delay(1);
    digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
    delay(1);
  }
  Serial.println("Enter new option");
  Serial.println();
}

Note: I remove the 3rd button and the function on the code.

Thank you in advance! :smiley:
i promise i will learn more so i wont bother anyone in the future

Image from Original Post so we don’t have to download it. See this Simple Image Guide

035d1372f91c59282ac9ae99ca8ab17f3bd1c6a2.png

…R

ysinata:

  • If i pressed button 1, it will spin to some position, but i can press it again and it just do the same thing, so does button 2

The code you have posted is designed to move the motor 2000 steps when you press a button. Your description of what happens is consistent with that. (I don't know what micro-step setting you have selected in the function resetEDPins(). IIRC the Easydriver defaults to x8 micro-stepping so the 2000 steps should equate to 250 full steps or 1.25 revolutions for a 200 step motor.)

The usual way to get a stepper motor to move to a HOME position limit switch is to get it to move one step at a time and check the switch between steps. If the switch has been triggered stop moving.

When you have established the HOME or zero position you can then tell the motor to move the required number of steps to do stuff.

...R
Stepper Motor Basics
Simple Stepper Code

Hi Robin,
Thanks for replying!

yea, the 2000 was me trying to figure out which on the code i need to change in order to change the motor rotation total.

and yea i kinda figure out the reason why the button action is repeatable is because there isn't anything on the code to stop it from doing so... thats kindof what im trying to figure out right now :smiley: do you mind helping me tell me what to change?

Thank you

Do you want a situation where the motor only moves while you press the button?

Or do you want it to go all the way to the top or bottom with a single button-press (and release)?

If you want the latter then you will probably need a microswitch at the top and the bottom so that the Arduino can know that the blind has reached the top or bottom.

Have you studied the code in my link?

...R

my thinking is to press button 1 once and the blind roll up (open)
and when i press button 2 once, the blind roll down (close)

yes i did read ur post about stepper motor, i was thinking about replicating it to learn it but i couldnt find any pic of how to wire it, since u are using different motor driver

ysinata:
my thinking is to press button 1 once and the blind roll up (open)
and when i press button 2 once, the blind roll down (close)

How do you plan to let the Arduino know that the bind is all the way open or closed?

yes i did read ur post about stepper motor, i was thinking about replicating it to learn it but i couldnt find any pic of how to wire it, since u are using different motor driver

The EasyDriver uses step and direction inputs just like the A4988 - most stepper drivers work like that.

...R

An optical sensor like the QRE113 is easy to use as the position detector. It has a very short range so you do need to control where the blind material is in relation to the sensor.

The optical sensor should not go at the bottom of the window.as the slightest wind will move the blind away from it. But at the top, where the material goes onto the roller, it cannot move much at all.