|
| 1 | +/* This sketch demonstrates WebUSB as web serial with browser with WebUSB support (e.g Chrome). |
| 2 | + * For use with the Teachable Machine Tiny Sorter (and others!) project |
| 3 | + * See https://learn.adafruit.com/using-webusb-with-arduino-and-tinyusb for |
| 4 | + * software installation instructions and then |
| 5 | + * https://learn.adafruit.com/machine-learning-with-marshmallows-and-tiny-sorter |
| 6 | + * for usage tutorial |
| 7 | + * |
| 8 | + * Targetted to work with Circuit Playground Express but will work with any |
| 9 | + * board that has TinyUSB support - don't forget to select TinyUSB in the Tools menu! |
| 10 | + */ |
| 11 | + |
| 12 | +#include <Servo.h> |
| 13 | +#include <Adafruit_NeoPixel.h> |
| 14 | +#include "Adafruit_TinyUSB.h" |
| 15 | + |
| 16 | +// Which pin on the CPX/board is the Servo connected to? |
| 17 | +#define SERVO_PIN A1 |
| 18 | +Servo myservo; |
| 19 | + |
| 20 | +// Use internal neopixel ring |
| 21 | +#define NEOPIX_PIN 8 |
| 22 | +#define NUMPIXELS 10 |
| 23 | +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800); |
| 24 | + |
| 25 | +// USB WebUSB object |
| 26 | +Adafruit_USBD_WebUSB usb_web; |
| 27 | + |
| 28 | +// Landing Page: scheme (0: http, 1: https), url |
| 29 | +WEBUSB_URL_DEF(landingPage, 1 /*https*/, "learn.adafruit.com/machine-learning-with-marshmallows-and-tiny-sorter"); |
| 30 | + |
| 31 | +// the setup function runs once when you press reset or power the board |
| 32 | +void setup() |
| 33 | +{ |
| 34 | + usb_web.begin(); |
| 35 | + usb_web.setLandingPage(&landingPage); |
| 36 | + usb_web.setLineStateCallback(line_state_callback); |
| 37 | + |
| 38 | + Serial.begin(115200); |
| 39 | + |
| 40 | + // This initializes the NeoPixel with RED |
| 41 | + pixels.begin(); |
| 42 | + pixels.setBrightness(20); |
| 43 | + pixels.fill(0x0F0F0F); // dim white |
| 44 | + pixels.show(); |
| 45 | + |
| 46 | + // wait until device mounted |
| 47 | + while( !USBDevice.mounted() ) delay(1); |
| 48 | + pixels.fill(0x0F0F00); // dim yellow |
| 49 | + pixels.show(); |
| 50 | + |
| 51 | + Serial.println("TinyUSB WebUSB RGB example"); |
| 52 | + usb_web.print("Sketch begins.\r\n"); |
| 53 | + usb_web.flush(); |
| 54 | + pinMode(LED_BUILTIN, OUTPUT); |
| 55 | + |
| 56 | + myservo.attach(SERVO_PIN); |
| 57 | + myservo.write(60); |
| 58 | +} |
| 59 | + |
| 60 | +uint32_t last_valid_timestamp = 0; |
| 61 | + |
| 62 | +void loop() |
| 63 | +{ |
| 64 | + if ((millis() - last_valid_timestamp) > 200) { |
| 65 | + pixels.fill(0); |
| 66 | + pixels.show(); |
| 67 | + } |
| 68 | + |
| 69 | + if ( usb_web.available()) { |
| 70 | + char val; |
| 71 | + digitalWrite(LED_BUILTIN, HIGH); |
| 72 | + Serial.print("-> "); |
| 73 | + val = usb_web.read(); |
| 74 | + digitalWrite(LED_BUILTIN, LOW); |
| 75 | + Serial.print("Read value: "); Serial.println(val, DEC); |
| 76 | + |
| 77 | + if (val == 1) { // Target bin #1 |
| 78 | + pixels.fill(0xFFFF00); |
| 79 | + pixels.show(); |
| 80 | + last_valid_timestamp = millis(); |
| 81 | + } else if (val == 2) { // Target bin #2 |
| 82 | + pixels.fill(0xFF000FF); |
| 83 | + pixels.show(); |
| 84 | + last_valid_timestamp = millis(); |
| 85 | + } else { |
| 86 | + pixels.fill(0); |
| 87 | + pixels.show(); |
| 88 | + } |
| 89 | + } else { |
| 90 | + // no webserial data |
| 91 | + for (int pos = 60; pos <= 90; pos += 1) { // slowly goes from 60 degrees to 90 degrees |
| 92 | + myservo.write(pos); |
| 93 | + delay(3); |
| 94 | + } |
| 95 | + for (int pos = 90; pos >= 60; pos -= 1) { // goes back to 60 |
| 96 | + myservo.write(pos); |
| 97 | + delay(3); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void line_state_callback(bool connected) |
| 103 | +{ |
| 104 | + // connected = green, disconnected = red |
| 105 | + pixels.fill(connected ? 0x00ff00 : 0xff0000); |
| 106 | + pixels.show(); |
| 107 | +} |
0 commit comments