The software below is to control 2 stepper motors on my Telescope for the main focuser and the autoguider scope. I’m using an old laptop to USB link to the Arduino Uno. along with a DHT 11 for temperature, Humidity and dew point. I have no problems linking the Arduino to my IPhone I can control the stepper motor enable inputs and get the temperature data without any issues. However both sliders V10 and V11 will move the motors up to a minute later then nothing. Below is the software any advise would be appreciated
#include <Blynk.h>
#include <SoftwareSerial.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <DHT.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
int long newvalst1 = 0; //new value stepper 1
int long newvalst2 = 0; //new value stepper 2
float focuserst1 = 0.0; //actual value stepper 1 mm
float focuserst2 = 0.0; //actual value stepper 2 mm
int st1onoff = 0; //stepper 1 enable on/off
int st2onoff = 0; //stepper 2 enable on/off
int coolingfan = 0; //cooling fan
// The X Stepper pins
#define STEPPER1_DIR_PIN 4
#define STEPPER1_STEP_PIN 5
// The Y stepper pins
#define STEPPER2_DIR_PIN 7
#define STEPPER2_STEP_PIN 6
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);
//*********************************************Setup Dew Point Calculations
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
//******************************************** Include Section
// You should get Auth Token in the Blynk App.
char auth[] = "84b261deea7843cdb57e93acc4b18260";
//********************************************** Setup Blynk_WRITE
// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V1);
// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V10) { //Main focuser slider value
newvalst1 = param.asInt();
stepper1.runToNewPosition(newvalst1); // Move stepper to new position
Blynk.syncVirtual(V10);
focuserst1 = newvalst1 * (30.0 / 1023.0);
Blynk.virtualWrite(V20, focuserst1);
Blynk.virtualWrite(V21, newvalst1);
terminal.flush();
}
BLYNK_WRITE(V11) { //autoguider focuser slider value
newvalst2 = param.asInt();
stepper2.runToNewPosition(newvalst2); // Move stepper to new position
Blynk.syncVirtual(V11);
focuserst2 = newvalst2 * (60.0 / 1023.0);
Blynk.virtualWrite(V3, focuserst2);
Blynk.virtualWrite(V2, newvalst2);
terminal.flush();
}
BLYNK_WRITE(V15) // main focuser stepper enable on/off
{
st1onoff = param.asInt();
terminal.flush();
}
BLYNK_WRITE(V16) //autoguider stepper enable on/off
{
st2onoff = param.asInt();
terminal.flush();
}
//*************************************** Setup Stepper Motors
#define stpren1 8 //pin 7 Main Focuser Stepper Enable 1
#define stpren2 12 //pin 9 Auto Guider Stepper Enable 2
#define DHTPIN A0 //pin A0 DHT11 Temperature Sensor
#define fan 2 // pin 2 Telescope cooling Fan
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//***************************************** Variables to store current, previous and move position
void setup() {
{
// Debug console
SwSerial.begin(9600);
// Blynk will work through Serial
// Do not read or write this serial manually in your sketch
Serial.begin(9600);
Blynk.begin(Serial, auth);
// This will print Blynk Software version to the Terminal Widget when
// your hardware gets connected to Blynk Server
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
terminal.println(F("-------------"));
terminal.println(F("Arduino Connected to Iphone"));
terminal.flush();
}
{
dht.begin();
}
{
stepper1.setMaxSpeed(500); // Set speed fast enough to follow pot rotation
stepper1.setAcceleration(400); // High Acceleration to follow pot rotation
stepper2.setMaxSpeed(500);
stepper2.setAcceleration(400);
pinMode(stpren1, OUTPUT); //enable stepper 1
pinMode(stpren2, OUTPUT); //enable stepper 2
pinMode(fan, OUTPUT); //cooling fan for telescope
}
}
void loop() {
{
Blynk.run();
}
{
stepperenables();
}
{
dewreading();
}
}
void dewreading() {
delay (5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
float hiDegC = dht.convertFtoC(hi);
Blynk.virtualWrite(V0, t, 1);
Blynk.virtualWrite(V7, h);
Blynk.virtualWrite(V6, dewPoint(t, h));
if (h > 70){
digitalWrite(fan, HIGH);
}
if (h < 65){
digitalWrite(fan, LOW);
}
}
void stepperenables() {
if (st1onoff == true) {
digitalWrite(stpren1, HIGH); //Stepper 1 Enable ON
}
if (st1onoff == false) {
digitalWrite(stpren1, LOW); //Stepper 1 Enable OFF
}
if (st2onoff == true) {
digitalWrite(stpren2, HIGH); //Stepper 2 Enable ON
}
if (st2onoff == false) {
digitalWrite(stpren2, LOW); //Stepper 2 Enable OFF
}
}
@Andybee You didn’t format your code correctly as requested and shown. I have corrected it for you. Please look back and see how it is done for the future. Thanks.
@wanek nice diagram (I replaced it with a current one, the previous one had a typo, my bad )
@Andybee you have a rather long delay that will basicly halt your entire program for the duration… probably the only reason you are not loosing full connection is because of the USB link; Same delays with a WiFi MCU would have you disconnecting all the time.
Change out the delays and use timer functions instead.
Thank you updating. I have adjusted the program to a timer from the delay. Cleaned up the program, Although there is an improvement it still stutters. Would there be any benefit to convert to a local server.!
@Andybee my guess is a local server will perform the same as it looks like a bad sketch and remember you are then responsible for maintaining the server.
I don’t like the look of your loop() , your terminal.flush() calls and syncing a pin within the pin read function.
Suggest you mod your sketch before considering a server change.
Are V0, V6 and V7 value display widgets or something else?
I am guessing you think you are sending temperature to V0 with 1 decimal place. That’s not the case, you are sending an array of values to V0 i.e. temperature to [0] and 1 to [1]. This can have some strange effects depending what the widget is.
The syntax should be something like:
Blynk.virtualWrite(V0, String(t, 1));
Other than Terminal commands in setup() I don’t see where you are sending anything to V1 so flush() serves no purpose. What do you think you are sending to Terminal and where is it in your sketch?
Again thanks for the support. the temperature, humidity and dew point at the moment are displayed gauge values but at some point they will be a temperature compensation for the focuser’s and the dew control to switch on heaters. The terminal widget has been removed along with the terminal flush. but still the sliders are an issue.
#include <Blynk.h>
#include <SoftwareSerial.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <DHT.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
int long newvalst1 = 0; //new value stepper 1
int long newvalst2 = 0; //new value stepper 2
float focuserst1 = 0.0; //actual value stepper 1 mm
float focuserst2 = 0.0; //actual value stepper 2 mm
int st1onoff = 0; //stepper 1 enable on/off
int st2onoff = 0; //stepper 2 enable on/off
int coolingfan = 0; //cooling fan
// The X Stepper pins
#define STEPPER1_DIR_PIN 4
#define STEPPER1_STEP_PIN 5
// The Y stepper pins
#define STEPPER2_DIR_PIN 7
#define STEPPER2_STEP_PIN 6
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);
//*********************************************Setup Dew Point Calculations
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
//******************************************** Include Section
// You should get Auth Token in the Blynk App.
char auth[] = "83a7ec4897b24c12ad99ec98e3dc0458";
//********************************************** Setup Blynk_WRITE
//setup the Sliders to control the Stepper motor position
BLYNK_WRITE(V0) { //Main focuser slider value
stepper1.runToNewPosition(param.asInt()); // Move stepper to new position
focuserst1 = (param.asInt() / 138.333); // convert value to mm distance
Blynk.virtualWrite(V2, focuserst1);
}
BLYNK_WRITE(V10) { //autoguider focuser slider value
stepper2.runToNewPosition(param.asInt()); // Move stepper to new position
focuserst2 = (param.asInt() / 102.5); // convert value to mm distance
Blynk.virtualWrite(V12, focuserst2);
}
//setup the PushButtons to control the Stepper motor position
BLYNK_WRITE(V1) // main focuser stepper enable on/off
{
st1onoff = param.asInt();
}
BLYNK_WRITE(V11) //autoguider stepper enable on/off
{
st2onoff = param.asInt();
}
//***********define inputs
#define stpren1 8 //pin 7 Main Focuser Stepper Enable 1
#define stpren2 12 //pin 9 Auto Guider Stepper Enable 2
#define DHTPIN A0 //pin A0 DHT11 Temperature Sensor
#define fan 2 // pin 2 Telescope cooling Fan
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void setup() {
// Debug console
SwSerial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
{
dht.begin();
timer.setInterval(3000, dewreading);
//****** Stepper motor variables ************************
stepper1.setMaxSpeed(500); // Set speed fast enough to follow pot rotation
stepper1.setAcceleration(400); // High Acceleration to follow pot rotation
stepper2.setMaxSpeed(500);
stepper2.setAcceleration(400);
pinMode(stpren1, OUTPUT); //enable stepper 1
pinMode(stpren2, OUTPUT); //enable stepper 2
pinMode(fan, OUTPUT); //cooling fan for telescope
}
}
void loop() {
Blynk.run();
timer.run();
stepperenables();
}
void dewreading() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
float f = dht.readTemperature(true);
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
float hiDegC = dht.convertFtoC(hi);
Blynk.virtualWrite(V20, t);
Blynk.virtualWrite(V21, h);
Blynk.virtualWrite(V22, dewPoint(t, h));
if (h > 70){
digitalWrite(fan, HIGH);
}
if (h < 65){
digitalWrite(fan, LOW);
}
}
void stepperenables() {
if (st1onoff == true) {
digitalWrite(stpren1, HIGH); //Stepper 1 Enable ON
}
if (st1onoff == false) {
digitalWrite(stpren1, LOW); //Stepper 1 Enable OFF
}
if (st2onoff == true) {
digitalWrite(stpren2, HIGH); //Stepper 2 Enable ON
}
if (st2onoff == false) {
digitalWrite(stpren2, LOW); //Stepper 2 Enable OFF
}
}
Originally I had a range of 0-1023 when I map the value. This was when I used a 10k pot for the distance. The 2 steppers are set for quarter stepping so now the range is 0 to 6150 and 0 to 4150. V2 and V12 convert the slider values into mm travel distance as the main focuses is 30mm travel and the autoguider is 60mm travel. It gives me a visual understanding of the position. The info forms part of my settings when I want to go back and view an object in the night sky.
V2 is calculated from focuserst1 from the slider value divided by 138.333 and V12 focuserst2 slider value divided by 102.5. Full travel time is up to 5 seconds it could take a lot longer. Speed was not the Requirement very fine adjustment is the most important criteria.
any advise on alternative stepper control would be welcome.
Perhaps use a Step Widget instead of sliders… I don’t think sliders have a very fine range, or at least hard to move carefully, depending on screen size and device digitizer sensitivity, etc.
Also, the only thing you have on a timer is your temp/hum sensor (PS - the DHT11 is very low quality, ±2 deg, etc.) but your stepper routines are running full tilt, do they need hundredths/thousandths of a second response time? It looks like at a slight twitch of a slider and your code is running many fine stepper adjustments per second.