need some code for a hobby project.

Hi All,

ive been struggling with the code side of this for days now and need some help. Its at the point id happily pay some one to write this for me.

A bit of history.

we live in a period house and want to automat wooden curtain poles. I thought id just buy some.... but there arnt any... ANYWHERE! So i thought id make some... HA HA!

i have learned enough fusion360 to fabricate all the parts and mounts and have the mechanical prototype up and running but when it come to control im struggling and dont in the short term have the time to learn c++. Ive been trying to cobble bits of code together from here and having some success but im getting strange errors.

The aim:

using D1 Mini and Blynk have a 5 button control on/off, open, close, jog open, jog closed.

On/Off make pin high/low to sleep pin on A4988 (apply/remove volts to motor)
Open: push button. start NEMA CW 17 motor and run until limit switch activated
Close: Push button. start NEMA CWW 17 motor and run until limit switch activated
Jog Open: push button. Jog NEMA CW 17 motor stop if limit switch activated
Jog Close: Push button. Jog NEMA CWW 17 motor stop if limit switch activated

ive bee playing with Robin2's coded and got the stepper working with blynk, but as soon as i start to mod it, it all goes pear shaped....

In the code below, ive added a pin (D4) for on off and im getting an error (attached screen grab) which is strange because earlier i had it working.....

Its really frustrating!

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

byte directionPin = D2;
byte stepPin = D3;
byte buttonOnOffpin = D4;
byte buttonCWpin = D5;
byte buttonCCWpin = D6;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonOnOffpressed = false;


unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     pinMode(buttonOnOffpin, INPUT_PULLUP);
     
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();

   curMillis = millis();
    readButtons();
    actOnButtons();
   
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
    buttonOnOffpressed = false;
   
    if (digitalRead(buttonCWpin) == LOW) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == LOW) {
        buttonCCWpressed = true;
    }
     if (digitalRead(buttonOnOffpin) == LOW) {
        buttonOnOffpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
    if (buttonOnOffpressed == true) {
        digitalWrite(buttonOnOffPin, HIGH);
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

Your actOnButtons function is missing a closing brace.

Where does the actOnButtons function end ?

If you Auto Format the code in the IDE then all function definitions should start on the left margin. In your code singleStep() function doesn't. The usual cause is a missing } at the end of the previous function

Thanks gentlemen,

I now have that working.

im going to try to add the jog buttons now.

i cant help but think that having latching buttons in the Blynk app is the best way to get the curtains to fully open/close. Would it be better to have some sort of code to make the motor run until the limit is met? That way if i move from Blynk to another app it will still work.

or am i over thinking this?

Hers some photos of the components ive designed and the setup in general (if your interested.

Well i got all of my buttons working and then i added limit switches..... and its all gone pear shaped again! i tried adding a while condition to say it would only move when the limit switch was HIGH, it kept asking for curly brackets... so i gave it them... it compiles ok but wont run properly. would appreciate it if anyone can tell me where i went wrong.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

byte limitCW = D0;
byte limitCCW = D1;
byte directionPin = D2;
byte stepPin = D3;
byte buttonOnOffpin = D4;
byte buttonCWpin = D5;
byte buttonCCWpin = D6;
byte buttonJogCWpin = D7;
byte buttonJogCCWpin = D8;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonJogCWpressed = false;
boolean buttonJogCCWpressed = false;
boolean buttonOnOffpressed = false;


unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(buttonOnOffpin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     pinMode(buttonJogCWpin, INPUT_PULLUP);
     pinMode(buttonJogCCWpin, INPUT_PULLUP);
     pinMode(limitCW, INPUT);
     pinMode(limitCCW, INPUT);
     
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();

   curMillis = millis();
    readButtons();
    actOnButtons();
   
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
    buttonJogCCWpressed = false;
    buttonJogCWpressed = false;
    buttonOnOffpressed = false;
   
    if (digitalRead(buttonCWpin) == HIGH) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == HIGH) {
        buttonCCWpressed = true;
    }
    if (digitalRead(buttonJogCWpin) == HIGH) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonJogCCWpin) == HIGH) {
        buttonCCWpressed = true;
    }
     if (digitalRead(buttonOnOffpin) == HIGH) {
        buttonOnOffpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
      while (digitalRead(limitCW) == HIGH) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
      while (digitalRead(limitCCW) == HIGH) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
    if (buttonJogCWpressed == true) {
      while (digitalRead(limitCW) == HIGH) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonJogCCWpressed == true) {
      while (digitalRead(limitCCW) == HIGH) {
        digitalWrite(directionPin, HIGH);
        singleStep();}}}}
    }
    if (buttonOnOffpressed == true) {
        digitalWrite(buttonOnOffpin, HIGH);
    }
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

Your buttons are sensibly set to input_pullup. The checks to see if they are pressed should be looking for LOW.

Thanks Bill, or is it wild? :o

i tried that before i added the while conditions and it wouldnt work correctly with the Blynk, ill try again im thinking that ill need to change the while conditions on the jog buttons to if statements

Changing from high to low made no difference

this version works, all the buttons do what i want them to. im using latching buttons for open/close which i want to change to momentary to activate a script in the arduino to home to the limit switch.

as soon as i add anything to work with the limit switches it stops working.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

byte directionPin = D2;
byte stepPin = D3;
byte buttonOnOffpin = D4;
byte buttonCWpin = D5;
byte buttonCCWpin = D6;
byte buttonJogCWpin = D7;
byte buttonJogCCWpin = D8;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
boolean buttonJogCWpressed = false;
boolean buttonJogCCWpressed = false;
boolean buttonOnOffpressed = false;


unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 1; // milliseconds

void setup()
{
  // Debug console
  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

     pinMode(directionPin, OUTPUT);
     pinMode(stepPin, OUTPUT);
     pinMode(buttonOnOffpin, OUTPUT);
     
     pinMode(buttonCWpin, INPUT_PULLUP);
     pinMode(buttonCCWpin, INPUT_PULLUP);
     pinMode(buttonJogCWpin, INPUT_PULLUP);
     pinMode(buttonJogCCWpin, INPUT_PULLUP);
     
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();

   curMillis = millis();
    readButtons();
    actOnButtons();
   
}

void readButtons() {
   
    buttonCCWpressed = false;
    buttonCWpressed = false;
    buttonJogCCWpressed = false;
    buttonJogCWpressed = false;
    buttonOnOffpressed = false;
   
    if (digitalRead(buttonCWpin) == HIGH) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonCCWpin) == HIGH) {
        buttonCCWpressed = true;
    }
    if (digitalRead(buttonJogCWpin) == HIGH) {
        buttonCWpressed = true;
    }
    if (digitalRead(buttonJogCCWpin) == HIGH) {
        buttonCCWpressed = true;
    }
     if (digitalRead(buttonOnOffpin) == HIGH) {
        buttonOnOffpressed = true;
    }
}

void actOnButtons() {
    if (buttonCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
    if (buttonJogCWpressed == true) {
        digitalWrite(directionPin, LOW);
        singleStep();
    }
    if (buttonJogCCWpressed == true) {
        digitalWrite(directionPin, HIGH);
        singleStep();
    }
    if (buttonOnOffpressed == true) {
        digitalWrite(buttonOnOffpin, HIGH);
    }
}

void singleStep() {
    if (curMillis - prevStepMillis >= millisBetweenSteps) {
            // next 2 lines changed 28 Nov 2018
        //prevStepMillis += millisBetweenSteps;
        prevStepMillis = curMillis;
        digitalWrite(stepPin, HIGH);
        digitalWrite(stepPin, LOW);
    }
}

I assume that these are physical buttons attached to the Arduino. How are they wired up?

Hi Bill, originally they were but once id got the motor turning both ways via a 2 physical button2 i removed them,

I now have 5 buttons on blynk. on/off, open, close, jog ccw & jog CW as far as they go it all works via the code in the post above.

I tried to add limit switches, but im not getting the script correct to make it work that code is also above

Format the sketch with t then examine the actOnButtons() function.

Hi all.

thanks for your responses i have someone to help me and will post again if it doesnt work out.

im out of time and need to finish a project at home before i go back to work. i need some code writing for an automated curtain pole im making.

All of the engineering is finished but im out of time and still need to learn how to code. ive started playing with it but i need it finished and working.

if anyone can help id much appreciate it and dont mind paying for you time if you want it.

please look at the thread "curtain pole conundrums" in the programming help section to get an idea of what i need.

Thanks

i assume your referring to the multitude of curly brackets? wont compile without the little buggers!

I don't understand how your code works with Blynk. I would expect to see something there mapping the Blynk buttons but instead, you have code that looks exactly as though you are using physical buttons.

Since it is apparently working with Blynk, I'll guess that the mechanism that makes it work also thinks that the limit switches come from Blynk, whereas they have to be on the curtain rail.

if (digitalRead(buttonCWpin) == HIGH) {

buttonCWpressed = true;

If the buttons are wired correctly (one wire to GND, other to input pin), buttonCWpin will = LOW when pressed.

wildbill:
I don't understand how your code works with Blynk. I would expect to see something there mapping the Blynk buttons but instead, you have code that looks exactly as though you are using physical buttons.

Since it is apparently working with Blynk, I'll guess that the mechanism that makes it work also thinks that the limit switches come from Blynk, whereas they have to be on the curtain rail.

blynk as far as i understand just mimics a physical switch all of the code must be in the library and handled by the blynk server. if it works with physical switches, it works with blynk.
tomorrow ill wire it all with actual switches and get it working that way first once its working with switches ill convert to blynk and get help from the blynk forum.

Topics merged for easy reference

Hi All,

The person helping turned out to be a bit of a fraud. (it was no one from here) im re thinking the project now and going to change the stepper out for a normal dc motor.

Im hoping that the code will be much simpler!

Since you already went to the trouble of printing the mounts, I'd stick with the stepper. With limit switches of course, it will work with either kind of motor.

Please post the latest code.

the mount i have printed is prototype and needs mods anyway. the thing im worried about is RPM. my screw has a travel of 2mm/rev (it was all i could get in LH & RH at a price that wasnt to much to make the project possible.

Its a total travel of 1m (or there abouts) and so a total of 500 revs for travel i had read that steppers were ok for below 3000 RPM and so i though it would be fine, and based my setup on that. However the guy i got to help me said id be waiting for ages and id need to think about gearing.

The blynk side of the project has now been dropped and i just need to concentrate on doing the following:

open: momentary button tells motor to drive until limit operated
Closed: same but in opposite direction:
jog open: open whilst button pressed or until limit reached.
jog closed: same but in opposite direction.
of course if i stick with a stepper then ill need sleep to stop the motor drawing power all the time.

so first question (well this time anyway) :o is 500/1000 rpm achievable with a stepper with a 14v power supply will torque be a problem at these faster speeds. if not what will i have to change to make one work.