Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////
- // Circuit Playground Express - Rotating Rainbow //
- // //
- // Author: thaufas //
- // //
- // Novel Concept: Only uses a delay() during setup, which permits more //
- // efficient processor usage. With this approach //
- // other processes can run without being blocked. //
- ///////////////////////////////////////////////////////////////////////////////
- #include <Adafruit_CircuitPlayground.h>
- const uint16_t BAUDRATE = 57600; // Set the serial communication BAUD rate
- const long SERIAL_CONFIG = SERIAL_8N1; // Set the serial configuration - default is SERIAL_8N1: 8 data bits, no parity, 1 stop bit
- const uint16_t LED_BRIGHTNESS = 128; // RANGE: 0-255, Higher is brighter
- const uint16_t NUM_PIXELS = 10; // RANGE: 0-9
- const uint16_t DISPLAYOFFTIME = 0; // milliseconds: Amount of time to turn LED off
- const uint16_t DISPLAYONTIME = 50; // milliseconds: Amount of time to turn LED on
- const uint16_t INTERPIXEL_UPDATE_DELAY = 0; // milliseconds: Time between initial lighting of pixels
- ///////////////////////////////////////////////////////////////////////////////
- // Define the class that will control the pixels and eliminate the //
- // the need for delays by monitoring and updating pixel state. //
- // This approach enables higher utilization of the microprocessor. //
- // Although the system is not truly multi-threaded, so long as //
- // no single task monopolizes the CPU, to the end user, the system //
- // will appear to be multi-threaded. //
- ///////////////////////////////////////////////////////////////////////////////
- class Pixel {
- private:
- uint8_t _pixel_id;
- uint8_t _LEDState;
- uint32_t _lastUpdateTime;
- uint32_t _timeIntervalOn;
- uint32_t _timeIntervalOff;
- uint32_t _nextTimeScheduledOff;
- uint32_t _nextTimeScheduledOn;
- uint32_t _colorValue;
- public:
- Pixel(uint8_t pixel_id = 0, uint32_t colorValue = 0x00FF00, uint32_t timeIntervalOn = 750, uint32_t timeIntervalOff = 500, uint8_t LEDState = LOW) :
- _pixel_id(pixel_id), _colorValue(colorValue), _timeIntervalOn(timeIntervalOn), _timeIntervalOff(timeIntervalOff), _LEDState(LEDState) {
- _lastUpdateTime = millis();
- if (_LEDState == HIGH ) {
- _nextTimeScheduledOff = _lastUpdateTime + _timeIntervalOn;
- _nextTimeScheduledOn = _nextTimeScheduledOff + _timeIntervalOff;
- } else {
- _nextTimeScheduledOn = _lastUpdateTime + _timeIntervalOff;
- _nextTimeScheduledOff = _nextTimeScheduledOn + _timeIntervalOn;
- }
- //Update();
- }
- bool Update() {
- if (_LEDState == HIGH && millis() > _nextTimeScheduledOff) {
- CircuitPlayground.setPixelColor(_pixel_id, 0x000000);
- _LEDState = LOW;
- _lastUpdateTime = millis();
- _nextTimeScheduledOn = _lastUpdateTime + _timeIntervalOff;
- return (true); // Return 'true' if updates were required.
- } else if (_LEDState == LOW && millis() > _nextTimeScheduledOff) {
- CircuitPlayground.setPixelColor(_pixel_id, _colorValue);
- _LEDState = HIGH;
- _lastUpdateTime = millis();
- _nextTimeScheduledOff = _lastUpdateTime + _timeIntervalOn;
- return (true); // Return 'true' if updates were required.
- } else {
- return (false); // Return 'false' if no updates were required.
- }
- }
- uint8_t SetPixelID(uint8_t pixel_id) {
- return (_pixel_id = pixel_id);
- }
- uint8_t GetPixelID() {
- return (_pixel_id);
- }
- uint32_t GetLastUpdateTime() {
- return (_lastUpdateTime);
- }
- uint32_t SetColorValue(uint32_t colorValue) {
- return (_colorValue = colorValue);
- }
- uint32_t GetColorValue() {
- return (_colorValue);
- }
- uint32_t SetTimeIntervalOn(uint32_t timeIntervalOn) {
- return (_timeIntervalOn = timeIntervalOn);
- }
- uint32_t GetTimeIntervalOn() {
- return (_timeIntervalOn);
- }
- uint32_t SetTimeIntervalOff(uint32_t timeIntervalOff) {
- return (_timeIntervalOff = timeIntervalOff);
- }
- uint32_t GetTimeIntervalOff() {
- return (_timeIntervalOff);
- }
- uint8_t SetLEDState(uint8_t LEDState) {
- return (_LEDState = LEDState);
- }
- uint8_t GetLEDState() {
- return (_LEDState);
- }
- uint32_t SetNextScheduledTimeOff(uint32_t nextTimeScheduledOff) {
- _nextTimeScheduledOff = nextTimeScheduledOff;
- return (_nextTimeScheduledOff);
- }
- uint32_t GetNextScheduledTimeOff() {
- return (_nextTimeScheduledOff);
- }
- uint32_t SetNextScheduledTimeOn(uint32_t nextTimeScheduledOn) {
- return (_nextTimeScheduledOn = nextTimeScheduledOn);
- }
- uint32_t GetNextScheduledTimeOn() {
- return (_nextTimeScheduledOn);
- }
- }; // End class Pixel
- void PrintDiagInfoToSerial(Pixel &P) {
- Serial.print("[");
- Serial.print(P.GetPixelID());
- Serial.print("]");
- Serial.print("\t");
- Serial.print("_LEDState==");
- Serial.print(P.GetLEDState());
- Serial.print("\t");
- Serial.print("millis()==");
- Serial.print(millis());
- Serial.print("\t");
- Serial.print("_timeIntervalOn==");
- Serial.print(P.GetTimeIntervalOn());
- Serial.print("\t");
- Serial.print("_timeIntervalOff==");
- Serial.print(P.GetTimeIntervalOff());
- Serial.print("\t");
- Serial.print("P.GetNextScheduledTimeOn()==");
- Serial.print(P.GetNextScheduledTimeOn());
- Serial.print("\t");
- Serial.print("P.GetNextScheduledTimeOff()==");
- Serial.print(P.GetNextScheduledTimeOff());
- Serial.print("\t");
- Serial.print("Color==");
- Serial.println(P.GetColorValue());
- }
- static uint16_t colorIndexCtr;
- static uint32_t colorValueArr[] = {
- 11815680, 6919680, 2023680, 53805, 34680,
- 15555, 983280, 5898405, 10813530, 15728655
- };
- Pixel Pixels[NUM_PIXELS]; // Declare the pixel array
- void setup() {
- Serial.begin(BAUDRATE, SERIAL_CONFIG);
- CircuitPlayground.begin(LED_BRIGHTNESS);
- CircuitPlayground.clearPixels();
- for (int i = 0; i < NUM_PIXELS; ++i) {
- Pixels[i].SetPixelID(i);
- Pixels[i].SetColorValue(colorValueArr[i]);
- Pixels[i].SetTimeIntervalOn(DISPLAYONTIME);
- Pixels[i].SetTimeIntervalOff(DISPLAYOFFTIME);
- Pixels[i].Update();
- delay(INTERPIXEL_UPDATE_DELAY);
- }
- }
- void loop()
- {
- static bool LEDWhiteState = false;
- static uint32_t lastUpdate = millis(), currTime;
- currTime = millis();
- if (currTime > lastUpdate + DISPLAYONTIME) {
- lastUpdate = currTime;
- ++colorIndexCtr;
- for (int i = 0; i < NUM_PIXELS; ++i) {
- Pixels[i].SetColorValue(colorValueArr[(i + colorIndexCtr) % NUM_PIXELS]);
- }
- }
- for (int i = 0; i < NUM_PIXELS; ++i)
- {
- Pixels[i].Update();
- if (CircuitPlayground.rightButton())
- {
- CircuitPlayground.setPixelColor(i, 0xFFFFFF);
- }
- if (CircuitPlayground.leftButton())
- {
- CircuitPlayground.setPixelColor(i, 0x0000FF);
- }
- if (CircuitPlayground.slideSwitch())
- {
- PrintDiagInfoToSerial(Pixels[i]);
- } else
- {
- PrintDiagInfoToSerial(CircuitPlayground);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment