/*Fail safe detection tester with actuonix linear servo:
www.actuonix.com
Author: Cajetan Chinonso Emmanuel
Company name: Impact Automation/Robotics Technologies */
#include <Wire.h> // Include wire library
#include <LiquidCrystal_I2C.h> // Include New Liquid crystal for I2C bus
#include <Servo.h> // Include Servo Library
#define LINEAR_ACTUATORPIN 11 // Define Servo Pin
LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display
Servo LINEAR_ACTUATOR; // Set object as servo
int linearValue = 1300; // Value for linear actuator on start
int actuatorPos = 2; // Positional pin for linear servo
int aPos = 0; // Positional Value
int switchPin = 13; // Test Pin
int startPin = 3; // Start pin
int stopPin = 4;
int count; // Counter Value
const int setPoint = 2000;
int countPin = A1;
int buzzer = 6; // Alarm Pin
int val = 0; // Value to store test pin
int startVal = 0; // Value to store start
int stopVal = 0;
unsigned long previousMillis;
const long setTime = 10000; // Switch monitor
byte currentButtonState;
byte previousButtonState;
boolean counting;
boolean funcRunning;
void setup() {
Serial.begin(115200); // Begin Serial Monitoring
lcd.init(); // LCD with 16 chars, 2 line display
lcd.clear(); // Clear screen
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("WELCOME");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" TRANOS ");
lcd.setCursor(0, 1);
lcd.print("CONTRACTING LTD");
delay(5000);
lcd.clear();
delay(200);
lcd.print("Loading.");
delay(800);
lcd.clear();
lcd.print("Loading..");
delay(800);
lcd.clear();
lcd.print("Loading...");
delay(800);
lcd.clear();
lcd.print("Loading.");
delay(800);
lcd.clear();
lcd.print("Loading..");
delay(800);
lcd.clear();
lcd.print("Loading...");
delay(800);
lcd.clear();
lcd.print("Loading.");
delay(800);
lcd.clear();
lcd.print("Loading..");
delay(800);
lcd.clear();
lcd.print("Loading...");
delay(2000);
lcd.clear();
LINEAR_ACTUATOR.attach(LINEAR_ACTUATORPIN, 1050, 2000); // min and max position for servo
pinMode(startPin, INPUT_PULLUP); // Set start pin as input
pinMode(stopPin, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
LINEAR_ACTUATOR.writeMicroseconds(linearValue); // Linear value
delay(20);
funcRunning = true;
}
void stopRun() {
lcd.setCursor(6, 0);
lcd.print("START");
linearValue = 1300;
Serial.println(linearValue);
}
void testFail() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("TEST FAIL");
delay(4000);
digitalWrite(buzzer, HIGH);
}
void loop() {
stopVal = digitalRead(stopPin);
startVal = digitalRead(startPin);
digitalWrite(buzzer, OUTPUT);
stopRun();
if ((startVal == LOW) && (stopVal == HIGH)) {
// funcRunning = true;
lcd.clear();
linearValue = 1350;
delay(4000);
Serial.println("Start");
while ((startVal == LOW) && (stopVal == HIGH)) {
// funcRunning = true;
lcd.setCursor(0, 0);
lcd.print("Testing = ");
lcd.setCursor(0, 1);
lcd.print("Max Count = ");
unsigned long currentMillis = millis();
previousButtonState = currentButtonState;
currentButtonState = digitalRead(A1);
if (currentButtonState == HIGH) {
linearValue = 1500;
Serial.println(linearValue);
}
if (previousButtonState == LOW) {
linearValue = 1700;
Serial.println(linearValue);
}
if (currentButtonState == HIGH and previousButtonState == LOW) {
count++;
lcd.setCursor(12, 1);
lcd.print(setPoint);
lcd.setCursor(10, 0);
lcd.print(count);
Serial.println(count);
previousMillis = currentMillis;
if (count == setPoint) {
while (counting = true) {
Serial.println("Successful");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Start New");
lcd.setCursor(0, 1);
lcd.print("Test Successful");
delay(4000);
counting = false;
stopRun();
}
}
}
if ((currentButtonState == HIGH or previousButtonState == LOW) && (currentMillis - previousMillis >= setTime)) {
while (counting = true) {
previousMillis = currentMillis;
lcd.clear();
Serial.println("Fail");
testFail();
delay(100);
stopRun();
counting = false;
}
}
if (stopVal == LOW && startVal == HIGH) {
stopRun();
Serial.println("Stop");
}
}
}
}
This is the code