help programming Arduino timers, clocks, counters.

I need some help with some code if possible....

I need to trigger a pin on the GLCD on when solar output reaches a certain level but it needs to wait until the PV output has been high for say 30 seconds, and then stay on and overrun once the PVoutput drops for say 15mins (to allow for clouds etc)

I am stuck with the millis() function any help appreciated, I have spent 2 long evenings and made no progress to the point where I have deleted all my code and am back to square one, this is what i need in arduino speak:-

if solarPV  > 3000 then

start a count down from 30 seconds to 0 then set output to true, if it drops below 3000 during this time, reset the 30second countdown. Once it has been higher than 3000 for 30 seconds it needs to run for 15mins minumum,

once output is true then keep track of the run time (in seconds)

once output drops below 3000 start a 15minute countdown before turn off (and reset to 15mins if output pops over 3000 again)

I know its easy enough, I just cant get it working! Cant see the wood for the trees.

 

JBecker's picture

Re: help programming Arduino timers, clocks, counters.

Please try something like this (modified 'Blink' example code):

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

int SolarPV;
unsigned long DelayON;
unsigned long DelayOFF;

// the setup routine runs once when you press reset:
void setup() {             
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);  
}

// the loop routine runs over and over again forever:
void loop() {

  // measure new SolarPV value here

  if( SolarPV>3000 )   // PV power over 3000W
  {
    DelayOFF = millis()+(15UL*60*1000);  // set OFF delay to 15 minutes from now
    if( millis()>DelayON )
       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
  else                 // PV power less ar equal 3000W
  {
    DelayON = millis()+(30*1000);  // set ON delay to 30 seconds from now
    if( millis()>DelayOFF )
       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  }
}

 

 

mattnj's picture

Re: help programming Arduino timers, clocks, counters.

thanks, will give that a whirl later

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.