Go Down

Topic: LVD And Mains Monitoring With Delay on and Delay Off. (Read 377 times) previous topic - next topic

mcmsat

Hello Engineers!

An "Engineer" is someone who is NEAR the ENGINE, I myself I am trying to be near but the heat and noise emissions from the engine is trying to scare me but I will not run away!

I am new to embedded system programming and I started with PIC Microcontrollers. I have carried out some experiments with PIC and CCS C Compiler and so far, I have been happy with my new learning.

Now, it comes! Constraints! I wanted to cross to Arduino to see how I can do those successful experiments with PIC Micros in Arduino but everything become weird to me especially in delay timing.

Here is the constraints:

In the code I will post below;

1. When MAINS is OK, both MAINS_OK & ENABLE Pins will go HIGH after 3sec delay(non-blocking).

2. When MAINS is BAD, MAINS_OK Pin will go LOW immediately while ENABLE Pin will wait for 3sec before going LOW.


3. If MAINS becomes OK again, the sequence described in "1." must be followed an so on.

I have tried in so many ways, it is not working as expected. Only the blinking of the SYSTEM LED is working fine.

Code: [Select]


// constants won't change. Used here to
// set pin numbers:
const int enablePin     = 7;      // the number of the Function Pins
const int lvdPin        = 6;
const int sysledPin    = 5;
const int mainsokPin   = 4;


// Variables will change:
int mainsPin = A0;
int batteryPin = A2;
int MAINS;
int BATT;
int ENABLE = LOW;
int LVD = LOW;
int SYS_LED = LOW;
int MAINS_OK = LOW; // State used to set the Function Pins

long previousMillis1 = 0;
long previousMillis2 = 0;// will store last time PIN was updated
long previousMillis3 = 0;
long previousMillis4 = 0;
long previousMillis5 = 0;


long ENABLE_ON        = 3000;
long ENABLE_OFF       = 3000;
long LVD_ON           = 3000;
long LVD_OFF          = 3000;
long MAINS_DELAY_ON   = 3000;
long MAINS_DELAY_OFF  = 0;
long SYS_LED_ON       = 1000;
long SYS_LED_OFF      = 1000; //Duration of the delay time (milliseconds)

void setup()
{
  pinMode(ENABLE, OUTPUT);  // set the digital pin as output:   
  pinMode(LVD, OUTPUT);     // set the digital pin as output:
  pinMode(SYS_LED, OUTPUT); // set the digital pin as output:
  pinMode(MAINS_OK, OUTPUT); // set the digital pin as output:
  pinMode(A0,INPUT); //set pin as Analog nput
  pinMode(A1,INPUT); //set pin as Analog nput 
}
void loop()
{
  unsigned long currentMillis1 = millis();
  unsigned long currentMillis2 = millis();
  unsigned long currentMillis3 = millis();
  unsigned long currentMillis4 = millis();


 
  if((SYS_LED == LOW) && (currentMillis1 - previousMillis1 >= SYS_LED_OFF))
  {
    SYS_LED = HIGH;  // Turn it off
    previousMillis1 = currentMillis1;  // Remember the time
    digitalWrite(sysledPin, SYS_LED);  // Update the actual SYS_LED
  }
  if((SYS_LED == HIGH) && (currentMillis1 - previousMillis1 >= SYS_LED_ON))
  {
    SYS_LED = LOW;  // turn it on
    previousMillis1 = currentMillis1;   // Remember the time
    digitalWrite(sysledPin, SYS_LED);    // Update the actual SYS_LED
  }

  MAINS =analogRead(mainsPin);
 
//========================= MAINS OK =======================================
/* If MAINS supply is ok, the MAINS_OK and ENABLE Pins will wait for 3sec
 *  before going high. Both Pins will go high at the same time.
 */

  if(MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V 
  if((MAINS_OK == LOW) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_ON))
  {
   MAINS_OK = HIGH;  // Turn MAINS_OK on with 3sec delay
   previousMillis2 = currentMillis2;  // Remember the time
   digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
  }
  if(MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V
  if((ENABLE == LOW) && (currentMillis3 - previousMillis3 >= ENABLE_ON))
  {
   ENABLE = HIGH;    //Turn ENABLE ON with 3sec delay
   previousMillis3 = currentMillis3;  // Remember the time
   digitalWrite(enablePin, ENABLE);  // Update the actual MAINS_OK
  }
   
//=================== MAINS NOT PRESENT or IT IS LOW/HIGH VOLTAGE ====================
/* As soon as MAINS is lost, whether it enters low voltage or high voltage,
 * the PINs MAINS_OK will go LOW imedatly without delay while ENABLE will wait for
 * 3sec before going LOW. Then If MAINS is restored OK, the initial delay sequence
 * must be followed.
 */
  if(MAINS <= 53 || MAINS >= 258)// <= 0.260V || >=1.26V// Below <= 120V and >= 276V
  if((MAINS_OK == HIGH) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_OFF))
  {
   MAINS_OK = LOW;  // Turn MAINS_OK off after 0sec delay
   previousMillis2 = currentMillis2;  // Remember the time
   digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
  }
  if(MAINS <= 53 || MAINS >= 258)// <= 0.260V || >=1.26V// Below <= 120V and >= 276V
  if((ENABLE == HIGH) && (currentMillis3 - previousMillis3 >= ENABLE_OFF))
  {
  ENABLE = LOW;    //Turn ENABLE off aftar 3sec delay
  previousMillis3 = currentMillis3;  // Remember the time
  digitalWrite(enablePin, ENABLE);  // Update the actual ENABLE
  }
}
{
BATT = analogRead(batteryPin);

 //============================ BATTERYERY SETTINGS ==============================
/* If Battery is below 21V LVD Pin go HIGH with 3sec delay
 *  When it goes high, it remains high the Batter goes up to 27.6V
 *  LVD will then go LOW ahter 3sec delay
 */
  if(BATT <= 391) //(1.91V) 21V         // BATTERY Low
  if ((LVD == LOW) && (currentMillis4 - previousMillis4 >= LVD_ON))
  {
  LVD = HIGH; //Turn LVD on with 3sec delay
  previousMillis4 = currentMillis4;  // Remember the time
  digitalWrite(lvdPin, LVD);  // Update the actual LVD 
  }

  if(BATT >=512)  // (2.50V)  27.6V         // BATTERY low Recovery
  if ((LVD == HIGH) && (currentMillis4 - previousMillis4 >= LVD_OFF))
  {
  LVD = LOW; // turn LVD off after 3sec delay
  previousMillis4 = currentMillis4;  // Remember the time
  digitalWrite(lvdPin, LVD);  // Update the actual LVD   
  } 
}
}





 

TomGeorge

#1
Jul 15, 2019, 01:28 pm Last Edit: Jul 15, 2019, 01:42 pm by TomGeorge
Hi,
Ops image;


Thanks.. Tom.. :)
PS, Tip, if in the IDE you select, TOOLS then AutoFormat, your code will be automatically indented, which will help follow your { and } groups.  :)
Everything runs on smoke, let the smoke out, it stops running....

mcmsat

Code: [Select]

// constants won't change. Used here to
// set pin numbers:
const int enablePin     = 7;      // the number of the Function Pins
const int lvdPin        = 6;
const int sysledPin    = 5;
const int mainsokPin   = 4;


// Variables will change:
int mainsPin = A0;
int batteryPin = A2;
int MAINS;
int BATT;
int ENABLE = LOW;
int LVD = LOW;
int SYS_LED = LOW;
int MAINS_OK = LOW; // State used to set the Function Pins

long previousMillis1 = 0;
long previousMillis2 = 0;// will store last time PIN was updated
long previousMillis3 = 0;
long previousMillis4 = 0;
long previousMillis5 = 0;


long ENABLE_ON        = 3000;
long ENABLE_OFF       = 3000;
long LVD_ON           = 3000;
long LVD_OFF          = 3000;
long MAINS_DELAY_ON   = 3000;
long MAINS_DELAY_OFF  = 0;
long SYS_LED_ON       = 1000;
long SYS_LED_OFF      = 1000; //Duration of the delay time (milliseconds)

void setup()
{
  pinMode(ENABLE, OUTPUT);  // set the digital pin as output:
  pinMode(LVD, OUTPUT);     // set the digital pin as output:
  pinMode(SYS_LED, OUTPUT); // set the digital pin as output:
  pinMode(MAINS_OK, OUTPUT); // set the digital pin as output:
  pinMode(A0, INPUT); //set pin as Analog nput
  pinMode(A1, INPUT); //set pin as Analog nput
}
void loop()
{
  unsigned long currentMillis1 = millis();
  unsigned long currentMillis2 = millis();
  unsigned long currentMillis3 = millis();
  unsigned long currentMillis4 = millis();



  if ((SYS_LED == LOW) && (currentMillis1 - previousMillis1 >= SYS_LED_OFF))
  {
    SYS_LED = HIGH;  // Turn it off
    previousMillis1 = currentMillis1;  // Remember the time
    digitalWrite(sysledPin, SYS_LED);  // Update the actual SYS_LED
  }
  if ((SYS_LED == HIGH) && (currentMillis1 - previousMillis1 >= SYS_LED_ON))
  {
    SYS_LED = LOW;  // turn it on
    previousMillis1 = currentMillis1;   // Remember the time
    digitalWrite(sysledPin, SYS_LED);    // Update the actual SYS_LED
  }

  MAINS = analogRead(mainsPin);

  //========================= MAINS OK =======================================
  /* If MAINS supply is ok, the MAINS_OK and ENABLE Pins will wait for 3sec
      before going high. Both Pins will go high at the same time.
  */

  if (MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V
    if ((MAINS_OK == LOW) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_ON))
    {
      MAINS_OK = HIGH;  // Turn MAINS_OK on with 3sec delay
      previousMillis2 = currentMillis2;  // Remember the time
      digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
    }
  if (MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V
    if ((ENABLE == LOW) && (currentMillis3 - previousMillis3 >= ENABLE_ON))
    {
      ENABLE = HIGH;    //Turn ENABLE ON with 3sec delay
      previousMillis3 = currentMillis3;  // Remember the time
      digitalWrite(enablePin, ENABLE);  // Update the actual MAINS_OK
    }

  //=================== MAINS NOT PRESENT or IT IS LOW/HIGH VOLTAGE ====================
  /* As soon as MAINS is lost, whether it enters low voltage or high voltage,
     the PINs MAINS_OK will go LOW imedatly without delay while ENABLE will wait for
     3sec before going LOW. Then If MAINS is restored OK, the initial delay sequence
     must be followed.
  */
  if (MAINS <= 53 || MAINS >= 258) // <= 0.260V || >=1.26V// Below <= 120V and >= 276V
    if ((MAINS_OK == HIGH) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_OFF))
    {
      MAINS_OK = LOW;  // Turn MAINS_OK off after 0sec delay
      previousMillis2 = currentMillis2;  // Remember the time
      digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
    }
  if (MAINS <= 53 || MAINS >= 258) // <= 0.260V || >=1.26V// Below <= 120V and >= 276V
    if ((ENABLE == HIGH) && (currentMillis3 - previousMillis3 >= ENABLE_OFF))
    {
      ENABLE = LOW;    //Turn ENABLE off aftar 3sec delay
      previousMillis3 = currentMillis3;  // Remember the time
      digitalWrite(enablePin, ENABLE);  // Update the actual ENABLE
    }


  BATT = analogRead(batteryPin);

  //============================ BATTERYERY SETTINGS ==============================
  /* If Battery is below 21V LVD Pin go HIGH with 3sec delay
      When it goes high, it remains high the Batter goes up to 27.6V
      LVD will then go LOW ahter 3sec delay
  */
  if (BATT <= 391) //(1.91V) 21V         // BATTERY Low
    if ((LVD == LOW) && (currentMillis4 - previousMillis4 >= LVD_ON))
    {
      LVD = HIGH; //Turn LVD on with 3sec delay
      previousMillis4 = currentMillis4;  // Remember the time
      digitalWrite(lvdPin, LVD);  // Update the actual LVD
    }

  if (BATT >= 512) // (2.50V)  27.6V         // BATTERY low Recovery
    if ((LVD == HIGH) && (currentMillis4 - previousMillis4 >= LVD_OFF))
    {
      LVD = LOW; // turn LVD off after 3sec delay
      previousMillis4 = currentMillis4;  // Remember the time
      digitalWrite(lvdPin, LVD);  // Update the actual LVD
    }
}






TomGeorge

#3
Jul 15, 2019, 01:47 pm Last Edit: Jul 15, 2019, 01:49 pm by TomGeorge
Hi,
Code: [Select]

  }
 {  <<<<<<<<<<<<<<<<<<<<<<<<<<<++++++++++++++++++++++++++++++++++are these necessary????
    MAINS = analogRead(mainsPin);


    //========================= MAINS OK =======================================
    /* If MAINS supply is ok, the MAINS_OK and ENABLE Pins will wait for 3sec
        before going high. Both Pins will go high at the same time.
    */


    if (MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V
      if ((MAINS_OK == LOW) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_ON))
      {
        MAINS_OK = HIGH;  // Turn MAINS_OK on with 3sec delay
        previousMillis2 = currentMillis2;  // Remember the time
        digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
      }
    if (MAINS >= 84 && MAINS <= 177) // >=0.410V && <=0.864V >=140V && <=250V
      if ((ENABLE == LOW) && (currentMillis3 - previousMillis3 >= ENABLE_ON))
      {
        ENABLE = HIGH;    //Turn ENABLE ON with 3sec delay
        previousMillis3 = currentMillis3;  // Remember the time
        digitalWrite(enablePin, ENABLE);  // Update the actual MAINS_OK
      }


    //=================== MAINS NOT PRESENT or IT IS LOW/HIGH VOLTAGE ====================
    /* As soon as MAINS is lost, whether it enters low voltage or high voltage,
       the PINs MAINS_OK will go LOW imedatly without delay while ENABLE will wait for
       3sec before going LOW. Then If MAINS is restored OK, the initial delay sequence
       must be followed.
    */
    if (MAINS <= 53 || MAINS >= 258) // <= 0.260V || >=1.26V// Below <= 120V and >= 276V
      if ((MAINS_OK == HIGH) && (currentMillis2 - previousMillis2 >= MAINS_DELAY_OFF))
      {
        MAINS_OK = LOW;  // Turn MAINS_OK off after 0sec delay
        previousMillis2 = currentMillis2;  // Remember the time
        digitalWrite(mainsokPin, MAINS_OK);  // Update the actual MAINS_OK
      }
    if (MAINS <= 53 || MAINS >= 258) // <= 0.260V || >=1.26V// Below <= 120V and >= 276V
      if ((ENABLE == HIGH) && (currentMillis3 - previousMillis3 >= ENABLE_OFF))
      {
        ENABLE = LOW;    //Turn ENABLE off aftar 3sec delay
        previousMillis3 = currentMillis3;  // Remember the time
        digitalWrite(enablePin, ENABLE);  // Update the actual ENABLE
      }
 }  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<========================are these necessary???
  {



the pair of { } indicated by   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<========================are these necessary???

Are they needed?
There is another pair following on that may not be needed either.

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

mcmsat


TomGeorge

Hi,
Does it make a difference to how your code runs without them?

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

mcmsat

Code: [Select]

 {  <<<<<<<<<<<<<<<<<<<<<<<<<<<++++++++++++++++++++++++++++++++++are these necessary????
    MAINS = analogRead(mainsPin);

The unnecessary bracing removed in post number #2

mcmsat

Hi,
Does it make a difference to how your code runs without them?

Tom.. :)
Hi,
Does it make a difference to how your code runs without them?

Tom.. :)
The code is same with or without them but I have removed them anyway

The system is Limiting my post to "1 post every 5min"

Go Up