標籤:

不要再被手寫1000組數據支配啦!用arduino自動測量測量數據點

亥姆霍茲線圈磁場的實驗都做過了,在座的各位可都是親手記下1000個數據點的男人,深入貫徹落實了當年從算盤造出原子彈的精神。。。。

後來發現arduino還真是個挺好玩的東西(不過這內存也太小了吧。。。只能犧牲一下用戶體驗了)

只用了個超聲測距模塊(其實完全可以換個滑動變阻器測距)

其實這個超聲測距的模塊真的沒有弄懂。。。測距離的部分直接借鑒了別人的,而且這個58是從哪來的完全沒弄明白。

另外一開始還沒有發現這個低級錯誤,沒有加.0結果出來的cm全是整數,也是很難受。

char workMod;

const int TrigPin = 2;

const int EchoPin = 3;

int voltagePin=A0;

float cm;

int verifyTimes=0;

float voltage1;

float voltage2;

float voltage3;

float voltage4;

float V_1[21]={0};

float V_2[21]={0};

float V_3[21]={0};

float V_4[21]={0};

float voltage;//單位毫伏

float ratio;

float magneticField;

float sensitivity;

void setup() {

Serial.begin(9600);

Serial.write("Welcome! Please send your order
");

Serial.println("Send "S" to set the ultrasonic sensor");

Serial.println("Send "K" to measure the sensitivity of the hall chip");

Serial.println("Send "M" to measure the field");

// put your setup code here, to run once:

}

void loop() {

while(!Serial.available()) delay(1000);

workMod=Serial.read();

switch(workMod){

case(S):Serial.println("Ive received your S order and you will be instructed to adjust the ultrasonic distance sensor");delay(3000);

Serial.println("make sure to use something as a reflector");delay(3000);

Serial.println("attach the sensor");delay(3000);

Serial.println("move the sensor with the linkage");delay(3000);

Serial.println("move to the 5cm site, put the reflector so that the sensor reply "5cm"");delay(3000);

while(cm!=5){

dis();

delay(1000);

if(cm==5) break;

}

Serial.println("clever boy! But we need to verify the stability. Stablize the reflector so that the sensor reply "5cm" for five times");

while(verifyTimes<5){

dis();

delay(1000);

if(cm==5) verifyTimes++;

else verifyTimes=0;

}

verifyTimes=0;

Serial.println("Youve done something! Lets verify the 15cm site");

while(verifyTimes<5){

dis();

delay(1000);

if(cm==15) verifyTimes++;

else verifyTimes=0;

}

verifyTimes=0;

Serial.println("Great!");

break;

case(K):Serial.println("You send K, right? First, help me revise my voltagemeter");delay(3000);

voltage=0;//只是初始化一下。。。

Serial.println("Whats on your voltagemeter? send the number as X.XX");delay(3000);

while(!Serial.available()) delay(1000);

voltage=Serial.read()-0;

Serial.read();//讀去小數點,因為arduino不能讀取浮點數,只好手寫了。。。

voltage+=((float)Serial.read()-0)*0.1;

voltage+=((float)Serial.read()-0)*0.01;

Serial.println(voltage);

Serial.println(analogRead(voltagePin));

ratio=((float)analogRead(voltagePin))/voltage;

Serial.println(ratio);

Serial.println("lets begin the first experiment, send any letter to measure V_1");delay(3000);

while(!Serial.available()) delay(1000);

Serial.read();

voltage1=((float)analogRead(voltagePin))/ratio;

Serial.println(voltage1);

Serial.println("send any letter to get V_2");delay(3000);

while(!Serial.available()) delay(1000);

Serial.read();

voltage2=((float)analogRead(voltagePin))/ratio;

Serial.println(voltage2);

Serial.println("send any letter to get V_3");delay(3000);

while(!Serial.available()) delay(1000);

Serial.read();

voltage3=((float)analogRead(voltagePin))/ratio;

Serial.println(voltage3);

Serial.println("send any letter to get V_4");delay(3000);

while(!Serial.available()) delay(1000);

Serial.read();

voltage4=((float)analogRead(voltagePin))/ratio;

Serial.println(voltage4);

voltage=(voltage1-voltage2+voltage3-voltage4)/4.0;

Serial.println(" magnetic field? Be sure to send the number as X.XX");delay(3000);

while(!Serial.available()) delay(1000);

magneticField=Serial.read()-0;

Serial.read();//讀去小數點,因為arduino不能讀取浮點數,只好手寫了。。。

magneticField+=((float)Serial.read()-0)*0.1;

magneticField+=((float)Serial.read()-0)*0.01;

sensitivity=magneticField/voltage;

Serial.print("the ratio of field to V_H is ");

Serial.println(sensitivity);delay(3000);

break;

case(M):

Serial.println("slowly move the device, Ill record the number, V_1 first");delay(3000);

cm=0;

while(cm<20){

dis();

if(V_1[(int) cm]==0) V_1[(int) cm]=((float)analogRead(voltagePin))/ratio;

Serial.println(((float)analogRead(voltagePin))/ratio);

delay(1000);

}

Serial.println("Then V_2 ");delay(3000);

cm=0;

while(cm<20){

dis();

if(V_2[(int) cm]==0) V_2[(int) cm]=((float)analogRead(voltagePin))/ratio;

Serial.println(((float)analogRead(voltagePin))/ratio);

delay(1000);

}

Serial.println("and V_3 ");delay(3000);

cm=0;

while(cm<20){

dis();

if(V_3[(int) cm]==0) V_3[(int) cm]=((float)analogRead(voltagePin))/ratio;

Serial.println(((float)analogRead(voltagePin))/ratio);

delay(1000);

}

Serial.println("last V_4 ");delay(3000);

cm=0;

while(cm<20){

dis();

if(V_4[(int) cm]==0) V_4[(int) cm]=((float)analogRead(voltagePin))/ratio;

Serial.println(((float)analogRead(voltagePin))/ratio);

delay(1000);

}

Serial.println("B-x relation as follows");delay(3000);

for(int c=1;c<=20;c++){

Serial.print(c);

Serial.print(" ");

magneticField=sensitivity*(V_1[c]-V_2[c]+V_3[c]-V_4[c])/4.0;

Serial.println(magneticField);

}

break;

}

Serial.println("Send "S" to set the ultrasonic sensor");

Serial.println("Send "K" to measure the sensitivity of the hall chip");

Serial.println("Send "M" to measure the field");

// put your main code here, to run repeatedly:

}

void dis(){

pinMode(TrigPin, OUTPUT);

pinMode(EchoPin, INPUT);

digitalWrite(TrigPin, LOW); //低高低電平發一個短時間脈衝去TrigPin

delayMicroseconds(2);

digitalWrite(TrigPin, HIGH);

delayMicroseconds(10);

digitalWrite(TrigPin, LOW);

cm = pulseIn(EchoPin, HIGH) / 58.0; //將回波時間換算成cm

Serial.print(cm);

Serial.println("cm");//串口輸出 Serial.println();

}


推薦閱讀:

來了!Office 2019!
松江大學城與NICE發布會
或許,你還沒有見過這樣的同濟和上海
我在同濟的日子

TAG:同濟大學 |