// Using RMT driver
// #include "driver/rmt.h"
// #include "esp32-hal-log.h"
// #define LED_PIN GPIO_NUM_6
// #define RMT_CHANNEL RMT_CHANNEL_0
// Using FastLED library
// #include <FastLED.h>
// #define LED_PIN D6
// #define NUM_LEDS 1
// #define BRIGHTNESS 64
// #define LED_TYPE WS2812B
// #define COLOR_ORDER GRB
// CRGB leds[NUM_LEDS];
// Neopixel Library
#include <Adafruit_NeoPixel.h>
#define PIN_NEO_PIXEL D0 // The ESP32 pin GPIO16 connected to NeoPixel
#define NUM_PIXELS 3 // The number of LEDs (pixels) on NeoPixel
Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);
// Function to initialize RMT
// void setupRMT() {
// gpio_num_t LED_PIN = (gpio_num_t)6;
// rmt_config_t config;
// config.rmt_mode = RMT_MODE_TX;
// config.channel = RMT_CHANNEL;
// config.gpio_num = LED_PIN;
// config.mem_block_num = 1;
// config.tx_config.loop_en = false;
// config.tx_config.carrier_en = false;
// config.clk_div = 2; // Clock divider for timing
// rmt_config(&config);
// rmt_driver_install(config.channel, 0, 0);
// }
// Function to create WS2812 signal
// void sendWS2812Data(uint8_t *data, size_t len) {
// rmt_item32_t items[len * 24];
// for (size_t i = 0; i < len; i++) {
// for (size_t j = 0; j < 8; j++) {
// uint8_t bit = (data[i] >> (7 - j)) & 1;
// if (bit) {
// // Logic 1
// items[i * 24 + j].level0 = 1;
// items[i * 24 + j].duration0 = 8;
// items[i * 24 + j].level1 = 0;
// items[i * 24 + j].duration1 = 4;
// } else {
// // Logic 0
// items[i * 24 + j].level0 = 1;
// items[i * 24 + j].duration0 = 4;
// items[i * 24 + j].level1 = 0;
// items[i * 24 + j].duration1 = 8;
// }
// }
// }
// rmt_write_items(RMT_CHANNEL, items, len * 24, true);
// rmt_wait_tx_done(RMT_CHANNEL, portMAX_DELAY);
// }
void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C3!");
Serial.println("Welcome to Wokwi :-)");
// Setup with RMT driver
// setupRMT();
// uint8_t color[] = {0xFF, 0xFF, 0xFF}; // White color
// sendWS2812Data(color, sizeof(color));
// Setup Neopixel
NeoPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
// Test pixels
for(int i=0; i<NUM_PIXELS; i++){
NeoPixel.setPixelColor(i, NeoPixel.Color(255, 255, 255));
delay(1000);
}
NeoPixel.setBrightness(200); // a value from 0 to 255
NeoPixel.show(); // update to the NeoPixel Led Strip
delay(500); // 2 seconds off time
// Setup FastLED
// FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
// Code for RMT driver
// We try to control this one with raw RMT signal generation
// uint8_t color[] = {0xFF, 0xFF, 0xFF};
// color[0] = 0xFF; // Red color
// color[1] = 0x00; // Red color
// color[2] = 0x00; // Red color
// sendWS2812Data(color, sizeof(color));
// color[0] = 0x00; // Green color
// color[1] = 0xFF; // Green color
// color[2] = 0x00; // Green color
// sendWS2812Data(color, sizeof(color));
// color[0] = 0x00; // Blue color
// color[1] = 0x00; // Blue color
// color[2] = 0xFF; // Blue color
// sendWS2812Data(color, sizeof(color));
// Loop for NeoPixel
// NeoPixel.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called
// NeoPixel.show();
// delay(500);
for(int i=0; i<NUM_PIXELS; i++){
Serial.printf("Red %d\n", i);
NeoPixel.setPixelColor(i, NeoPixel.Color(255, 0, 0));
NeoPixel.show();
delay(500);
Serial.printf("Green %d\n", i);
NeoPixel.setPixelColor(i, NeoPixel.Color(0, 255, 0));
NeoPixel.show();
delay(500);
Serial.printf("Blue %d\n", i);
NeoPixel.setPixelColor(i, NeoPixel.Color(0, 0, 255));
NeoPixel.show();
delay(500);
NeoPixel.clear();
NeoPixel.show();
}
// Code for Fast LED
// fill_solid(leds, NUM_LEDS, CRGB::Red); // Set all LEDs to red
// FastLED.show();
// delay(1000);
// fill_solid(leds, NUM_LEDS, CRGB::Green); // Set all LEDs to green
// FastLED.show();
// delay(1000);
// fill_solid(leds, NUM_LEDS, CRGB::Blue); // Set all LEDs to blue
// FastLED.show();
// delay(1000);
// fill_rainbow(leds, NUM_LEDS, 0, 7); // Create a rainbow effect
// FastLED.show();
// delay(1000);
}