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.
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.
//********************************************************************************
//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.
//********************************************************************************
//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()
//********************************************************************************