#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "Adafruit_NeoPixel.hpp"
#define PIN_WS2812B 28
#define PIN_LED 5
#define NUM_PIXELS 52
void main_core1() {
gpio_init(PIN_LED);
gpio_set_dir(PIN_LED, GPIO_OUT);
while (true) {
gpio_put(PIN_LED, 0);
sleep_ms(500);
gpio_put(PIN_LED, 1);
sleep_ms(500);
}
}
int main() {
stdio_init_all();
struct {
int max_pixels_on = 12;
int max_pixels_area = 4;
} modeSnake;
struct {
int max_pixels_area = 13;
int max_areas = 4;
} modeBlockFlashing;
// multicore_launch_core1(main_core1); // Core 1 is not working in Wokwi
Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
gpio_init(PIN_WS2812B);
gpio_set_dir(PIN_WS2812B, GPIO_OUT);
ws2812b.begin();
while (true) {
ws2812b.clear();
// // Snake with 3 colors (red, green and blue)
for (int times = 0; times < 40; times++){
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
// Set pixel to red
ws2812b.setPixelColor(pixel, ws2812b.Color(255, 0, 0));
// Set pixel to green
ws2812b.setPixelColor(pixel >= modeSnake.max_pixels_area ? pixel - modeSnake.max_pixels_area : NUM_PIXELS - (modeSnake.max_pixels_area - pixel), ws2812b.Color(0, 255, 0));
// Set pixel to blue
ws2812b.setPixelColor(pixel >= (modeSnake.max_pixels_area * 2) ? pixel - (modeSnake.max_pixels_area * 2) : NUM_PIXELS - ((modeSnake.max_pixels_area * 2) - pixel), ws2812b.Color(0, 0, 255));
// Set last pixel off
ws2812b.setPixelColor(pixel >= modeSnake.max_pixels_on ? pixel - modeSnake.max_pixels_on : NUM_PIXELS - (modeSnake.max_pixels_on - pixel), ws2812b.Color(0, 0, 0));
// Switch on/off leds
ws2812b.show();
sleep_ms(10);
}
}
ws2812b.clear();
// Block flashing with 2 colors (yellow and green)
bool bFirstColor = false;
for (int times = 0; times < 40; times++){
bFirstColor = !bFirstColor;
for (int area = 0; area < modeBlockFlashing.max_areas; area++){
bFirstColor = !bFirstColor;
for (int area_pixel = 0; area_pixel < modeBlockFlashing.max_pixels_area; area_pixel++) {
int pixel = area_pixel + (area * modeBlockFlashing.max_pixels_area);
ws2812b.setPixelColor(pixel, bFirstColor ? ws2812b.Color(0, 255, 0) : ws2812b.Color(255, 255, 0));
}
}
// Change leds
ws2812b.show();
sleep_ms(500);
}
}
}