Hello boys are days that I work on this project. At school we are studying the uniform motion and I thought of a little speed meter of a body. What I have available are two photocells (from what I understand others have already used them). The environment in which I have to show the project isn't the same environment in which I am developing the project and then, working with light sensors (photocells), I decided to calibrate the two photocells. So everything is right, but the problem that persists, however, is how to determine a movement using the calibrated values. Here is my code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7,3, POSITIVE);
int valoreSensore = 0, valoreSensore1 = 0, led = 13, led1 = 2, minSensore = 1023, maxSensore = 0, minSensore1 = 1023, maxSensore1 = 0, a = 0, b = 0, velocita = 0, distanza = 0.112, secondi = 0, val = 0, val1 = 0;
void setup() {
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
lcd.begin (16,2);
lcd.setBacklight(HIGH);
lcd.print("Creato da");
lcd.setCursor(0, 1);
lcd.print("**********");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sto calibrando");
lcd.setCursor(0, 1);
lcd.print("il primo sensore");
digitalWrite(led, HIGH);
while(millis() < 5000){
valoreSensore = analogRead(A0);
if(valoreSensore > maxSensore){
maxSensore = valoreSensore;
}
if(valoreSensore < minSensore){
minSensore = valoreSensore;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Primo sensore");
lcd.setCursor(0, 1);
lcd.print("calibrato!");
digitalWrite(led, LOW);
delay(2000);
lcd.clear();
lcd.print("Ora calibro il");
lcd.setCursor(0, 1);
lcd.print("secondo sensore!");
digitalWrite(led1, HIGH);
while(millis() < 8000){
valoreSensore1 = analogRead(A2);
if(valoreSensore1 > maxSensore1){
maxSensore1 = valoreSensore1;
}
if(valoreSensore1 < minSensore1){
minSensore1 = valoreSensore1;
}
}
digitalWrite(led1, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Secondo sensore");
lcd.setCursor(0, 1);
lcd.print("calibrato!");
delay(2000);
lcd.clear();
}
void loop() {
valoreSensore = analogRead(A0);
val = valoreSensore;
valoreSensore = map(valoreSensore, minSensore, maxSensore, 0, 255);
valoreSensore = constrain(valoreSensore, 0, 255);
valoreSensore1 = analogRead(A2);
val1 = valoreSensore1;
valoreSensore1 = map(valoreSensore1, minSensore1, maxSensore1, 0, 255);
valoreSensore1 = constrain(valoreSensore1, 0, 255);
if(val< valoreSensore){
digitalWrite(led, HIGH);
a = millis();
if(val1< valoreSensore1){
b = millis();
digitalWrite(led1, HIGH);
delay(2000);
digitalWrite(led, LOW);
digitalWrite(led1, LOW);
}else{
digitalWrite(led, LOW);
}
}
a = (a != 0) ? a * 0.001 : 0;
b = (b != 0) ? b * 0.001 : 0;
secondi = ((a - b) > 0) ? (a-b) : 0;
velocita = ((distanza / secondi)) ? (distanza / secondi) : 0;
if(velocita > 0){
lcd.clear();
lcd.print(String(velocita));
lcd.print(" m/s");
}
delay(1000);
}
In summary, what I need is to know how to determine the first and second passages, using calibrated sensors.
I'm desperate, I have to deliver the project tomorrow.
Sorry for my english,i'm italian!