How do I add joystick control to stepper motor with Blynk?

Hardware:

  1. Arduino Uno
  2. CC3000 WiFi Shield
  3. ULN2003 Motor Driver
  4. 5V stepper motor

My goal is to use joystick control to make the motor go CW or CCW.

Current code is as follows:

// This Arduino example demonstrates bidirectional operation of a 
// 28BYJ-48, using a ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the 
// operating Frequency is 100pps. Current draw is 92mA. 
////////////////////////////////////////////////

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

// These are the interrupt and control pins for СС3000
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <BlynkSimpleCC3000.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "6e6c85ca16a9408299ee8417477ba786";

//declare variables for the motor pins
int motorPin1 = 7;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 8;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 9;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
/*
void loop(){
  if(count < countsperrev )
    clockwise();
  else if (count == countsperrev * 2)
    count = 0;
  else
    anticlockwise();
  count++;
}
*/

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

BLYNK_WRITE(V1){
  clockwise();
}

BLYNK_WRITE(V2){
  counterclockwise();
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "HooToo", "ee55dd44cc", WLAN_SEC_WPA2);
}

void loop()
{
  Blynk.run();
}

Is it possible to call the anticlockwise() and clockwise() function using the joystick or even 2 buttons?

Yes, take a look at the “Robot” project.

Are you referring to the Blynk Example Browser? I’ve never worked with virtual pins before. So, not sure how to port an existing working code to a virtual pin and run it using Blynk. At this point, I’d be happy with just a 2-button control.

Put Robot in the search box and select the first entry.

This is what I got so far. I’m still having problems with the virtual pin code:

BLYNK_WRITE(V1)
{
  while(param.asInt()>200){
    anticlockwise();
  }
}

I thought the while loop would exit when I pull the slider down, but that isn’t happening.

Full code:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


// These are the interrupt and control pins for СС3000
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <BlynkSimpleCC3000.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "6e6c85ca16a9408299ee8417477ba786";

// Your WiFi credentials.
// Choose wifi_sec from WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
char ssid[] = "HooToo";
char pass[] = "ee55dd44cc";
int wifi_sec = WLAN_SEC_WPA2;

//declare variables for the motor pins
int motorPin1 = 7;    // Blue   - 28BYJ48 pin 1
int motorPin2 = 8;    // Pink   - 28BYJ48 pin 2
int motorPin3 = 9;    // Yellow - 28BYJ48 pin 3
int motorPin4 = 2;    // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1200;  //variable to set stepper speed
int count = 0;          // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

Servo servo;

BLYNK_WRITE(V1)
{
  while(param.asInt()>200){
    anticlockwise();
  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, wifi_sec);

  servo.attach(9);
}

void loop()
{
  Blynk.run();
}

void anticlockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

It doesn’t go out of the while loop because param is populated when blinkwrite is invoqued not every time.

Set a variable instead the while and using simpletimer change the direction of the motor.