/
sketch.ino
49 lines (44 loc) · 1.18 KB
/
sketch.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
#include <WebUSB.h>
/**
* Creating an instance of WebUSBSerial will add an additional USB interface to
* the device that is marked as vendor-specific (rather than USB CDC-ACM) and
* is therefore accessible to the browser.
*
* The URL here provides a hint to the browser about what page the user should
* navigate to to interact with the device.
*/
WebUSB WebUSBSerial(1 /* https:// */, "webusb.github.io/arduino/demos/rgb");
#define Serial WebUSBSerial
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int color[3];
int colorIndex;
void setup() {
while (!Serial) {
;
}
Serial.begin(9600);
Serial.write("Sketch begins.\r\n");
Serial.flush();
colorIndex = 0;
}
void loop() {
if (Serial && Serial.available()) {
color[colorIndex++] = Serial.read();
if (colorIndex == 3) {
analogWrite(redPin, color[0]);
analogWrite(greenPin, color[1]);
analogWrite(bluePin, color[2]);
Serial.print("Set LED to ");
Serial.print(color[0]);
Serial.print(", ");
Serial.print(color[1]);
Serial.print(", ");
Serial.print(color[2]);
Serial.print(".\r\n");
Serial.flush();
colorIndex = 0;
}
}
}