Control a step motor

And the details :
Arduino Mega 2560, Ethernet module W5100, iPhone 6 plus and ios 8.1.3

Please format code by selecting it and pressing Ctrl+K before posting

I tried something like servo example:

char auth[] = "YourAuthToken";

#include <Stepper.h>

const int stepsPerRevolution = 200;  

Stepper myStepper(stepsPerRevolution, 8,9,10,11); 

void setup()
{
  Serial.begin(9600); 
  Blynk.begin(auth);
  
}

BLYNK_WRITE(1)
{
  myStepper.setSpeed(asInt());  **// rpm**
}

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

But in step motor code you need to put this part :

  myStepper.step(stepsPerRevolution);
  delay(500);
  myStepper.step(-stepsPerRevolution);
  delay(500);

I thing that my mistake is here, were I put this part ?

You may want to do a little homework around the web, this project and community isnt really focused on “how to” with code.

here are some links to jump start you… once you have it working without Blynk… it will be real easy to add Blynk :smile:

http://www.arduino.cc/en/reference/stepper
http://www.arduino.cc/en/Tutorial/MotorKnob

Even that, I can’t do the step motor works with Blynk. I didn’t understand very well the library and how it works :confused:

It seems that you have modified the example and not getting the parameter from Virtual Pin correctly.

// Here you state that you have a widget (e.g Slider)
// attached to Virtual Pin 1
BLYNK_WRITE(1) 
{
    vpinValue = param.asInt(); // This is the value you get from the Slider
    myStepper.setSpeed(pinValue); // Here you write this value to your motor
}

It should work. Let me know.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <BlynkEthernet.h>
#include <Stepper.h>
int pinValue=0;
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution,53,51,49,47);
char auth[] = "b52126f2528748b38d6dd65b30d22762";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);

}
BLYNK_WRITE (1){
   
pinValue=(param.asInt()); 
  myStepper.setSpeed(pinValue);
}

void loop()
{
    myStepper.step(stepsPerRevolution);
  delay(500);

  myStepper.step(-stepsPerRevolution);
  delay(500); 
  Blynk.run();

}

I tried to change the location(before and after blynk.run), tried to put in (BLYNK_WRITE), I even tried to cut off this part :

     myStepper.step(stepsPerRevolution);
  delay(500);

  myStepper.step(-stepsPerRevolution);
  delay(500); 

But with no success

Daniel, what are you trying to achieve? Change the speed of the motor with the Slider Widget? Or rotate it with the slider?

Did you have any success making it work without Blynk? It’s really hard to help you without understanding your end goal

I’m just trying to change the speed. Yes, I can do this without Blynk. The only thing I don’t get it is how can I put the Blynk in this system? How use the code?
Really thank you for your help :smile:

ahh that helps a lot… so here is a draft for you…

    **************************************************************
 * You can use this sketch as a debug tool that prints all incoming values
 * sent by a widget connected to a Virtual Pin 1 in the Blynk App.
 *
 * App dashboard setup:
 *   Slider widget (0...100) on V1
 *
 **************************************************************/

// This function will be called every time
// when App writes value to Virtual Pin 1
BLYNK_WRITE(1)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  int stepsPerRevolution = param.asInt();
}

@Spudgunman This was really helpful for me in getting started on getting bi-directional THX

1 Like

Geab, how did you do to work?

@DanielFerreira seems I was to positive to early :frowning:

I thought I could control the blinking freq of the LED on the Arduino UNO with a delay via a slider on V3

BLYNK_WRITE(3)

{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
 int timeled = param.asInt();
 digitalWrite(13, HIGH);
 delay(timeled);
 digitalWrite(13, LOW);
}

but so far I fail

this may put you back on track…

int led = 13;
int timeled = 1000;

// This function will be called every time
// when App writes value to Virtual Pin 1
BLYNK_WRITE(1)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  timeled = param.asInt();
}


void loop() {
  Blynk.run();
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(timeled);               // wait 
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(timeled);               // wait 
}

good idea @Spudgunman did modify your input to fit into what I have tried earlier

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

int timeled = 400;

SimpleTimer timer;

void setup()
{
 pinMode(13, OUTPUT);
 
  SwSerial.begin(9600);
  Blynk.begin(auth);
  timer.setInterval(1000, sendUptime);  
}

void sendUptime()
{
  Blynk.virtualWrite(5, millis()/1000);
  Blynk.virtualWrite(6, millis()/1000);
}


BLYNK_READ(5)
{
  // This command writes Arduino's uptime in seconds to Virtual Pin (5)
  Blynk.virtualWrite(5, timeled);
}



BLYNK_WRITE(3)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  int timeled = param.asInt();
  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
 //timer.setInterval(50, Blink_On);
}

the virtual pin 5 will either show the uptime of the arduino or timeled
The uptime works fine but the timeled will stay at the 400 defined in the beginning of the sketch
int timeled = 400;

whats on virtual pin 3 in the blynk app? also uncomment the log shown here to help you debug

also so people dont mess with you - you may want to remove your API key from that last post.

at least I get the value from the slider V3 read by below function and the output is displayed at v17

@Spudgunmanhow can I see the output from :// BLYNK_LOG(“Got a value: %s”, param.asStr()); I am currently connected on USb serial with Arduino UNO

Now how do I get the timeled value accessible in the sktech outside of the BLYNK_Write(3) function?

BLYNK_WRITE(3)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  int timeled = param.asInt();
  Blynk.virtualWrite(17, timeled);
}

http://www.arduino.cc/en/Reference/Scope

I don’t know about the Serial Debug I haven’t tried with out network stack yet. I saw your post here Serial Debug for Arduino UNO connected on USB what happens if you click monitor serial when this whole thing is running? will it monitor it or say device in use?

thanks for the link to Variable Scope

removing the definition of timeled as local variable should do the job

BLYNK_WRITE(3)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  //int timeled = param.asInt();
  timeled = param.asInt();
  Blynk.virtualWrite(17, timeled);
}

This seems to work now :smile:

the varaible timeled can be changed from a slider on V3 pin; the V3 slider value is shown at V17; BLYNK_READ(5) reads the value on V5 (= uptime) but writes the timeled variable to V13

and in the main loop an if statement checks timeled and in case it is above 3000 it is set to 300.

So far so good: the only thing i wanted also to learn/achieve was to change the timer during a run by a slider
eg using timeled variable timer.setInterval(timeled/2,Blink_On)

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11);
#define BLYNK_PRINT SwSerial
#include <BlynkSimpleSerial.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

int timeled = 300;

SimpleTimer timer;

void setup()
{
  pinMode(13, OUTPUT);
  SwSerial.begin(9600);
  Blynk.begin(auth);
  timer.setInterval(1000, sendUptime);
  timer.setInterval(timeled/2,Blink_On);
  timer.setInterval(timeled,Blink_Off);  
}

void sendUptime()
{
  Blynk.virtualWrite(5, millis()/1000);
  Blynk.virtualWrite(6, millis()/100);
 }

void Blink_On()
{
 digitalWrite(13, HIGH);
 }

void Blink_Off()
{
digitalWrite(13, LOW);
}


BLYNK_READ(5)
{
  // This command writes Arduino's uptime in seconds to Virtual Pin (5)
  Blynk.virtualWrite(13, timeled);
}


BLYNK_WRITE(3)
{
  // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use: asInt() and asDouble()
  timeled = param.asInt();
  Blynk.virtualWrite(17, timeled);
}



void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer
  if (timeled > 3000)
 {
  timeled = 300;// do something here
}
}