void serialOutput(){ // Decide How To Output Serial.
if (serialVisual == true){
arduinoSerialMonitorVisual('-', Signal); // 去串行監視器展示台的功能goes to function that makes Serial Monitor Visualizer
} else{
sendDataToSerial('S', Signal); // 去sendDataToSerial函數goes to sendDataToSerial function
}
}
// 決定如何輸出BPM和IBI數據Decides How To OutPut BPM and IBI Data
void serialOutputWhenBeatHappens(){
if (serialVisual == true){ // 使串行監視器可視化工作的代碼 Code to Make the Serial Monitor Visualizer Work
Serial.print("*** Heart-Beat Happened *** "); //ASCII藝術瘋狂ASCII Art Madness
Serial.print("BPM: ");
btSerial.println("HEART RATE IS ");
btSerial.println(BPM);
Serial.print(BPM);
Serial.print(" ");
} else{
sendDataToSerial('B',BPM); // 用“B”前綴發送心率send heart rate with a 'B' prefix
sendDataToSerial('Q',IBI); // 用“Q”前綴在節拍之間發送時間send time between beats with a 'Q' prefix
}
}
// 將數據發送到脈衝傳感器處理應用程序,本地Mac應用程序或第三方串行讀取器Sends Data to Pulse Sensor Processing App, Native Mac App, or Third-party Serial Readers.
void sendDataToSerial(char symbol, int data ){
Serial.print(symbol);
Serial.println(data);
}
void ledFadeToBeat(){
fadeRate -= 15; // 設置LED淡入值set LED fade value
fadeRate = constrain(fadeRate,0,255); // 保持LED衰減價值進入負數keep LED fade value from going into negative numbers!
analogWrite(fadePin,fadeRate); // 淡入LEDfade LED
}
// Code to Make the Serial Monitor Visualizer Work
void arduinoSerialMonitorVisual(char symbol, int data ){
const int sensorMin = 0; // 傳感器最小值,通過實驗發現sensor minimum, discovered through experiment
const int sensorMax = 1024; // 傳感器最大值,通過實驗發現sensor maximum, discovered through experiment
int sensorReading = data;
// 將傳感器範圍映射到12個選項的範圍內map the sensor range to a range of 12 options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 11);
// 做一些不同的取決於do something different depending on the
// 範圍值range value:
switch (range) {
case 0:
Serial.println(""); /////ASCII Art Madness
break;
case 1:
Serial.println("---");
break;
case 2:
Serial.println("------");
break;
case 3:
Serial.println("---------");
break;
case 4:
Serial.println("------------");
break;
case 5:
Serial.println("--------------|-");
break;
case 6:
Serial.println("--------------|---");
break;
case 7:
Serial.println("--------------|-------");
break;
case 8:
Serial.println("--------------|----------");
break;
case 9:
Serial.println("--------------|----------------");
break;
case 10:
Serial.println("--------------|-------------------");
break;
case 11:
Serial.println("--------------|-----------------------");
break;
}
}
void printFloat(double number, int digits)
{
// 處理負數Handle negative numbers
if (number < 0.0)
{
Serial.print('-');
number = -number;
}
// 圓形正確打印(1.999,2)打印為“2.00”Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// 提取數字的整數部分並打印出來Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
Serial.print(int_part);
// 打印小數點,但只有在數字之外Print the decimal point, but only if there are digits beyond
if (digits > 0)
Serial.print(".");
// 一次提取餘數中的數字Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
Serial.print(toPrint);
remainder -= toPrint;
}
}
void gpsdump(TinyGPS &gps)
{
long lat, lon;
float flat, flon;
unsigned long age, date, time, chars;
int year;
byte month, day, hour, minute, second, hundredths;
unsigned short sentences, failed;
gps.get_position(&lat, &lon, &age);
Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
// 在Arduino上,在冗長的Serial.print()中可能會丟失GPS字符On Arduino, GPS characters may be lost during lengthy Serial.print()
// 在Teensy上,串行打印到USB,它具有較大的輸出緩沖和On Teensy, Serial prints to USB, which has large output buffering and
// 運行速度非常快,所以沒必要擔心丟失4800runs very fast, so it's not necessary to worry about missing 4800
// 波特GPS字符。baud GPS characters.
gps.f_get_position(&flat, &flon, &age);
Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
gps.get_datetime(&date, &time, &age);
Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): ");
Serial.print(time);
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/");
Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year);
Serial.print(" Time: "); Serial.print(static_cast<int>(hour+8)); Serial.print(":"); //Serial.print("UTC +08:00 Malaysia");
Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second));
Serial.print("."); Serial.print(static_cast<int>(hundredths)); Serial.print(" UTC +08:00 Malaysia");
Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): ");
Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed());
Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): ");
printFloat(gps.f_course()); Serial.println();
Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): ");
printFloat(gps.f_speed_mph());
Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): ");
printFloat(gps.f_speed_kmph()); Serial.println();
gps.stats(&chars, &sentences, &failed);
Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: ");
Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);
btSerial.println("Date(ddmmyy): "); btSerial.println(date);
btSerial.println(" Time(hhmmsscc): ");btSerial.println(time);
}
|