|
| 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 | + |
| 61 | +void loop() |
| 62 | +{ |
| 63 | + if ( usb_web.available()) { |
| 64 | + digitalWrite(LED_BUILTIN, HIGH); |
| 65 | + Serial.print("-> "); |
| 66 | + char val = usb_web.read(); |
| 67 | + digitalWrite(LED_BUILTIN, LOW); |
| 68 | + Serial.print("Read value: "); Serial.println(val, DEC); |
| 69 | + |
| 70 | + if (val == 1) { // Target bin #1 |
| 71 | + pixels.fill(0xFFFF00); |
| 72 | + pixels.show(); |
| 73 | + Serial.println("CEREAL!"); |
| 74 | + |
| 75 | + myservo.write(0); // push cereal to one side |
| 76 | + delay(2000); // wait |
| 77 | + for (int pos = 0; pos <= 75; pos++) { // return servo |
| 78 | + myservo.write(pos); |
| 79 | + delay(5); |
| 80 | + } |
| 81 | + delay(1000); // another wait before we continue |
| 82 | + |
| 83 | + } else if (val == 2) { // Target bin #2 |
| 84 | + pixels.fill(0xFF000FF); |
| 85 | + pixels.show(); |
| 86 | + Serial.println("MALLOW!"); |
| 87 | + |
| 88 | + myservo.write(180); // push mallows to other side |
| 89 | + delay(2000); // wait |
| 90 | + for (int pos = 180; pos >= 75; pos--) { // return servo |
| 91 | + myservo.write(pos); |
| 92 | + delay(5); |
| 93 | + } |
| 94 | + delay(1000); // another wait before we continue |
| 95 | + } |
| 96 | + pixels.fill(0); |
| 97 | + pixels.show(); |
| 98 | + |
| 99 | + while (usb_web.available()) { |
| 100 | + usb_web.read(); |
| 101 | + delay(10); |
| 102 | + } |
| 103 | + } else { |
| 104 | + // no webserial data, tick tock the servo |
| 105 | + for (int pos = 60; pos <= 90; pos++) { // slowly goes from 60 degrees to 90 degrees |
| 106 | + myservo.write(pos); |
| 107 | + delay(3); |
| 108 | + } |
| 109 | + for (int pos = 90; pos >= 60; pos--) { // goes back to 60 |
| 110 | + myservo.write(pos); |
| 111 | + delay(3); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void line_state_callback(bool connected) |
| 117 | +{ |
| 118 | + // connected = green, disconnected = red |
| 119 | + pixels.fill(connected ? 0x00ff00 : 0xff0000); |
| 120 | + pixels.show(); |
| 121 | +} |
0 commit comments