Help with programming a tester!

Hi -

I am making a simple tester. I have a timer that runs a switch for ten seconds and would like to test a bunch of them with using the arudino as a tester. I would like to hit "Run" and it runs a test. (Hitting reset would restart the test). The tester would turn on a Green LED if it passes the test and a Red LED if it fails. The timer runs for 10 seconds so the switch is closed for ten seconds. I'd like the range to for the test to pass if it runs bewteen 8-12 seconds. If it falls below 8 seconds it fails and if it runs above 12 seconds, it also fails. Total time of test could be 15 or 20 seconds that doesn't matter.

The Arudino I want to use it to power an Arudino relay which supplies the external 12V power supply to the timer that is being tested. This relay would have to power on at the same time as the test begins, obviously.

I started some coding based off a push button example but am lost in how to develop it any further. Please help! I attached a drawing for reference. It's not a final design or anything - just for clarifying what I intend.

Test_For_reading_time_of_switch.ino (1.5 KB)

You should avoid using the 5v Arduino pin supplying power to a relay coil.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.


#include <ezButton.h>
const int RUN = 4;
const int ResetTest = 3;
const int PassLED = 12;
const int FailLed = 11;
const int RelayDrive = 8;
const int SHORT_PRESS_TIME = 7999; // 1000 milliseconds
const int LONG_PRESS_TIME  = 1201; // 1000 milliseconds
int input = Serial.parseInt();  // keep other operations outside the constrain function
int constrainedInput = constrain(input, 8000, 12000);
ezButton button(2);  // create ezButton object that attach to pin 2;

unsigned long pressedTime  = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
const int PassLed = 12;
const int FailLed = 11;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if (button.isPressed()) {
    pressedTime = millis();
    isPressing = true;
    isLongDetected = false;
  }

  if (button.isReleased()) {
    isPressing = false;
    releasedTime = millis();

    long pressDuration = releasedTime - pressedTime;

    if ( pressDuration < SHORT_PRESS_TIME )
      Serial.println("A short press is detected");
  }

  if (isPressing == true && isLongDetected == false) {
    long pressDuration = millis() - pressedTime;

    if ( pressDuration > LONG_PRESS_TIME ) {
      Serial.println("A long press is detected");
      isLongDetected = true;
    }
    if ( pressDuration = constrainedInput ) {
      Serial.println("Pass");
      isLongDetected = true;
    }
  }
}

Ok - I won't use that. I'll probably run the relay another way.

Show us a good schematic of your UUT circuit. Give links to components.

I don't have access to the UUT schematic or components. The UUT is a 12VDC Timer that powers a 12V relay. My plan was to use this tester to test how long the relay is closed for by connecting the relay to ground and the other terminal of the UUT to pin 2 on the arudino circuit.

For the UUT - when it recieves power, the output (relay) is closed for the ten seconds and won't reset until power is taken away.

When the Arduino Run switch closes:

  • turn off the LEDs
  • energize the relay module
  • start a TIMER
  • if the TIMER falls up out of the maximum range turn ON red LED
  • if reset switch closes, cancel timing operation

When the UUT closes it’s contact:

  • stop the TIMER
  • determine if the TIMER value is within the allowable time needed.
  • control LEDs as appropriate
  • de-energize the relay module

Thats correct!

Try making another attempt at the sketch without using external libraries.

Ok! How should I use a timing function? Do you have any examples?



//********************************************************************************
//Version    YY/MM/DD    Description
//1.00       21/09/29    Running sketch
//
//

#define CLOSED                       LOW
#define OPENED                       HIGH

#define PRESSED                      LOW
#define RELEASED                     HIGH

#define ENABLED                      true
#define DISABLED                     false

#define relayON                      LOW
#define relayOFF                     HIGH

//***************************************************************

const byte heartbeatLED            = 13;
const byte passLED                 = 12;
const byte failLED                 = 11;
const byte relayPin                = 8;
const byte runSwitch               = 4;
const byte resetSwitch             = 3;

byte lastRunSwitchState            = OPENED;

//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;


//***************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(passLED, OUTPUT);
  pinMode(failLED, OUTPUT);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, relayOFF);

  pinMode(runSwitch, INPUT_PULLUP);
  pinMode(resetSwitch, INPUT_PULLUP);

} //END of setup()


//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //*************************************

} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    r u n S w i t c h
  //runSwitch code
  byte currentState = digitalRead(runSwitch);

  //**********************
  //was there a change in state ?
  if (lastRunSwitchState != currentState)
  {
    //update to the new state
    lastRunSwitchState = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      //do something ?
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      //do something ?
    }

  } //END of runSwitch code


  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()


//********************************************************************************

First of all, you probably meant:
if ( pressDuration == constrainedInput ) {

Second, what are the chances that the value will be exactly the same as your serial input?

You can't call Serial.parseInt() until AFTER you call Serial.begin(baudrate);. Move the input = Serial.parseInt(); to setup(). What is the serial input for? You already know the allowed range of values.

@eopett, as you don't seem to have upload problems, your topic has been moved to a more suitable location on the forum.

//********************************************************************************
//Version    YY/MM/DD    Description
//1.00       21/09/29    Running sketch
//
//

#define CLOSED                       LOW
#define OPENED                       HIGH

#define PRESSED                      LOW
#define RELEASED                     HIGH

#define ENABLED                      true
#define DISABLED                     false

#define relayON                      LOW
#define relayOFF                     HIGH

//***************************************************************

const byte heartbeatLED            = 13;
const byte passLED                 = 12;
const byte failLED                 = 11;
const byte relayPin                = 8;
const byte runSwitch               = 4;
const byte resetSwitch             = 3;

byte lastRunSwitchState            = OPENED;

//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;


//***************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(passLED, OUTPUT);
  pinMode(failLED, OUTPUT);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, relayOFF);

  pinMode(runSwitch, INPUT_PULLUP);
  pinMode(resetSwitch, INPUT_PULLUP);

} //END of setup()


//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //toggle the heartbeat LED every 500ms
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //*************************************

} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    r u n S w i t c h
  //runSwitch code
  byte currentState = digitalRead(runSwitch);

  //**********************
  //was there a change in state ?
  if (lastRunSwitchState != currentState)
  {
    //update to the new state
    lastRunSwitchState = currentState;

    //**********************                                         C L O S E D
    //is the switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(relayON, HIGH);
    }

    //**********************                                         O P E N E D
    //the switch is opened
    else
    {
      digitalWrite(relayOFF, LOW);
    }

  } //END of runSwitch code


  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()


//********************************************************************************

Thanks Larry, I'm not really good at programming. Not sure how to continue I added two lines not sure if its correct.

99.9% of new people never ask questions about solutions they are given.

Below should be most of what's needed.
You have a chance to learn something here, if you review the sketch and don't understand the code ask for explanations on the parts you don't understand.

EDIT
Added comments:

//https://forum.arduino.cc/t/help-with-programming-a-tester/910200?u=larryd

//Tester for a UUT relay board 

//********************************************************************************
//Version    YY/MM/DD    Description
//1.00       21/09/29    Running sketch
//
//

#define LEDon                        HIGH
#define LEDoff                       LOW

#define uutCONTACTpicked             LOW  //swap these to reverse
#define uutCONTACTdropped            HIGH //swap these to reverse

#define CLOSED                       LOW
#define OPENED                       HIGH

#define PRESSED                      LOW
#define RELEASED                     HIGH

#define ENABLED                      true
#define DISABLED                     false

#define relayON                      LOW
#define relayOFF                     HIGH

//***************************************************************

//DISABLED = we are not in timing mode
boolean timingFlag                 = DISABLED;  

const byte relayOperatedelay       = 10;     //10ms, time to allow the relays to energize

const byte heartbeatLED            = 13;     //[PIN 13]---[220R]---[A->|-K]---GND
const byte passLED                 = 12;
const byte failLED                 = 11;
const byte relayPin                = 10;

const byte uutSwitch               = 5;      //+5V---[50k Internal Pullup]---PIN---[contact]---GND
const byte runSwitch               = 4;      //+5V---[50k Internal Pullup]---PIN---[switch]---GND
const byte resetSwitch             = 3;      //+5V---[50k Internal Pullup]---PIN---[switch]---GND

byte lastRunSwitchState            = OPENED;
byte lastResetSwitchState          = OPENED;

//timing stuff
const unsigned long lowLimit       = 8  * 1000ul; //8  seconds
const unsigned long highLimit      = 12 * 1000ul; //12 seconds
const unsigned long exceededLimit  = 15 * 1000ul; //15 seconds

unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long uutMillis;
unsigned long resultMillis;


//***************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);

  pinMode(passLED, OUTPUT);
  digitalWrite(passLED, LEDoff);

  pinMode(failLED, OUTPUT);
  digitalWrite(failLED, LEDoff);

  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, relayOFF);

  pinMode(runSwitch, INPUT_PULLUP);
  pinMode(resetSwitch, INPUT_PULLUP);
  pinMode(uutSwitch, INPUT_PULLUP);

} //END of setup()


//***************************************************************
void loop()
{
  //*************************************                          h e a r t b e a t   T I M E R
  //to see if the sketch is blocking,
  //is it time to toggle the LED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*************************************                          c h e c k S w i t c h   T I M E R
  //is it time to read the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    //go read the switches
    checkSwitches();
  }

  //*************************************                          U U T   T I M E R
  //are we testing a relay board ?
  if (timingFlag == ENABLED)
  {
    //*********************
    //has the UUT relay contact changed ?
    if (digitalRead(uutSwitch) == uutCONTACTpicked)
    {
      //stop the test
      timingFlag = DISABLED;

      //save the UUT time results
      resultMillis = millis() - uutMillis;

      //*************
      //are we within the range limits ?
      if (resultMillis >= lowLimit && resultMillis <= highLimit)
      {
        //test passed
        digitalWrite(relayPin, relayOFF);
        digitalWrite(passLED, LEDon);
      }

      //*************
      else
      {
        //test failed
        digitalWrite(relayPin, relayOFF);
        digitalWrite(failLED, LEDon);
      }
    }

    //*************
    //have we exceed the maximum time ?
    else if (millis() - uutMillis >= exceededLimit)
    {
      //stop the test
      timingFlag = DISABLED;

      //test failed
      digitalWrite(relayPin, relayOFF);
      digitalWrite(failLED, LEDon);
    }
    
  } //END of   if (timingFlag == ENABLED)

  

  //*************************************
  //other non blocking code goes here
  //*************************************


} //END of loop()


//********************************************************************************
void checkSwitches()
{
  //*********************************************                    r u n S w i t c h
  //runSwitch code
  byte currentState = digitalRead(runSwitch);

  //**********************
  //was there a change in state ?
  if (lastRunSwitchState != currentState)
  {
    //update to the new state
    lastRunSwitchState = currentState;

    //**********************                                         C L O S E D
    //if we are not currently in test mode, is this switch closed ?
    if (timingFlag == DISABLED && currentState == CLOSED)
    {
      digitalWrite(relayPin, relayON);
      
      //time for the relays to operate
      delay(relayOperatedelay);
      
      digitalWrite(passLED, LEDoff);
      digitalWrite(failLED, LEDoff);

      //start the test
      timingFlag = ENABLED;

      //restart the TIMER
      uutMillis = millis();
    }

  } //END of runSwitch code

  //*********************************************                    r e s e t S w i t c h
  //resetSwitch code
  currentState = digitalRead(resetSwitch);

  //**********************
  //was there a change in state ?
  if (lastResetSwitchState != currentState)
  {
    //update to the new state
    lastResetSwitchState = currentState;

    //**********************                                         C L O S E D
    //is this switch closed ?
    if (currentState == CLOSED)
    {
      digitalWrite(relayPin, relayOFF);
      digitalWrite(passLED, LEDoff);
      digitalWrite(failLED, LEDoff);

      //stop the test
      timingFlag = DISABLED;
    }

  } //END of resetSwitch code


  //*********************************************                    o t h e r S w i t c h e s
  //next switch code
  //*********************************************

} //END of   checkSwitches()


//********************************************************************************

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.