Being a beginner in the Arduino world I've run into problems.
I use four Panasonic Photoelectric Sensor PM-Y45 (NPN type) to measure speed for a free-falling mass. Brown wires connected to +5V, blue wires connected to GND and white wires to D8-D11, with pull-up resistors.
Panasonic Photoelectric Sensor PM-Y45 (NPN type)
I can get reasonable results for all four time intervals, but in some cases I get completely unrealistic results for one or more time for the travel between the sensors, like many thousand milliseconds, (should be around 16-18 milliseconds). I can also get what look like multiples of the actual timestamp, i.e. 2 times or 4 times the real result.
Sometimes (or quite often to be honest) the LCD shows gibberish. Even though I reset the Arduino (by pressing the Reset button) I get gibberish in the LCD.
I didn’t manage to put in the relay in the Fitzing schematic. It is a Velleman WPM406
Velleman WPM406
It’s connected to +18V to middle connection (between NC and NO) and NC to GND (18V) (holding magnet normally activated)
S is connected to pin 13, while + is connected to +5V and – is connected to GND (5V).
When the button is pressed the holding magnet is released and the mass is dropped. At the same time, I want a time stamp (start time for time interval between start and first photo sensor).
Following the suggestions to solve LCD-problems I added two capacitators, a 10 µF and a 0,22 µF (.22K, couldn’t find a 0,1 µF), between LCD pin 1 and 2 (VSS and VDO), but the problem still occur just as often.
I use a trim pot for the contrast on the LCD.
I’ve tried both USB and 9V, with the same result.
BN: Arduino Uno
VID: 0x2341
PID: 0x0043
Port: COM3
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
unsigned long timer0;
unsigned long timer1;
unsigned long timer2;
unsigned long timer3;
unsigned long timer4;
float Time01;
float Time12;
float Time23;
float Time34;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;
const int BUTTON_PIN = 12; // Arduino pin connected to button's pin
const int RELAY_PIN = 13; // Arduino pin connected to relay's pin
void setup() {
Serial.begin(9600);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
//Inledningsmeddelande i LCD'n
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Drop Test");
lcd.setCursor(0, 1);
delay(1000);
lcd.clear();
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read new state
if (buttonState == LOW) {
digitalWrite(RELAY_PIN, HIGH); // turn on
timer0 = micros();
} else if (buttonState == HIGH) {
digitalWrite(RELAY_PIN, LOW); // turn off
}
if (digitalRead(8) == LOW && flag1 == 0) {
timer1 = micros();
flag1 = 1;
}
if (digitalRead(9) == LOW && flag2 == 0) {
timer2 = micros();
flag2 = 1;
}
if (flag1 == 1) { Time01 = timer1 - timer0; }
if (flag1 == 1 && flag2 == 1) {
Time12 = timer2 - timer1;
}
Time01 = Time01 / 1000; //convert microsecond to millisecond
Time12 = Time12 / 1000; //convert microsecond to millisecond
if (digitalRead(10) == LOW && flag3 == 0) {
timer3 = micros();
flag3 = 1;
}
if (flag2 == 1 && flag3 == 1) {
Time23 = timer3 - timer2;
Time23 = Time23 / 1000; //convert microsecond to millisecond
}
if (digitalRead(11) == LOW && flag4 == 0) {
timer4 = micros();
flag4 = 1;
}
if (flag3 == 1 && flag4 == 1) {
Time34 = timer4 - timer3;
}
Time34 = Time34 / 1000; //convert microsecond to millisecond
if ((Time01 > 0) && (Time12 > 0) && (Time23 > 0) && (Time34 > 0)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("t1: ");
lcd.print(Time01, 2);
lcd.print(" ms");
lcd.setCursor(0, 1);
lcd.print("t2: ");
lcd.print(Time12, 2);
lcd.print(" ms");
delay(7000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("t3: ");
lcd.print(Time23, 2);
lcd.print(" ms");
lcd.setCursor(0, 1);
lcd.print("t4: ");
lcd.print(Time34, 2);
lcd.print(" ms");
delay(7000);
lcd.clear();
flag1 = 0;
flag2 = 0;
flag3 = 0;
flag4 = 0;
}
}