I am writing a basic sketch to turn a pump on for X amount of time and turn if off again after 3X time.
I tried the TimerAlarms.h route, I tried the mills rout and finally came up with something a lot simpler...so I thought.
Simply create own variable and increase it the same ways as mills increases in the back ground just now at the frequency of the processor (16MHz) and basically once the flag has reached 1 time the frequency of the processor it should be 1 second (Relay turns off) and 3 times the frequency should be 3 seconds (Flag clears) and the cycle can start again.
But it does notwork like that..The LED(Relay) flashes at a rate that is almost just a fade.
What am I missing. Surely if the processor speed is 16 000 000 Hz it will process the "Loop" function 16 000 000 times per second?
long Flag = 0; // Variable I am going to monitor to know when to switch pump on and off
const int Increment = 1; // inrement by which I will inrease the FLAG value
const int TurnON = 10; // Time to turn pump ON
long TurnOFF = 16000010; // Turn pump OFF
long ClearFlag = 48000010; // Zero Flag and start counting again
const byte ledPin = 13; // using the built in LED to simulate RELAY for Pump
void setup()
{
Serial.begin(9600); // start Serial in case we need to print debugging info
pinMode(ledPin, OUTPUT); // Declaring Pin 13 as an OUTPUT
//digitalWrite(ledPin, LOW); // Start the pump at the beginning of the program
}
void loop()
{
Flag = (Flag + Increment);
Serial.print("*****Starting the Loop again ");
Serial.println(Flag);
if (Flag = TurnON) // Test if Relay must be turned ON
{
digitalWrite(ledPin, HIGH); // If Flag = 10 turn the LED ON//
Serial.print("Pump is ON: ");
Serial.println(Flag);
}
if (Flag = TurnOFF) // Test if flag has reached TurnOff value
{
digitalWrite(ledPin, LOW); // Turn Relay OFF
Serial.print("Pump is OFF: ");
Serial.println(Flag);
}
if (Flag = ClearFlag) // Test whether Flag must be Reset
{
Flag = 0;
Serial.print("Flag Rest to: ");
Serial.println(Flag);
}
}