The following code is for testing the lifetime of an actuator.
When the actuator is contracted, it outputs a value of 0.4V based on hall sensors that I can tap into.
When it's going up or down, it outputs a value of 2.4V
When it's extended, it outputs a value of 1.4V
I use two relays to control the direction of the actuator.
It takes about 9 sec for the actuator to extend and contract again.
I want the code to do the following:
Extend and contract (one loop).
Count the loop and display it.
Pause for a desired time (after one loop) and then do it again, until the actuator motor breaks.
The following code does all the above.
What I want is when the actuator motor breaks, the count should stop and the LCD should display the count and stop both replays.
I have researched and came to the conclusion that I should use a State Change detection.
So when the analog voltage value does not change in 5 or 10 min, then the actuator motor must've stopped working.
So I need a few lines of code that stop the counter when the analog value does not change in a certain amount of time. I tried different stuff, but It can't seem to make it work.
// include the library code til LCD skærm
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Millis function
const unsigned long eventInterval_1 = 14000; //14 sec delay.
unsigned long previousTime_1 = 0;
//voltage sensor
int value = 0;
float voltage;
int offset =4;// set the correction offset value
//Relay
int Relay_1 = 9;
int Relay_2 = 10;
int counter;
void setup()
{
// indstil LCD skærmens rækker og kolonner
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(Relay_1,OUTPUT); //pin 10 defineret som output til relæ 1
pinMode(Relay_2,OUTPUT); //Pin 11 defineret som output til relæ 2
}
void loop()
{
delay(500);
//for analog sensor
int volt = analogRead(A0);//analog input fra pin A0
double voltage = map(volt,0,1023, 0, 2500) + offset;// dele spændingen fra 0-1023 til 0-2500 of tilføj correction offset
voltage /=100;
Serial.print("Spænding: ");
Serial.print(voltage); //print voltage
Serial.println(" V");
//millis timer
unsigned long currentTime = millis();
if ((voltage > 0.0) && (voltage <= 0.7)) //if voltage is between these values, aktuator will to up
{
if (currentTime - previousTime_1 >= eventInterval_1) //millis counter
{
digitalWrite(Relay_1,HIGH);
digitalWrite(Relay_2,LOW);
counter++; //count should go up everytime a loop is complete (up and down onnce).
previousTime_1 = currentTime; // til millis funktionen
}
//if voltage is > 1.75V, the actuator should keep going up until it reaches a voltage between 1.2 and 1.7V
if (voltage > 1.75 && Relay_1==HIGH)
{
digitalWrite(Relay_1,HIGH);
digitalWrite(Relay_2,LOW);
}
}
//when the voltage is between 1.2 and 1.7V, the actuator is in the top and it should not come back down again.
else if(voltage > 1.2 && voltage < 1.7) //når spænding
{
digitalWrite(Relay_1,LOW);
digitalWrite(Relay_2,HIGH);
}
//when it's going down, it should keep doing down.
if(voltage>1.75 && Relay_2==HIGH)
{
digitalWrite(Relay_1,LOW);
digitalWrite(Relay_2,HIGH);
}
/*
* missing code
*
* it the value (in this case the voltage), has not changed in, let's say 5 min, I
* want the counter to stop counting and display the latest count and stop both relays and.
*
*
*
*This is the part that I have troube programming.
*
*/
lcd.setCursor(8, 0);
lcd.print(counter); //print counter on lcd display
} // void loop