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