Hi all,I would like a little bit of advice about a program i am writing.
the idea is that there are 2 pumps which sit in a pond and when a level set point is reached then one of the pumps runs till the lower set point is reached the next time thew set point is reached the other pump should run as to kind of balance the duty. if one pump is running then then trips then the second pump will run. so i know what i want to do but i cant seem to write the code. If someone would point me down the right road im sure i can figure it out.
Thanks…Ben
here is my code
//------------------Pump 1 inputs and outputs----------------------
int Pump1Auto = D0; //pump 1 selector switch
int Pump1Running = D1;
int Pump1Run = D5;
int Pump1Tripped = A1;
//-----------------------------------------------------------------
//----------------- Pump 2 Inputs and Outputs
int Pump2Auto =D2;
int Pump2Running =D3;
int Pump2Run = D6;
int Pump2Tripped = A2;
//-----------------------------------------------------------------
//----------------Alarm inputs and outputs-------------------------
int Alarm4 = A3; // high float will be connected to pin A3 on the unit
int highlevellight = D7; //this is the light that will give us info about the state of the alarm
//-----------------------------------------------------------------
// ---------------Varibles----------------------------------------
bool isArmed = true; //true or false is the alarm set
int i; // integer to count 1 time so multiple emails are not sent
int Duty;
//-----------------------Level Inputs-----------------------------
const int LevelReading = A0;
float Level; // to hold the reading from analog 0
float StartSetpoint; // Holds the setpoing for turning on the pump
float StopSetpoint;// Holds the setpoint for turning off the pump
void setup()
{
// Particle.function("rearmAlarm", rearm);
Serial.begin(9600); // begin serial for displaying reading
pinMode(Alarm4, INPUT_PULLDOWN);
pinMode(highlevellight, OUTPUT);// declare the highlevellight pin as an output
pinMode(Pump1Auto, INPUT_PULLDOWN);
pinMode(Pump1Tripped, INPUT_PULLDOWN);
pinMode(Pump2Tripped, INPUT_PULLDOWN);
pinMode(Pump1Run, OUTPUT);
pinMode(Pump2Run, OUTPUT);
// pinMode(Level, Input); // does not need this line of code
i = 0;
StartSetpoint = 5;
Duty = Pump1Run;
// Pump1Tripped = LOW;
// Automode = LOW;
}
void loop()
{
//Alarm system which is activated when input becomes LOW
//if the alarm is activated and waiting for a low input then led d7 will flash on and off every second
//if a low input is detected then the led on d7 will flash one cycle at 0.5seconds then stay on
//
if (isArmed == true && i<1)
{
SetAlarm();
}
bool state = !digitalRead(Alarm4); // invert active LOW to more intuitive active HIGH /////// state is asigned the oposite of Alarm1 so if it is low state will be TRUE
if (isArmed && state) /////// if isArmed and state is true
{
RunAlarm();
}
if (!isArmed && !state) //if thr alarm is not armed and the state is false the alarm is not activated (not LOW)
{
i = 0; //set i back to 0 so the alarm active email can be sent once
isArmed = true; // set isArmed to true so the alarm process can be run again
}
//---------------------------------------------------------------------------------
//----------------Running the pump on level----------------------------------------
//Serial.println(StartSetpoint);
delay(250);
Serial.print("Level Is....");
Serial.print(Level);
//Serial.println(Level);
Serial.println(" Metres");
//Serial.write(“Level is”);
//delay(2000); // use this when serial printing the analog value
delay(250);
Level = analogRead(LevelReading);
//Level = constrain(Level, 0, 2000);
Level = map(Level, 0, 4095, 0, 10); // re map the analog value to 0 - 10
if ((digitalRead(Pump1Auto) == HIGH )&& (Level>=StartSetpoint)&& (digitalRead(Pump1Tripped) == LOW))//&& (digitalRead(Pump2Auto)== HIGH)&&(digitalRead(Pump2Tripped)== LOW))
//if (Level >= Setpoint)
{
digitalWrite(Duty, HIGH);
flasher();
if ((digitalRead(Pump1Tripped) == HIGH)&& (digitalRead(Pump2Auto) == HIGH))
{
Duty = Pump2Run;
}
}
else digitalWrite(Duty, LOW);
}
//-------------------------------Functions wrote below here--------------------------------------
//-----------------------------------------------------------------------------------------------
//---------------------------------test function flash light when flasher() is called in the loop
void flasher()
{
digitalWrite(highlevellight, LOW);
delay (90);
digitalWrite(highlevellight, HIGH);
delay(90);
digitalWrite(highlevellight, LOW);
delay (90);
// digitalWrite(highlevellight, HIGH);
// delay(90);
// digitalWrite(highlevellight, LOW);
// delay (90);
}
void RunAlarm()
{
isArmed = false; // don't trigger till rearmed
//----------blocked for testing
Particle.publish("EVENT_PREFIX", PRIVATE); // publish the event so an email can be sent
//----------BLocked for Testing
Particle.publish("twilio", PRIVATE);
/////////////Flash light to indicate an alarm state////////////////////////////////
digitalWrite(highlevellight, LOW);
delay (500);
digitalWrite(highlevellight, HIGH);
delay(500);
digitalWrite(highlevellight, LOW);
delay (500);
digitalWrite(highlevellight, HIGH);
delay(500);
digitalWrite(highlevellight, LOW);
delay (500);
}
void SetAlarm()
{
//-------------blocked for testing Particle.publish("ALARM_SET", PRIVATE); // publish the event so alarm set email can be sent
////// flash the lights so we know alarm is armed--------------------------------------------
digitalWrite(highlevellight, LOW);
delay (1000);
digitalWrite(highlevellight, HIGH);
delay(1000);
digitalWrite(highlevellight, LOW);
delay (1000);
digitalWrite(highlevellight, HIGH);
delay(1000);
digitalWrite(highlevellight, LOW);
delay(1000);
digitalWrite(highlevellight, HIGH);
delay (1000);
digitalWrite(highlevellight, LOW);
i++; //add one to i so the code will not be run again
}