-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino04-webserial.ino
69 lines (52 loc) · 1.63 KB
/
arduino04-webserial.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* webSerial for testing javascript connection with an arduino
*
* 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 <Arduino.h> // Only needed for https://platformio.org/
String readString;
int myDelay = 5000; // non-block delay in milliseconds
unsigned long myStart;
int serialMessage = 1;
int myCount=48; //48 = ascii 0, 58 ascii 9
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // onboard LED, HIGH = off
myStart = millis(); // set delay
randomSeed(analogRead(A0)); // AO pin on XIAO does not have to be connected to anything
}
void loop() {
if ( (millis() - myStart ) >= myDelay) {
myStart = millis(); // reset the delay time
myCount++;
if (myCount >= 58){ // 48 = ascii 0, 58 ascii 9
myCount = 48;
}
//char myChar = (char)myCount;
byte myChar = (byte)myCount;
Serial.write(myChar);
}
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() > 0) {
Serial.println(readString);
if (readString == "a"){
digitalWrite(LED_BUILTIN, LOW); //onboard LED LOW = on
Serial.println("LED ON");
}
if (readString == "b"){
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED OFF");
}
readString="";
}
}