Hi!!! I'm new on arduino and probably my problem is so simple, but I cannot understand it jeje
I'm trying to connect a neopixel ring to my arduino, through a mux CD74HC4067, but I'm not able to make it work.
With direct connection to my board, the neopixel works but through the mux... Only achieved to make it work once. It's too strange. here is my code, and USING
S0 - Pin 8
S1 - Pin 9
S2 - Pin 10
S3 - Pin 11
Signal - A0
Also, add my code here. Thanks in advance
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define MUX_S0 8
#define MUX_S1 9
#define MUX_S2 10
#define MUX_S3 11
#define MUX_SIG A0
#define NEOPIXEL_TYPE NEO_GRB + NEO_KHZ800
#define NUM_PIXELS 24
#define NUM_STRIPS 1
#define DELAYVAL 100 // Time (in milliseconds) to pause between pixels
#define SERIAL_PORT 9600
Adafruit_NeoPixel pixels(NUM_PIXELS, MUX_SIG, NEO_GRB + NEO_KHZ800);
void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
pinMode(MUX_SIG, OUTPUT);
pinMode(MUX_S0, OUTPUT);
pinMode(MUX_S1, OUTPUT);
pinMode(MUX_S2, OUTPUT);
pinMode(MUX_S3, OUTPUT);
Serial.begin(SERIAL_PORT);
}
void SetMuxChannel(byte channel) {
digitalWrite(MUX_S0, bitRead(channel, 0));
digitalWrite(MUX_S1, bitRead(channel, 1));
digitalWrite(MUX_S2, bitRead(channel, 2));
digitalWrite(MUX_S3, bitRead(channel, 3));
}
void setColor(int red, int green, int blue, int pixelsNum = NUM_PIXELS) {
pixels.clear();
for (int i = 0; i < pixelsNum; i++) {
pixels.setPixelColor(i, pixels.Color(red, green, blue));
}
pixels.show(); // Send the updated pixel colors to the hardware.
}
int* readSerial() {
static int valores[3];
if (Serial.available()) { // Si hay datos disponibles en el puerto serial
String entrada = Serial.readStringUntil('\n'); // Leer los datos enviados hasta encontrar el carácter '\n'
char* ptr = strtok(const_cast<char*>(entrada.c_str()), ","); // Separar los valores utilizando el caracter ','
int i = 0;
while (ptr != NULL && i < 3) { // Leer los valores separados y guardarlos en el arreglo valores
valores[i] = atoi(ptr);
ptr = strtok(NULL, ",");
i++;
}
return valores;
}
return nullptr;
}
void loop() {
int mux_channel = 0;
int* color = readSerial();
for (mux_channel = 0; mux_channel < NUM_STRIPS; mux_channel++) {
SetMuxChannel(mux_channel);
if (color != nullptr) {
setColor(color[0], color[1], color[2]);
}
}
delay(DELAYVAL); // Pause before next pass through loop
}