/* * webSerial for testing javascript connection with an arduino * * Latest work at https://github.com/hpssjellis/webMLserial * * Note: On the Arduino Serial monitor make sure no line-ending or if statements will not work * * Android https://hpssjellis.github.io/web-serial-polyfill/index.html * Laptops Desktops https://hpssjellis.github.io/my-examples-of-arduino-webUSB-webSerial/public/index.html * IOS not really sure * */ #include // Only needed for https://platformio.org/ #include #define CONVERT_G_TO_MS2 9.80665f #define FREQUENCY_HZ 50 #define INTERVAL_MS (1000 / (FREQUENCY_HZ + 1)) #define COLLECTION_SECONDS 10 int myMaxData = FREQUENCY_HZ * COLLECTION_SECONDS; int myCount = 0; int myDelay = INTERVAL_MS; // non-block delay in milliseconds unsigned long myStart; String readString; bool mySendData = true; void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); // onboard LED, HIGH = off while (!Serial) {} // do nothing and wait myStart = millis(); // set delay randomSeed(analogRead(A0)); // AO pin on XIAO does not have to be connected to anything Serial.println("timestamp,accX,accY,accZ"); // CSV file heading titles if (!IMU.begin()) { Serial.println("Failed to initialize IMU!"); while (1); } } void loop() { float x, y, z; if ( (millis() - myStart ) >= myDelay) { myStart = millis(); // reset the delay time myCount += 1; IMU.readAcceleration(x, y, z); //Serial.println( String(myStart)+ "," + String(analogRead(A0)) + "," + String(analogRead(A1)) + "," + String(analogRead(A2)) ); if (myCount >= myMaxData){ mySendData = false; // stop sending data when amount reached } if (mySendData){ Serial.println( String(myStart)+ "," + String(x) + "," + String(y) + "," + String(z) ); } } while (Serial.available()) { delay(3); char myChar = Serial.read(); readString += myChar; } if (readString.length() > 0) { if (readString == "a"){ digitalWrite(LED_BUILTIN, LOW); //onboard LED LOW = on } if (readString == "b"){ digitalWrite(LED_BUILTIN, HIGH); } if (readString == "start"){ mySendData = true; myCount = 0; } if (readString == "stop"){ mySendData = false; } if (readString == "firstline"){ Serial.println("timestamp,accX,accY,accZ"); // CSV file heading titles } readString=""; } }