Skip to content

Instantly share code, notes, and snippets.

@bterlson
Created September 30, 2020 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bterlson/ba1930677e9fac30f1ad8d43eacb8ee2 to your computer and use it in GitHub Desktop.
Save bterlson/ba1930677e9fac30f1ad8d43eacb8ee2 to your computer and use it in GitHub Desktop.
Source code for a useless box.
import Servo from "pins/servo";
import Timer from "timer";
import Digital from "pins/digital";
import Monitor from "pins/digital/monitor";
import NeoPixel from "neopixel";
const doorServo = new Servo({pin: 5});
const armServo = new Servo({pin: 14});
const switchMonitor = new Monitor({
mode: Digital.InputPullUp,
pin: 18,
edge: Monitor.Falling
});
const pixel = new NeoPixel({length: 4, pin: 13, order: "GRB"});
pixel.brightness = 0;
const color = {
white: pixel.makeRGB(255, 255, 255),
blue: pixel.makeRGB(0, 0, 255),
red: pixel.makeRGB(255, 0, 0),
green: pixel.makeRGB(0, 255, 0)
}
const colorSequence = [color.white, color.blue, color.red, color.green];
let colorIndex = 0;
trace('startup\n');
switchMonitor.onChanged = function() {
if(switchMonitor.busy) {
return;
}
trace('starting sequence\n');
doSequence();
}
async function doSequence() {
switchMonitor.busy = true;
pixel.brightness = 255;
pixel.fill(colorSequence[colorIndex]);
pixel.update();
trace('opening door\n');
doorServo.write(90);
await delay(250);
trace('extending arm\n');
armServo.write(30);
await delay(500);
trace('retracting arm\n');
armServo.write(120);
await delay(250);
trace('closing door\n');
doorServo.write(180);
pixel.brightness = 0;
pixel.update();
await delay(150)
switchMonitor.busy = false;
trace('ending sequence');
colorIndex = colorIndex++ % 4;
}
function delay(ms) {
return new Promise(r => Timer.set(r, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment