28BYJ-48 Stepper Motor curtain control Android APP

Hello world!

Yesterday i made a stepper motor control app that could only turn one way (on and off) and i said I was going to make one to control your curtains, So here it is! Can be used for multiple things of course.
The source code of the app is available just contact me @ contact@arduinosensors.nl

stepper motor curtain control

Schematic:
28byj-48

Wire everything up like the schematic and it should work!
Make sure you disconnect the HC-06 bluetooth module when uploading the code! other wise the arduino becomes confused

Parts list:
Arduino uno, I used the RobotDyn Uno, a copy with 7 analog pins!
HC-06 Bluetooth module.
28BYJ 48 Stepper motor With the ULN2003 driver board
Jumper wires set MM/FF/MF
Android phone/Tablet & the APP

Code:

// com0  STOP
// com1  step+
// com2  step-
// com3  run+ 
// com4  run- 
// comZ  STOP after Bluetooth disconnection

#include <AccelStepper.h>

#define HALFSTEP 8
#define motorPin1  8     // IN1 on ULN2003 ==> Blue   on 28BYJ-48
#define motorPin2  9     // IN2 on ULN2004 ==> Pink   on 28BYJ-48
#define motorPin3  10    // IN3 on ULN2003 ==> Yellow on 28BYJ-48
#define motorPin4  11    // IN4 on ULN2003 ==> Orange on 28BYJ-48

// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

String myString = "";

boolean validCommand = false;
boolean justRun = false;          // if True, indicates need to run at constant speed

long mySteps = 2048;              // Assuming a nominal 4096 steps per revolution

float mySpeed = 500.0;

char command;

void setup()
{
  Serial.begin(9600);
  Serial.println("Bluetooth is NOT connected");
  Serial.println("Waiting for data");
  stepper1.disableOutputs();                    // disable motor coils until needed
  stepper1.setMaxSpeed(1000.0);
}

void loop()
{
  validCommand = false;  
  
  readData();             // Read any available characters from the serial input buffer
  
  checkData();            // Check the input data string for validity and extract the command
 
  if (validCommand)       // If the command string was valid
  {
    processCommand();     // Process command character and change stepper motor behaviour
  }
  if (justRun)
  {
    stepper1.runSpeed();    // Keep running continuously at predefined speed & direction
  }
  else
  {
    stepper1.run();         // Run until previously defined endpoint is reached,
  }                         // with previously defined acceleration and final speed. 
}
//-------------------------------------------------------------------------------------------
//
// Function to read available data from serial input buffer
//
void readData()
{
  while ( Serial.available() )         // Read while there are available characters
  {
    delay(3);
    char c = Serial.read();
    myString += c;                     // build the command string
  }
  Serial.print(myString);              // Output the full command string - for debugging
}
//-------------------------------------------------------------------------------------------
//
// Function to check the input data string for validity and extract the command character
//
void checkData()
{
  // Is the input data string 4 characters long, starting with sub-string "com" ?
  if (( myString.length() == 4) && (myString.startsWith("com") )) 
  {
    validCommand = true;
    command = myString[3];             // Get the command character
    Serial.print(" command is   ");
    Serial.println(command);
  }
  else  // input data string must contain at least 1 character, or we get stuck in a loop
  if ( myString.length() > 0)
  {
    validCommand = false;
    Serial.println("   command string is invalid");
  }
  myString = "";                        // Clear the input data string for next time
}
//-------------------------------------------------------------------------------------------
//
// Function to process command character and change stepper motor behaviour
//
void processCommand()
{
  justRun = false;
  stepper1.enableOutputs();              // enable the motor coils

  switch (command)
  {
  case '0':        // Stop immediately
    {
      Serial.println("STOP immediately");
      stopNow();
      break;
    }
  case '1':        // move half turn clockwise, accelerating & decelerating
    {
      Serial.println("step clockwise");
      moveSteps(mySteps);
      break;
    }
  case '2':        // move half turn anticlockwise, accelerating & decelerating
    {
      Serial.println("step anti-clockwise");
      moveSteps(-mySteps);
      break;
    }
  case '3':        // run slow clockwise at Constant Speed, no acceleration
    {
      Serial.println("run clockwise");
      runAtSpeed(mySpeed);
      break;
    }
  case '4':        // run slow anti-clockwise at Constant Speed, no acceleration
    {
      Serial.println("run anti-clockwise");
      runAtSpeed(-mySpeed);
      break;
    }
  case 'Z':        // Bluetooth Disconnected; Stop immediately
    {
      Serial.println("Bluetooth Disconnected; STOP immediately");
      stopNow();
      break;
    }
  default:         // command character does not match range of valid commands
    Serial.print(command);
    Serial.println(" ....is not in valid command range");
    break;
  }
}
//-------------------------------------------------------------------------------------------
//
// Function to setup the stepper motor to move a defined number of steps
//
void moveSteps(long steps)
{
  stepper1.setAcceleration(10.0);
  stepper1.setSpeed(5.0);
  stepper1.setCurrentPosition(0);
  stepper1.moveTo(steps);
}
//-------------------------------------------------------------------------------------------
//
// Function to set up the stepper motor to run at a defined speed
//
void runAtSpeed(float cSpeed)
{
  stepper1.setSpeed(cSpeed); 
  justRun = true; 
}
//-------------------------------------------------------------------------------------------
//
// Function to stop the stepper motor immediately and disable the motor coils
//
void stopNow()
{
  stepper1.stop();
  stepper1.setCurrentPosition(0);
  stepper1.run();                 // stop & set commands only "active" after  a call to "run"
  stepper1.disableOutputs();      // disable the motor coils to conserve power 
}

Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *