For my first C++ / Arduino project attempting to construct a pump motor controller application that monitors 24 VDC motor current, when the current exceeds 4.5 amps opens the motor relay. I am interested in any clues how best to approach this. Thank you!
Mark
Typically the motor relay will remain closed, supplying 24VDC power to the pump motor control circuit. The circuit has a pressure switch with a mechanical adjustable dead band range preventing any short cycling. Would like to program when the pressure switch turns on the pump, momentarily ignore motor start current spike (up to 6 AMPs), then monitor the motor run current 3.0 – 4 amps. If the current is 4.1 – 4.4 amps, start a countdown timer that would open the motor relay in 20 seconds as long as the current remained 4.1 – 4.4 amps, if the current reaches 4.5 amps, open the motor relay shutting off the pump for 45 seconds and then close the motor relay to begin the process again.
To make it easier for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum
Your code is too long for me to study quickly without copying to a text editor.
You have not said what the code you have posted actually does.
To implement an action after an value has been continuous for a given time you need some code like this pseudo code
if (value is acceptable) {
lastOKmillis = millis();
}
if (mills() - lastOKmillis >= timeLimitMillis) {
// problem has been continuous for time limit
}
Majority of the code posted operates the Analog 20A Current Sensor, can view the output from serial monitor and plotter. "// Pump motor control relay" is where i am working to figure out logic to drive the relay.
Mark
/***********************************************************
This sample code shows how to use 20A current sensor module.
Created 2016-4-26
By Bernie Chen <bernie.chen@dfrobot.com>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here, http://www.dfrobot.com/wiki/index.php?title=20A_Current_Sensor_SKU:SEN0214
2.This code is tested on Arduino Uno.
****************************************************/
const int currentSensorPin = A0; //define sensor pin
const int mVperAmp = 100; // use 185 for 5A Module, and 66 for 30A Module
float Vref = 0; //read your Vcc voltage,typical voltage should be 5000mV(5.0V)
#define RELAY1 6
void setup()
{
Serial.begin(115200);
Vref = readVref(); //read the reference votage(default:VCC)
pinMode(RELAY1, OUTPUT);
}
void loop()
{
/*If you are reading DC current, use this function to read DC current. Then uncomment the AC function.*/
float CurrentValue = readDCCurrent(currentSensorPin);
/*If you are reading AC current, use this function to read AC current, it returns the RMS. Then uncomment the DC function.*/
//float CurrentValue = readACCurrent(currentSensorPin);
Serial.println(CurrentValue);
delay(500);
// Pump motor control relay
if (CurrentValue <= 0.5) //delay (1000); // ignore 1st time > 4.5 = disregard motor start amps
if (CurrentValue < 3.5) digitalWrite (RELAY1,LOW); // Turns Relay On
if (CurrentValue > 4.5) digitalWrite (RELAY1,HIGH); // Turns Relay Off
}
/*read DC Current Value*/
float readDCCurrent(int Pin)
{
int analogValueArray[31];
for(int index=0;index<31;index++)
{
analogValueArray[index]=analogRead(Pin);
}
int i,j,tempValue;
for (j = 0; j < 31 - 1; j ++)
{
for (i = 0; i < 31 - 1 - j; i ++)
{
if (analogValueArray[i] > analogValueArray[i + 1])
{
tempValue = analogValueArray[i];
analogValueArray[i] = analogValueArray[i + 1];
analogValueArray[i + 1] = tempValue;
}
}
}
float medianValue = analogValueArray[(31 - 1) / 2];
float DCCurrentValue = (medianValue / 1024.0 * Vref - Vref / 2.0) / mVperAmp; //Sensitivity:100mV/A, 0A @ Vcc/2
return DCCurrentValue;
}
/*read AC Current Value and ruturn the RMS*/
float readACCurrent(int Pin)
{
int analogValue; //analog value read from the sensor output pin
int maxValue = 0; // store max value
int minValue = 1024; // store min value
unsigned long start_time = millis();
while((millis()-start_time) < 200) //sample for 0.2s
{
analogValue = analogRead(Pin);
if (analogValue > maxValue)
{
maxValue = analogValue;
}
if (analogValue < minValue)
{
minValue = analogValue;
}
}
float Vpp = (maxValue - minValue) * Vref / 1024.0;
float Vrms = Vpp / 2.0 * 0.707 / mVperAmp; //Vpp -> Vrms
return Vrms;
}
/*read reference voltage*/
long readVref()
{
long result;
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
ADCSRB &= ~_BV(MUX5); // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#endif
#if defined(__AVR__)
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1126400L / result; //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
return result;
#elif defined(__arm__)
return (3300); //Arduino Due
#else
return (3300); //Guess that other un-supported architectures will be running a 3.3V!
#endif
}