Timing AC solenoid on time.

I am trying to use a reed switch to time the on cycle of an AC solenoid, the issue is that the reed switch cycles on and off with every top and bottom of the sin wave at 50hz when the solenoid is energized using this code below I can start and stop the count but I am struggling as to how I can reset the timer only when the solenoid is de energized

void loop() {

  unsigned long currentMillis = millis();
  val = digitalRead(inPin);
  if (!val) {
    freq++;
  }

  if (currentMillis - previousMillis >= interval and freq > 0) {

    // save the last time you blinked the LED
    previousMillis = currentMillis;

    Serial.println(timercount);
    Serial.println(freq);
    timercount++;
    freq = 0;
  }
}
const byte inPin = 2;
unsigned long freq = 0;

void setup() {
  Serial.begin(9600);
  pinMode (inPin, INPUT);
}

void loop() {
  unsigned long currentMillis = millis();
  bool val = digitalRead(inPin);
  static bool preVal;
  if (val != preVal) {
    freq ++;
  }
  
  const unsigned long interval = 1000;
  static unsigned long previousMillis;
  static unsigned long timercount;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    Serial.print("timercount :");
    Serial.println(timercount);
    Serial.print("freq       :");
    Serial.println(freq);
    timercount++;
    freq = 0;
    preVal = val;
  }
}

Try this.

  bool val = digitalRead(inPin);
  static bool preVal;

Is there a particular reason why these are bools ?

Is there a particular reason why these are bools ?

Yeah, it's looking more pretty to me. :slight_smile:

Count the transitions of the reed switch. Each transition represents 20 milliseconds @ 50Hz. You will have to use a timeout value for when a transition doesn't happen that represents the selonoid being off (unenergized)
You will start your count after the first transition. Then just multiply 20 millisecs by the number of counts and that will give you the time the selonoid is energized.

GrOnThOs:
Yeah, it's looking more pretty to me. :slight_smile:

That's not a valid reason to misuse a data type in my opinion

simonnomis:
I am trying to use a reed switch to time the on cycle of an AC solenoid, the issue is that the reed switch cycles on and off with every top and bottom of the sin wave at 50hz when the solenoid is energized using this code

Sounds like you need to 'debounce' the input. :slight_smile:

const byte ReedSwitchPin = 2;


bool CurrentRunningState = false;
unsigned long PreviousChangeTime = 0;
unsigned long DebounceInterval = 100;  // Allow only 10 changes per second


unsigned long TotalRunTime = 0;


void setup()
{
  pinMode(ReedSwitchPin, INPUT_PULLUP);
}


void loop()
{
  unsigned long currentMillis = millis();
  bool newState = digitalRead(ReedSwitchPin) == LOW;
  unsigned long timeInState = currentMillis - PreviousChangeTime;


  if (newState != CurrentRunningState && timeInState >= DebounceInterval)
  {
    PreviousChangeTime = currentMillis;
    CurrentRunningState = newState;


    // Record how long we were in the energized state:
    if (CurrentRunningState)
    {
      TotalRunTime += timeInState;
    }
  }


  // NOTE: TotalRunTime does not include the current ON cycle.  If CurrentRunningState is 'true', display
  // TotalRunTime + timeInState to get current total running time.
}

Thanks for the input but I don't think its a 'debounce' issue because the input never stabilizes and stays solid on it will all ways cycles @50hz when the solenoid is energized and because of the reed switch the on off period changes so I can't just count them an then multiply them to get the time.

I don't think its a 'debounce' issue because the input never stabilizes and stays solid on it will all ways cycles @50hz when the solenoid is energized

So, when the solenoid is active, the input voltage bouncing Between 0 VDC and 5 VDC about 50 times per second.
The easy solution its connect a capacitor on the input, this will converting the input voltage to almost a 5v straight line.
Or even better write a good debounce code.

Hi,

Can you tell us the application please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How does your reed switch cycle at 50Hz?
OR
Does it pass current flowing at 50Hz?

Thanks.. Tom... :slight_smile:

simonnomis:
Thanks for the input but I don’t think its a ‘debounce’ issue because the input never stabilizes and stays solid on it will all ways cycles @50hz when the solenoid is energized and because of the reed switch the on off period changes so I can’t just count them an then multiply them to get the time.

You’re right. I should have just filtered out short Inactive pulses. Let me try again:

const byte ReedSwitchPin = 2;
const bool ActiveState = LOW;  // Pin is Active LOW, grounded when reed switch is closed


// Get rid of the 50Hz 'inactive' pulses dureing the 'active' interval
const unsigned long PulseFilterInterval = 100;  // Require 'inactive' be >1/10th second


bool CurrentRunningState = !ActiveState;
unsigned long PreviousChangeTime = 0;
const unsigned long DebounceInterval = 10;


unsigned long TotalActiveTime = 0;


void setup()
{
  pinMode(ReedSwitchPin, INPUT_PULLUP);
}


void loop()
{
  unsigned long currentMillis = millis();


  bool newState = digitalRead(ReedSwitchPin);
  unsigned long timeInState = currentMillis - PreviousChangeTime;


  // Ignore any 'inactive' pulses less than the PulseFilterInterval
  static unsigned long lastActiveTime = 0;
  if (newState == ActiveState)
    lastActiveTime = currentMillis;
  else  // newState is 'inactive'
    if (currentMillis - lastActiveTime <= PulseFilterInterval)
      return;  // Do nothing


  if (newState != CurrentRunningState && timeInState >= DebounceInterval)
  {
    PreviousChangeTime = currentMillis;
    CurrentRunningState = newState;


    // If we have just left the ActiveState...
    if (CurrentRunningState != ActiveState)
    {
      // ... record how long we were in the ActiveState
      TotalActiveTime += timeInState;
    }
  }


  // NOTE: TotalActiveTime does not include the current Active cycle.  
  // If CurrentRunningState is ActiveState, display
  // TotalRunTime + timeInState to get current total Active time.
}

The reed switch cycles on and off with every top and bottom of the sin wave at 50Hz

Count the transitions of the reed switch. Each transition represents 20 milliseconds @ 50Hz.

How does your reed switch cycle at 50Hz?

Have I misunderstood something?
Surely the reed switch is switching at 100Hz, on / off for a positive half cycle and then on / off again for a negative half cycle, so 10ms per cycle.