I have a large inflatable balloon that inflates and deflates via a relay controlled fan.
It is suspended from the ceiling and as it deflates I have a winch that pulls the loose fabric up. When it inflates I have a winch let the fabric down.
The relays work and I can get the Arduino to control the different parts but I cannot set up a schedule of events.
In the inflatable are strings of lights that flash on the 1/4 hour and top of the hour like a clocktower. That part of the code seems to be working although it uses and alarm.delay and I think that needs to be removed so it does not hold up the rest of the program but I have had trouble doing that.
For example my goal:
inflate 40 min (while inflating run winch down for 20 min) then stay inflated for 10 min deflate for 10 min (while deflating run winch up for 10 min) stay deflated 30 minutes, inflate for 35 min ( while inflating run winch down for 15 min) stay inflated for 50 min ..... etc for a 16 hr daily sequence.
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
/*-----( Declare Constants and Pin Numbers )-----*/
const int lightPin1 = 30; // Light #1
const int lightPin2 = 31; // Light #2
const int lightPin3 = 32; // Light #3
const int lightPin4 = 33; // Light #4
const int deflateFanPin = 2; // Fan #1 Deflate
const int inflateFanPin = 3; // Fan #2 Inflate
const int deflateDamperPin = 4; // Damper Motor #1
const int inflateDamperPin = 5; // Damper Motor #2
const int winchUpPin = 8; // winch up
const int winchDownPin = 9; // winch down
/*-----( Declare objects )-----*/
RTC_DS1307 rtc; // Create a RealTimeClock object
uint32_t syncProvider() //function which sets up the RTC as the source of external time
{
return rtc.now().unixtime();
}
/*-----( Declare Variables )-----*/
int i;
int ding;
int n;
int hr;
int has15run = 0;
int has30run = 0;
int has45run = 0;
int has00run = 0;
int winchUpState = HIGH;
int winchDownState = HIGH;
int winchUpOK = 1;
int winchDownOK = 1;
unsigned long previousMillisUp = 0; // will store last time Winch was updated
unsigned long previousMillisDown = 0; // will store last time Winch was updated\
unsigned long previousMillisUpOK = 0;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
long UpOnTime = 1000;
long UpOffTime = 2000;
long DownOnTime = 1000;
long DownOffTime = 2000;
long Upduration = 5000;
int winchUpseconds;
int winchDownseconds;
void setup()
{
pinMode(lightPin1, OUTPUT); // sets the digital pins as output
pinMode(lightPin2, OUTPUT);
pinMode(lightPin3, OUTPUT);
pinMode(lightPin4, OUTPUT);
pinMode(deflateFanPin, OUTPUT);
pinMode(inflateFanPin, OUTPUT);
pinMode(deflateDamperPin, OUTPUT);
pinMode(inflateDamperPin, OUTPUT);
pinMode(winchUpPin, OUTPUT);
pinMode(winchDownPin, OUTPUT);
digitalWrite(lightPin1, HIGH); // Prevents relays from starting up engaged
digitalWrite(lightPin2, HIGH);
digitalWrite(lightPin3, HIGH);
digitalWrite(lightPin4, HIGH);
digitalWrite(deflateFanPin, HIGH);
digitalWrite(inflateFanPin, HIGH);
digitalWrite(deflateDamperPin, HIGH);
digitalWrite(inflateDamperPin, HIGH);
digitalWrite(winchUpPin, HIGH);
digitalWrite(winchDownPin, HIGH);
Serial.begin(57600); // Set up for Serial Monitor to be able to see this work
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__)); //comment this out when the RTC has been set
setSyncProvider(syncProvider); // the function to get the time from the RTC
/*-----( Declare alarms )-----*/
// Alarm.alarmRepeat(8,30,0, inflate);
// Alarm.alarmRepeat(9,00,0, stayinflated);
// Alarm.alarmRepeat(10,00,0, deflate);
// Alarm.alarmRepeat(10,30,0, staydeflated);
// Alarm.alarmRepeat(11,30,0, inflate);
// Alarm.alarmRepeat(12,00,0, stayinflated);
Alarm.alarmRepeat(14, 45, 0, deflate);
Alarm.alarmRepeat(14, 46, 0, upWinch);
}//--(end setup )---
void loop()
{
currentMillis = millis();
blinkClock();
nightLights();
//digitalClockDisplay();
//Alarm.delay(1000); // wait one second
//digitalWrite(winchUpPin, LOW);
//digitalWrite(winchUpPin, HIGH);
// digitalWrite(winchDownPin, LOW);
//digitalWrite(winchDownPin, HIGH);
//winchUpOK = true;
//upWinch();
//downWinch();
inflate();
//deflate();
//stayinflated();
//staydeflated();
}
void upWinch ()
{
unsigned long currentMillis = millis();
if ((winchUpState == HIGH) && (winchUpOK = true) && (currentMillis - previousMillisUp >= UpOnTime))
{
{
winchUpState = LOW; // Turn it on
previousMillisUp = currentMillis; // Remember the time
digitalWrite(winchUpPin, winchUpState); // Update the winch pin
}
}
else if ((winchUpState == LOW) && (currentMillis - previousMillisUp >= UpOffTime))
{
winchUpState = HIGH; // turn it off
previousMillisUp = currentMillis; // Remember the time
digitalWrite(winchUpPin, winchUpState); // Update the winch pin
}
}
void downWinch ()
{
if ((winchDownState == HIGH) && (currentMillis - previousMillisDown >= DownOnTime))
{
winchDownState = LOW; // Turn it on
previousMillisDown = currentMillis; // Remember the time
digitalWrite(winchDownPin, winchDownState); // Update the winch pin
}
else if ((winchDownState == LOW) && (currentMillis - previousMillisDown >= DownOffTime))
{
winchDownState = HIGH; // turn it off
previousMillisDown = currentMillis; // Remember the time
digitalWrite(winchDownPin, winchDownState); // Update the winch pin
}
}
void inflate ()
{
digitalWrite(inflateFanPin, LOW); // Turn on inflate fan
digitalWrite(deflateFanPin, HIGH); // Make sure deflate fan is off
digitalWrite(deflateDamperPin, HIGH); // Make sure deflate damper is closed
// digitalWrite(inflateDamperPin, LOW); // Open inflate damper (not used in current project)
// winchDownOK = true;
}
void deflate ()
{
digitalWrite(inflateFanPin, HIGH); // Make sure inflate fan is off
digitalWrite(deflateFanPin, LOW); // Turn on deflate fan
digitalWrite(deflateDamperPin, LOW); // Open deflate damper
// digitalWrite(inflateDamperPin, HIGH); // Make sure inflate damper is closed (not used in current project)
// winchUpOK = true;
}
void stayinflated ()
{
inflate ();
winchDownOK = false;
}
void staydeflated ()
{
deflate ();
winchUpOK = false;
}