自由落體測定

#include <SoftwareSerial.h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

SoftwareSerial BT(9,8); // TX:10, RX:11

float x1= 302.0 ; //填入第一個sensor與第二個sensor的距離(毫米)

float x2= 299.20 ; //填入第二個sensor與第三個sensor的距離(毫米)

int photoPin1 = A1;

int photoPin2 = A2;

int photoPin3 = A3;

byte serialA;

int limit1 ;

int limit2 ;

int limit3 ;

int i = 0 ;

int photo1 ; // 第一個sensor的讀數

int photo2 ; // 第二個sensor的讀數

int photo3 ; // 第三個sensor的讀數

int a ;

int b ;

int c ;

long firstPass ; // 通過第一個sensor的時間(毫秒)

long secondPass ; // 通過第二個sensor的時間(毫秒)

long thirdPass ; // 通過第三個sensor的時間(毫秒)

long dt1 ; //通過第一個sensor與第二個sensor的時間差(毫秒)

long dt2 ; //通過第二個sensor與第三個sensor的時間差(毫秒)

int g1 ;

int g2 ;

int g3 ;

int g0 ;

float v1 ;

float v2 ;

float dv ;

float dt ;

float g ;

char command ;

void setup(){

lcd.init();

lcd.backlight();

Serial.begin(9600);

BT.begin(9600);

pinMode(10,OUTPUT);

}

void loop(){

if ( BT.available()>0 ){

command = BT.read();

switch (command){

case 'y' :

initial();

break;

case 'x' :

digitalWrite(10, LOW);

compute();

delay(100);

display();

delay(100);

bt();

break;

}

}

}

void initial(){

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("initial...");

digitalWrite(10, HIGH);

a=0 ;

b=0 ;

c=0 ;

for(int j=0 ; j<10 ; j++){

photo1 = analogRead(photoPin1);

a = a + photo1 ;

photo2 = analogRead(photoPin2);

b = b + photo2 ;

photo3 = analogRead(photoPin3);

c = c + photo3 ;

}

limit1 = a/10 - 10 ;

limit2 = b/10 - 10 ;

limit3 = c/10 - 10 ;

delay(555);

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("Ready...");

}

void compute(){

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("Ready......");

do{

photo1 = analogRead(photoPin1);

}while(photo1>limit1);

firstPass = millis();

do{

photo2= analogRead(photoPin2);

}while(photo2 > limit2);

secondPass = millis();

do{

photo3= analogRead(photoPin3);

}while(photo3 > limit3);

thirdPass = millis();

dt1 = secondPass - firstPass ;

v1 = x1 /(float)dt1;

dt2 = thirdPass - secondPass;

v2 = x2 / (float)dt2;

dv = (v2 - v1);

dt = 0.500*(thirdPass-firstPass);

g = 1000*dv/(float) dt ;

i=i+1 ;

}

void display(){

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("g=");

lcd.setCursor(2, 0);

lcd.print(g,3);

lcd.setCursor(9, 0);

lcd.print("i=");

lcd.setCursor(11, 0);

lcd.print(i);

/*

Serial.print( "i= " );

Serial.println(i);

Serial.print( "dt1= " );

Serial.println(dt1);

Serial.print( "v1= " );

Serial.println(v1,4);

Serial.print( "dt2= " );

Serial.println(dt2);

Serial.print( "v2= " );

Serial.println(v2,4);

Serial.print( "g= " );

Serial.print(g,4);

Serial.println("m/s2 ");

Serial.println(" ");

*/

}

void bt(){

byte Data[18];

byte cmmd[20];

int insize;

g0 = int(g * 1000) ;

if(g>=0){

g1 = g0/1000 ;

g2 = g0 % 1000 ;

g3 = 1 ;

}

if(g<0){

g1 = -1 * g0 / 1000 ;

g2 = -1 * g0 % 1000 ;

g3 = 2 ;

}

serialA=BT.read();

Data[0]='a';

Data[1]=i/256;

Data[2]=i%256;

Data[3]='b';

Data[4]=dt1/256;

Data[5]=dt1%256;

Data[6]='c';

Data[7]=dt2/256;

Data[8]=dt2%256;

Data[9]='d';

Data[10]=g1/256;

Data[11]=g1%256;

Data[12]='e';

Data[13]=g2/256;

Data[14]=g2%256;

Data[15]='f';

Data[16]=g3/256;

Data[17]=g3%256;

if (serialA == 49){

for(int j=0;j<18;j++)

BT.write(Data[j]);

serialA=0;

}

delay(111);

}