Hello everyone,
Thanks in advance.
I am creating a circuit for measuring the relay pick up and drop time measurements calculation. I have received the accurate measurements but after 1 or 2 operation my lcd display goes with characters. And I am planning to use lm 7805 ic to step down the voltage which is parallel to relay 24 volt and it's given to aurdino through an opto coupler.
If any one have the better solution for accurate and robust circuit. Please help me.
I share my codings below,
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
unsigned long time_start;
unsigned long time_end;
unsigned long time;
int PD_start; // value of the start-Photodiode
int PD_end; // value of the end-Photodiode
int Werte[100];
long start;
int i;
//unsigned long d = 320000;
float multiplier;
#define FASTADC 1
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
void setup()
{
Serial.begin(9600);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, LOW);
lcd.begin(16, 2);
lcd.clear();
#if FASTADC
cbi(ADCSRA,ADPS2) ; // ADPS2 auf 0 gesetzt
sbi(ADCSRA,ADPS1) ; // ADPS1 auf 1 gesetzt
sbi(ADCSRA,ADPS0) ;
#endif
multiplier = 0.70;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Initializing...");
lcd.setCursor(10,1);
lcd.print("A/");
delay(1000);
// ADPS2 ADPS1 ADPS0 Division factor
// 0 0 0 1 nicht schneller als 2
// 0 0 1 2
// 0 1 0 4
// 0 1 1 8
// 1 0 0 16
// 1 0 1 32
// 1 1 0 64
// 1 1 1 128 (Standard)
}
void loop()
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("SELECT T1/T2");
delay(1000);
if(digitalRead(8) == LOW)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("T1 SELECTED");
delay(1000);
Serial.print(analogRead(A0));
Serial.print(" ");
Serial.println(analogRead(A1));
delay(100);
PD_start = analogRead(A0); // measuring the brightness of the starting-PD without projectile
delay(20);
PD_end = analogRead(A1); // measuring the brightness of the ending-PD without projectile
delay(1000);
lcd.clear();
Serial.print("PD-start: ");
Serial.print(PD_start);
Serial.print(" , PD-end: ");
Serial.println(PD_end);
lcd.setCursor(0,0); // to be added for testing
lcd.print("PD1: ");
lcd.print(PD_start);
lcd.setCursor(0,1);
lcd.print("PD2: ");
lcd.print(PD_end);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("start test");
lcd.setCursor(0,1);
lcd.print("... ");
delay(10);
unsigned long d = 200000;
while(analogRead(A0) < 200) //while(analogRead(A0) > PD_start * multiplier)
{
// waiting for the projectile
}
time_start = micros(); // start-time in µs
while(analogRead(A1) < 200) // while(analogRead(A1) > PD_end * multiplier)
{
// waiting for the projectile
}
time_end = micros(); // end-time in µs
time = time_end - time_start; // flight-time in µs
PD_start = analogRead(A0); // newly added measuring the brightness of the starting-PD without projectile
delay(20);
PD_end = analogRead(A1); // measuring the brightness of the ending-PD without projectile
delay(1000);
Serial.print("Time: ");
Serial.println(time);
lcd.clear(); // newly added
lcd.setCursor(0,0); // to be added for testing
lcd.print("PD1: ");
lcd.print(PD_start);
lcd.setCursor(0,1);
lcd.print("PD2: ");
lcd.print(PD_end);
delay(1000);
lcd.clear(); // newly added
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("t = ");
lcd.setCursor(4,0);
lcd.print(time);
lcd.print(" us");
if(time < d)
{
lcd.setCursor(1,1);
lcd.print("PASS");
delay(100);
}
else{
lcd.setCursor(1,1);
lcd.print("FAIL");
delay(100);
}
delay(50000);
}
if(digitalRead(9) == HIGH)
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("T2 SLECTED");
delay(1000);
Serial.print(analogRead(A2));
Serial.print(" ");
Serial.println(analogRead(A3));
delay(100);
PD_start = analogRead(A2); // measuring the brightness of the starting-PD without projectile
delay(20);
PD_end = analogRead(A3); // measuring the brightness of the ending-PD without projectile
delay(1000);
lcd.clear();
Serial.print("PD-start: ");
Serial.print(PD_start);
Serial.print(" , PD-end: ");
Serial.println(PD_end);
lcd.setCursor(0,0);
lcd.print("PD1: ");
lcd.print(PD_start);
lcd.setCursor(0,1);
lcd.print("PD2: ");
lcd.print(PD_end);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("start test");
lcd.setCursor(0,1);
lcd.print("... ");
delay(10);
unsigned long d = 50000;
while(analogRead(A2) > PD_start * multiplier)
{
// waiting for the projectile
}
time_start = micros(); // start-time in µs
while(analogRead(A3) > PD_end * multiplier)
{
// waiting for the projectile
}
time_end = micros(); // end-time in µs
time = time_end - time_start; // flight-time in µs
PD_start = analogRead(A2); // newly added measuring the brightness of the starting-PD without projectile
delay(20);
PD_end = analogRead(A3); // measuring the brightness of the ending-PD without projectile
delay(1000);
lcd.clear();// newly addded
lcd.setCursor(0,0);
lcd.print("PD1: ");
lcd.print(PD_start);
lcd.setCursor(0,1);
lcd.print("PD2: ");
lcd.print(PD_end);
delay(2000);
Serial.print("Time: ");
Serial.println(time);
lcd.clear();// newly addded
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("t = ");
lcd.setCursor(4,0);
lcd.print(time);
lcd.print(" us");
if(time < d)
{
lcd.setCursor(1,1);
lcd.print("PASS");
delay(100);
}
else{
lcd.setCursor(1,1);
lcd.print("FAIL");
delay(100);
}
delay(50000);
}
}