Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bottango - Neopixel color changes
- // Controls colors of Neopixels via Color Event in Bottango
- // In Arduino IDE, replace BottangoArduinoCallbacks.cpp with this code
- // Tested with Bottango 0.7.1p7a
- // Edit the following:
- // - Pin Neopixels are connected to
- // - Number of Neopixels you have
- //
- // In Arduino software, add FastLED library:
- // Library Manager > Search for "FastLED", and install
- //
- // Then upload to your controller with Arduino IDE.
- //
- // In Bottango desktop app, set up a Color Event:
- // Build tab > Create Part > Custom Events > Color
- // On right panel:
- // Rename to NeoPixels
- // Select Live On Project Launch
- // Set Identifier to a descriptive name - use "neoColor" to match this code
- // NOTE: For more than 1 or 2 Neopixels you will need to power them with their own
- // power supply. See the Adafruit Neopixel Guide for details.
- // An Arduino UNO or ESP32 can not run them on their own, too many will
- // brown out & reset the controller, and the Bottango desktop app will show an error that
- // says, "The hardware driver has been set to not live."
- #include "BottangoArduinoCallbacks.h"
- #include "src/AbstractEffector.h"
- #include "src/Outgoing.h"
- #include "src/BottangoCore.h"
- // FastLED setup
- #include "FastLED.h"
- #define DATA_PIN 14 // Edit this to match your setup
- #define LED_TYPE NEOPIXEL
- #define COLOR_ORDER GRB
- #define NUM_LEDS 8 // Edit this - How many LEDs?
- #define BRIGHTNESS 75
- CRGB leds[NUM_LEDS];
- namespace Callbacks {
- // !!!!!!!!!!!!!!! //
- // !! CONTROLLER LIFECYCLE CALLBACKS !! //
- // !!!!!!!!!!!!!!! //
- // called AFTER a successful handshake with the Bottango application, signifying that this controller has started.
- // use for general case startup process
- // Effector registration will happen after this callback, in their own callback.
- // If you have effector registration specific needs, you should use onEffectorRegistered
- void onThisControllerStarted() {
- FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- }
- // called after the controller recieves a stop command. The controller will stop all movement, deregister all effectors
- // After which this call back is triggered.
- void onThisControllerStopped() {
- }
- // called each loop cycle. If you have timing based code you'd like to utilize outside of the Bottango animation
- // This callback occurs BEFORE all effectors process their movement, at the end of the loop.
- void onEarlyLoop() {
- // Example for triggering animations in your own logic, while in offline (save to code / SD card) mode
- // if not playing anything, play animation index 2 (the third exported animation) and set it to looping
- // if (BottangoCore::commandStreamProvider->streamIsInProgress() == false)
- // {
- // BottangoCore::commandStreamProvider->startCommandStream(2, true);
- // }
- //
- // The following will stop an offline animation from playing if any
- // BottangoCore::commandStreamProvider->stop();
- }
- // called each loop cycle.
- // This callback occurs AFTER all effectors process their movement, at the end of the loop.
- void onLateLoop() {
- }
- // !!!!!!!!!!!!!!! //
- // !! EFFECTOR CALLBACKS !! //
- // !!!!!!!!!!!!!!! //
- // All effectors have an identifier. It is an 8 char or less string. Check Bottango to see the identifier for a given effector in app.
- // for most effectors, it is the first pin in their set of pins
- // i2c effectors have the i2c address before the first pin
- // you query for an effector with a c string char array, instanitated at 9 characters (8 for the identifier, and a null terminating char)
- // The below are called by built in effectors at various stages in their lifecycle
- // called by an effector when registered, after registration is complete
- void onEffectorRegistered(AbstractEffector *effector) {
- // example, turn on built in LED if effector registered with identifier "1";
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- }
- // called by an effector when deregistered, before deregistration is complete
- void onEffectorDeregistered(AbstractEffector *effector) {
- // example, turn off built in LED if effector registered with identifier "1";
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "1") == 0)
- // {
- // pinMode(LED_BUILTIN, OUTPUT);
- // digitalWrite(LED_BUILTIN, LOW);
- // }
- }
- // called by effectors each loop with its current signal (example: servo PWM or stepper steps from home )
- // didChange is true if different from last update called
- void effectorSignalOnLoop(AbstractEffector *effector, int signal, bool didChange) {
- // example, set built in led for effector with identifier "1" based on if signal is greater than 1500
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "1") == 0)
- // {
- // pinMode(LED_BUILTIN, OUTPUT);
- // if (signal > 1500)
- // {
- // digitalWrite(LED_BUILTIN, HIGH);
- // }
- // else
- // {
- // digitalWrite(LED_BUILTIN, LOW);
- // }
- // }
- // another example, drive a custom motor, which you have coded to have a setSignal function
- // if (strcmp(effectorIdentifier, "myMotor") == 0)
- // {
- // myMotor->setSignal(signal);
- // }
- }
- // !!!!!!!!!!!!!!!!!!! //
- // !! CUSTOM EVENTS !! //
- // !!!!!!!!!!!!!!!!!!! //
- // The below are called by custom events so you can provide your own behaviours
- // called by a curved custom event any time the movement value is changed during a curved movement. (Movement is a normalized float between 0.0 - 1.0)
- void onCurvedCustomEventMovementChanged(AbstractEffector *effector, float newMovement) {
- // example, fade an led based on the new movement value
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "myLight") == 0)
- // {
- // pinMode(5, OUTPUT);
- // int brightness = 255 * newMovement;
- // analogWrite(5, brightness);
- // }
- }
- // called by a on off custom event any time the on off value is changed.
- void onOnOffCustomEventOnOffChanged(AbstractEffector *effector, bool on) {
- // example, turn on built in led based on the on off value
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "Cylon") == 0) { // Edit the Identifier ("Cylon") to match On/Off event
- // Cylon_animating = !Cylon_animating;
- // }
- }
- // called by a trigger custom event any time the on event is triggered.
- void onTriggerCustomEventTriggered(AbstractEffector *effector) {
- // example, set led to a random brightness each trigger
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "myLight") == 0)
- // {
- // pinMode(5, OUTPUT);
- // int brightness = random(0, 256);
- // analogWrite(5, brightness);
- // }
- }
- void onColorCustomEventColorChanged(AbstractEffector *effector, byte newRed, byte newGreen, byte newBlue) {
- char effectorIdentifier[9];
- effector->getIdentifier(effectorIdentifier, 9);
- if (strcmp(effectorIdentifier, "neoColor") == 0) {
- fill_solid(leds, NUM_LEDS, CRGB(newRed, newGreen, newBlue));
- FastLED.show();
- }
- }
- bool isEffectorAutoHomeComplete(AbstractEffector *effector, int &postAutoSyncMove, int autoSyncDirection) {
- // return true if the given stepper is at home position
- // else return false
- // example, end homing on stepper with step on pin 6, when pin 10 is read high
- // int autoSyncDirection is the direction (+1 or -1) of the auto sync movement
- // postAutoSyncMove is additional steps you'd like to take after auto home is complete
- // as an example, if you want to hit a limit switch, then move 200 steps clockwise
- // set postAutoSyncMove = 200 and return true when the limit switch is hit
- // set postAutoSyncMove to a negative number to step counter clockwise
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "6") == 0)
- // {
- // pinMode(10, INPUT);
- // if (digitalRead(10) == HIGH)
- // {
- // return true;
- // }
- // }
- return false;
- }
- void onEffectorPostAutoHomeSecondarySyncComplete(AbstractEffector *effector) {
- // if moving a stepper after homing via the postAutoSyncMove int in isStepperAutoHomeComplete callback
- // this callback is called after that additional move is complete
- // in this example, after doing the additional post limit switch steps
- // if the effector has an identifer of "6", set pin 12 high
- // char effectorIdentifier[9];
- // effector->getIdentifier(effectorIdentifier, 9);
- // if (strcmp(effectorIdentifier, "6") == 0)
- // {
- // digitalWrite(12, HIGH);
- // }
- }
- void onEffectorHomeReset(AbstractEffector *effector) {
- // callback when an effector is no longer considered homed, use for you own internal state tracking if needed.
- }
- } // namespace Callbacks
Advertisement
Add Comment
Please, Sign In to add comment