I need some help. I have been using a lot of the codes posted out there to create my Frankenstein code. The objective is to use the blynk app to control a stepper motor to open my blinds. I would like to have two modes, one for manual control and the second as an alarm. (It will be nice to wake up too!!)
Setup:
Arduino Uno
ESP8266
Adafruit Motor Shield V2
Stepper Motor
I have a working code, that connects to the blynk server, controls the stepper motors (Manual Mode). The part I am stuck at now is making the alarm work. I saw was using the post with Costas great example. But now I am at the last steps and running low on my memory on my Arduino UNO. I could use some help to:
- condense my code to run more efficiently
- get the alarm to trigger.
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "XXX";
char ssid[] = "XXX"; // your network SSID (name)
char pass[] = "XXX" // your network password
char Date[16];
char Time[16];
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
//#include <SimpleTimer.h>
//#include <Time.h>
//#include <TimeLib.h>
#include <WidgetRTC.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
WidgetLCD lcd(V0);
#define VIRTUAL_SLIDER_POSITION V1
#define VIRTUAL_OPEN_PIN V2
#define VIRTUAL_CLOSED_PIN V3
#define VIRTUAL_ALARM_PIN V6
long startseconds; // start time in seconds
long stopseconds; // stop time in seconds
long nowseconds; // time now in seconds
//int btnpin;
SimpleTimer timer;
WidgetRTC rtc;
//bool isFirstConnect = true; // Keep this flag not to re-sync on every reconnection
//BLYNK_CONNECTED() { // This function will run every time Blynk connection is established
// if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
// Blynk.syncAll();
// isFirstConnect = false;
// }
//}
void setup()
{
Serial.begin(9600);
delay(10);
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(1000); // 100 rpm
lcd.clear();
lcd.print(0,0,"Blynk Connected");
delay(50);
updatedisplay();
rtc.begin();
//timer.setInterval(60000L, activetoday);
}
BLYNK_WRITE(V6) {
TimeInputParam t(param);
int dayadjustment = -1;
if (t.hasStartTime())
{
Serial.println(String("Start: ") +
t.getStartHour() + ":" +
t.getStartMinute() + ":" +
t.getStartSecond());
}
if(weekday() == 1){
dayadjustment = 6; // needed for Sunday, Time library is day 1 and Blynk is day 7
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
nowseconds = ((hour() * 3600) + (minute() * 60) + second());
startseconds=(t.getStartHour() * 3600) + (t.getStartMinute() * 60);
Serial.println(nowseconds);
Serial.println(startseconds);
if(nowseconds >= startseconds){
if(nowseconds <= startseconds + 90){ // 90s on 60s timer ensures 1 trigger command is sent
Serial.println("Alarmon!");
}
}
}
}
/*
void activetoday(){ // check if schedule should run today
if(year() != 1970){
Blynk.syncVirtual(V6); // sync timeinput widget
}
}
*/
BLYNK_WRITE(VIRTUAL_OPEN_PIN)
{
int DOpen = param.asInt();
if (DOpen==1) {
OpenB();
Blynk.run();
// DOpen=0;
Blynk.syncVirtual(VIRTUAL_OPEN_PIN);
}
}
BLYNK_WRITE(VIRTUAL_CLOSED_PIN)
{
int DClose = param.asInt();
if (DClose ==1) {
CloseB();
Blynk.run();
DClose=0;
Blynk.syncVirtual(VIRTUAL_CLOSED_PIN);
}
}
void loop()
{
Blynk.run();
//timer.run();
}
int StepMotorPosition=0;
int StepperScale=2000; //Need to Define
int MotorPosition= ((float)StepMotorPosition/StepperScale)*100;
int Steps=1000; //Optimize
void CloseB(void) {
while((float)MotorPosition/StepperScale*100<=99){
int rotation=FORWARD;
myMotor->step(Steps, rotation, DOUBLE);
MotorPosition=MotorPosition+Steps;
updatedisplay();
}
}
void OpenB() {
while((float)MotorPosition/StepperScale*100>=1){
int rotation=BACKWARD;
myMotor->step(Steps, rotation, DOUBLE);
MotorPosition=MotorPosition-Steps;
updatedisplay();
}
}
void updatedisplay(){
lcd.clear();
lcd.print(0,0,"Blind Position:");
lcd.print(0,1,(float)MotorPosition/StepperScale*100);
lcd.print(3,1,"% ");
Blynk.run();
}
As you can see from my code, I have the simple timers commented out. As I am getting farther in the project, I am really strapped for memory on the Arduino UNO. As I am at the last steps of which I think for triggering the timer for the alarm, I am out of memory. I had difficulties earlier on trying to figure out how to make the push buttons update, so found an example to force the update with (below) and found success:
BLYNK_WRITE(VIRTUAL_OPEN_PIN)
{
int DOpen = param.asInt();
if (DOpen==1) {
OpenB();
Blynk.run();
// DOpen=0;
Blynk.syncVirtual(VIRTUAL_OPEN_PIN);
}
}
Please help. I’m new to this and my background is not in coding, its actually building the thing (mechanical).