// Brake / Tail Light (JavaScript version) // // by James Shaughnessy - See www.demonstudios.com/microbit.html for more micro:bit apps! // // // Run in a website via this link: // https://makecode.microbit.org/_X9oEboXfehL3 // /* Shared project: https://makecode.microbit.org/_X9oEboXfehL3 Raw HTML to embed the Code:
Raw HTML to embed the Editor: Raw HTML to embed the Simulator: */ // Installing Instructions For Copying and Pasting Code: // 1. Launch the MakeCode Editor from microbit.org // 2. Select "New Project" ensuring 'Code options' shows either "Blocks, JavaScript or Python" or "JavaScript Only" // 3. It launches in Blocks mode, so select JavaScript at the top to switch to JavaScript mode // 4. Highlight all the placeholder text in the window (is just an empty basic.forever() function) // 5. Paste this entire program into the empty window. It should automatically compile // 6. [Optional] Run on the editor's micro:bit to test it works OK // 7. Click 'Download' on the bottom left to save the HEX file // 8. Follow the instructions below for loading a downladed HEX file directly // // Installing Instructions For loading a downloaded HEX file directly: // 1. Connect your micro:bit to your computer. It should show up as a device called MICROBIT. // 2. Drag the downloaded HEX file onto the MICROBIT drive and the Operating System should install the code // 3. The program should run automatically // // // Changelog: // v1.0 (First release for CodeKingdoms JavaScript Editor) // v1.1 (Updated for the new MakeCode Editor in JavaScript mode (can be auto-converted to Blocks or Python)) // // // Instructions for Use: // This is a novelty brake/tail light for your bike, scooter, skateboard, or R/C car! // Using the accelerometer it will make a bright brake light come on when you brake, and if you brake // really hard the light will flash just like on some cars with LED taillights! There is also a dimmer // tail light that optionally stays on all the time. You can also choose between a round and a square // pattern for both the tail light and the brake light. // It works best when mounted perfectly vertically // // Button A: Toggle Head/Tail lights // Button B: Toggle between Round or Square light pattern // NOTE: Always use proper bicycle lights when riding at night! This is just a novelty for use in your // garden or in the park, not on the road! // Default variable values - defaults to a round tail light and a square brake light let tailLightOn = true; let squareModeTail = false; let squareModeBrake = true; let braking = false; input.onButtonPressed(Button.A, function () { // Toggle whether the tail light is on tailLightOn = !tailLightOn; }) input.onButtonPressed(Button.B, function () { if (braking) { // Toggle between a round and a square brake light squareModeBrake = !squareModeBrake; } else { // Toggle between a round and a square tail light squareModeTail = !squareModeTail; } }) basic.forever(function () { // Get the acceleration in the negative Z-axis (where 1G = 1024) let brakeForce = -input.acceleration(Dimension.Z); // Get a value that runs from 0-200 every 200ms using modulus (%) let flashTimer = Math.floor(input.runningTime() % 200); // Sets variable to see if we're braking or not. Change this value // to vary the sensitivity of the brake light (eg between 32 and 128) braking = (brakeForce > 64); // If we're braking and it's in square brake mode, OR // if we're not braking and the tail light is in square mode if ((braking && squareModeBrake) || (!braking && squareModeTail)) { // Light all LEDs showing square pattern basic.plotLeds(` # # # # # # # # # # # # # # # # # # # # # # # # # `); } else { // Light LEDs in a round pattern basic.plotLeds(` . # # # . # # # # # # # # # # # # # # # . # # # . `); } // If we're braking if (braking) { if (brakeForce > 768) { // Flash the leds between full brightness // and dim brightness every 1/10 second (100ms) if (flashTimer > 100) { led.setBrightness(64); } else { led.setBrightness(255); } } else { // Full LED brightness led.setBrightness(255); } } else { // Set LED brightness to fairly dim if (tailLightOn) { led.setBrightness(64); } else { // Turn off all LEDs led.setBrightness(0); } } })