I'm working on using a GUI in Processing to control some LED rope from Adafruit, and using this multiplexer.
I've gotten the following Arduino code working the way I want to cycle through the various strands of LED rope and turn them on:
#include <SoftwareSerial.h>
//Mux1 control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
//Mux2 control pins
int s4 = 12;
int s5 = 13;
int s6 = 14;
int s7 = 15;
//Mux in "SIG" pin
int SIG_pin = 3;
//Voltage vlaue to write to the LEDs
int testValue = 255;
void setup(){
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(s4, OUTPUT);
pinMode(s5, OUTPUT);
pinMode(s6, OUTPUT);
pinMode(s7, OUTPUT);
pinMode(SIG_pin, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
digitalWrite(s5, LOW);
digitalWrite(s6, LOW);
digitalWrite(s7, LOW);
}
void loop(){
for(int i = 0; i <= 19 ; i ++){
Serial.println(i);
testValue = 255;
writeMux(i);
delay(500);
// testValue = 0;
// writeMux(i);
}
}
int writeMux(int channel){
int controlPin[] = {s0, s1, s2, s3, s4, s5, s6, s7};
int muxChannel[20][8]={
//Mux 1
{0,0,0,0,0,0,0,0}, //channel 0
{1,0,0,0,0,0,0,0}, //channel 1
{0,1,0,0,0,0,0,0}, //channel 2
{1,1,0,0,0,0,0,0}, //channel 3
{0,0,1,0,0,0,0,0}, //channel 4
{1,0,1,0,0,0,0,0}, //channel 5
{0,1,1,0,0,0,0,0}, //channel 6
{1,1,1,0,0,0,0,0}, //channel 7
{0,0,0,1,0,0,0,0}, //channel 8
{1,0,0,1,0,0,0,0}, //channel 9
//Max 2
{1,1,1,1,0,0,0,0}, //channel 0
{1,1,1,1,1,0,0,0}, //channel 1
{1,1,1,1,0,1,0,0}, //channel 2
{1,1,1,1,1,1,0,0}, //channel 3
{1,1,1,1,0,0,1,0}, //channel 4
{1,1,1,1,1,0,1,0}, //channel 5
{1,1,1,1,0,1,1,0}, //channel 6
{1,1,1,1,1,1,1,0}, //channel 7
{1,1,1,1,0,0,0,1}, //channel 8
{1,1,1,1,1,0,0,1}, //channel 9
};
//loop through the 4 sig
for(int i = 0; i < 8; i ++){
digitalWrite(controlPin[i], muxChannel[channel][i]);
// Serial.println("CHANNEL IS ");
// Serial.println(muxChannel[channel][i]);
// Serial.println("Control pin IS ");
// Serial.println(controlPin[i]);
}
//write the value at the SIG pin
analogWrite(SIG_pin, testValue);
// Serial.println("Test value IS ");
// Serial.println(testValue);
//return the value
return 0;
}
I'm working on converting this Arduino code to a Processing sketch so I can control the lights with a GUI. I've run the Standard Firmata sketch on my Uno before running my Processing sketch. However, nothing seems to happen...I know that my circuit is working (since it worked for the Arduino sketch), and most of my Processing code should be correct. Logging shows the sketch is trying to write the values I want. Anyone have any ideas? Below is the relevant part of the Processing sketch:
import cc.arduino.*;
import processing.serial.*;
int [] muxChannel;
//Mux1 control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;
//Mux2 control pins
int s4 = 12;
int s5 = 13;
int s6 = 14;
int s7 = 15;
//Mux in "SIG" pin
int SIG_pin = 3;
//Voltage vlaue to write to the LEDs
int testValue = 255;
void setup() {
size(700,400);
noStroke();
cp5 = new ControlP5(this);
println(Arduino.list());
//// Set up Arduino and pin output modes
arduino = new Arduino(this, Arduino.list()[1], 96000);
arduino.pinMode(s0, Arduino.OUTPUT);
arduino.pinMode(s1, Arduino.OUTPUT);
arduino.pinMode(s2, Arduino.OUTPUT);
arduino.pinMode(s3, Arduino.OUTPUT);
arduino.pinMode(s4, Arduino.OUTPUT);
arduino.pinMode(s5, Arduino.OUTPUT);
arduino.pinMode(s6, Arduino.OUTPUT);
arduino.pinMode(s7, Arduino.OUTPUT);
arduino.pinMode(SIG_pin, Arduino.OUTPUT);
arduino.digitalWrite(s0, Arduino.LOW);
arduino.digitalWrite(s1, Arduino.LOW);
arduino.digitalWrite(s2, Arduino.LOW);
arduino.digitalWrite(s3, Arduino.LOW);
arduino.digitalWrite(s4, Arduino.LOW);
arduino.digitalWrite(s5, Arduino.LOW);
arduino.digitalWrite(s6, Arduino.LOW);
arduino.digitalWrite(s7, Arduino.LOW);
}
void draw() {
background(bgColor);
for(int i = 0; i <= 19 ; i ++){
testValue = 255;
writeMux(i);
delay(500);
}
}
int writeMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
final int[][] muxChannels = {
//Mux 1
{0,0,0,0,0,0,0,0}, //channel 0
{1,0,0,0,0,0,0,0}, //channel 1
{0,1,0,0,0,0,0,0}, //channel 2
{1,1,0,0,0,0,0,0}, //channel 3
{0,0,1,0,0,0,0,0}, //channel 4
{1,0,1,0,0,0,0,0}, //channel 5
{0,1,1,0,0,0,0,0}, //channel 6
{1,1,1,0,0,0,0,0}, //channel 7
{0,0,0,1,0,0,0,0}, //channel 8
{1,0,0,1,0,0,0,0}, //channel 9
//Max 2
{1,1,1,1,0,0,0,0}, //channel 0
{1,1,1,1,1,0,0,0}, //channel 1
{1,1,1,1,0,1,0,0}, //channel 2
{1,1,1,1,1,1,0,0}, //channel 3
{1,1,1,1,0,0,1,0}, //channel 4
{1,1,1,1,1,0,1,0}, //channel 5
{1,1,1,1,0,1,1,0}, //channel 6
{1,1,1,1,1,1,1,0}, //channel 7
{1,1,1,1,0,0,0,1}, //channel 8
{1,1,1,1,1,0,0,1}, //channel 9
};
//loop through the 4 sig
for(int i = 0; i < 4; i ++){
arduino.digitalWrite(controlPin[i], muxChannels[channel][i]);
}
//write the value at the SIG pin
arduino.analogWrite(SIG_pin, testValue);
int val = 0;
return val;
}
EDIT: I put the project and a little explainer on GitHub to show what this type of project can or can't be useful for here: https://github.com/narner/Analog-LED-Multiplexing-Example
muxChannels[][]
array, which wastes 128 bytes of RAM. Comments in the bildr.org webpage you referenced pointed out that it's unnecessary, but the webpage author had a lame excuse for not fixing it. Instead offor(int i = 0; i < 4; i ++) arduino.digitalWrite(controlPin[i], muxChannels[channel][i]);
, sayfor(int i=0; i<4; ++i) arduino.digitalWrite(controlPin[i], (channel>>i)&1);
or alternately,PORTB = (PORTB & 0xF0)|channel;
controlM2[]
and the channels are 0 to 9, tryfor(int i=0; i<4; ++i) if (firstMux()) arduino.digitalWrite(controlPin[i], (channel>>i)&1); else arduino.digitalWrite(controlM2[i], (channel>>i)&1);
or you could sayfor(int i=0; i<4; ++i) arduino.digitalWrite( firstMux()? controlPin[i] : controlM2[i], (channel>>i)&1);
wherefirstMux()
is true for the first mux but not the second