I did some coding and tryed to convert my integer patterns into bytes, submit them and turn each digit into a boolean.
I guess this works fine, but it seems there is still a submission problem.
I wired up a test matrix(4x4) with one tlc5940 and tryed to only affect the LEDs 1-16. But the result is a bit strange: LED 14 and LED 12 are off the whole time and LED 2 is flickering and then of for a litte bit longer(~half a second) the other 13LEDs are always on.
This is my code for the processing interface:
//***********************table_grid.pde***********************************//
//************************************************************************//
//**********************by Mathias Kuse***********************************//
//************************************************************************//
//provides controll interface for DER_TISCH v.7***************************//
//************************************************************************//
//special thanks to: extrapixel (bits2byte) and grumpy_mike(arduino forum)//
import cc.arduino.*;
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
Serial arduino;
Minim minim;
AudioPlayer song;
BeatDetect beat;
int i=0;
//defines pattern
int b = 0;
int z = 0;
int counter = 0;
color currentcolor;
RectButton field[][], lock, play;
int cols = 8;
int rows = 8;
boolean locked = true;
boolean play_song = false;
byte[][] byte_pattern = new byte[8][8];
//define patterns
int[][][] pattern = new int[][][] {{{0,1,1,1,0,1,1,1},{1,0,0,0,1,1,1,1},{1,0,0,0,1,1,1,1},{1,1,1,1,1,1,1,1},{1,0,0,0,1,1,1,1},{1,0,0,0,1,1,1,1},{1,0,0,0,1,1,1,1},{1,0,0,0,1,1,1,1}},
{{0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0},{0,1,0,1,0,1,0,1},{1,0,1,0,1,0,1,0}},
{{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1}},
{{1,1,1,1,1,1,1,0},{1,1,1,1,1,1,0,1},{1,1,1,1,1,0,1,1},{1,1,1,1,0,1,1,1},{1,1,1,0,1,1,1,1},{1,1,0,1,1,1,1,1},{1,0,1,1,1,1,1,1},{0,1,1,1,1,1,1,1}},
{{0,1,1,1,1,1,1,0},{1,0,1,1,1,1,0,1},{1,1,0,1,1,0,1,1},{1,1,1,0,0,1,1,1},{1,1,1,0,0,1,1,1},{1,1,0,1,1,0,1,1},{1,0,1,1,1,1,0,1},{0,1,1,1,1,1,1,0}},
{{0,1,1,1,1,1,1,0},{1,0,1,1,1,1,0,1},{1,1,0,0,0,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,0,0,0,1,1},{1,0,1,1,1,1,0,1},{0,1,1,1,1,1,1,0}},
{{0,1,1,1,1,1,1,0},{1,0,0,0,0,0,0,1},{1,0,1,1,1,1,0,1},{1,0,1,1,1,1,0,1},{1,0,1,1,1,1,0,1},{1,0,1,1,1,1,0,1},{1,0,0,0,0,0,0,1},{0,1,1,1,1,1,1,0}},
{{0,0,0,0,0,0,0,0},{0,1,1,1,1,1,1,0},{0,1,1,1,1,1,1,0},{0,1,1,1,1,1,1,0},{0,1,1,1,1,1,1,0},{0,1,1,1,1,1,1,0},{0,1,1,1,1,1,1,0},{0,0,0,0,0,0,0,0}}
};
void setup(){
//convert all patterns to bytes
for(int i = 0; i < pattern.length; i++) {
for(int j = 0; j < 8; j++) {
byte_pattern[i][j] = bits2byte(pattern[i][j]);
}
}
//size of the window
size(640, 800);
smooth();
// define and create rectangle buttons
color buttoncolor = color(80,80,80);
color highlight = color(255,0,0);
field = new RectButton[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Initialize each object
field[i][j] = new RectButton(i*80,j*80,80,buttoncolor, highlight);
}
}
//define and create navigate buttons
lock = new RectButton(0, 680, 50, color(0,100,0), color (0,200,0));
play = new RectButton(60, 680, 50, color(0,0,100), color (0,000,200));
//define and initialize Arduino
//print(Serial.list());
arduino = new Serial(this, Serial.list()[1], 115200);
//initialize Minim library
minim = new Minim(this);
//load track
song = minim.loadFile("track.mp3", 2048);
//initialize beat detection
beat = new BeatDetect();
beat.setSensitivity(400);
}
void draw()
{
background(0);
stroke(255);
update(mouseX, mouseY);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// display each object
field[i][j].display();
}
}
lock.display();
play.display();
//execute beat detection
if(play_song == true) {
beat.detect(song.mix);
if (beat.isOnset()) {
//do this for every 2nd beat
if (counter < 2) {
//sending pattern to arduino
sendPattern(byte_pattern[z]);
if(z < 7) z++; else z = 0;
print(byte_pattern[z]);
counter++;
}else {
counter = 0;
}
}
}
}
void sendPattern(byte[] pattern) {
for(int i = 0; i < 8; i++) {
arduino.write(pattern[i]);
}
}
// turns an int-array into a byte
byte bits2byte(int[]bits) {
byte returnByte = 0x00; // = 00000000
byte bitMask = 0x01; // = 00000001
if (bits.length==8) {
// loop through the 8 bits
for (int bitNr=7; bitNr>=0; bitNr--) {
if (bits[bitNr]==1) {
returnByte = (byte) (returnByte | bitMask);
}
bitMask = (byte) (bitMask << 1) ;
}
}
return returnByte;
}
//checks weather a bit "n" from a byte is 1 or 0
boolean is_on(int ledNumber) {
if ((b & 1 << ledNumber) != 0) return true; else return false;
}
void update(int x, int y)
{
if(locked == false) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// update each object
field[i][j].update();
}
}
lock.update();
play.update();
}
if(mousePressed) {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// display each object
if(field[i][j].pressed()) {
field[i][j].basecolor= color(255,0,0);
}
}
}
if(lock.pressed()) {
if(locked == false) {
locked = true;
print (locked);
}
else {
locked = false;
print(locked);
}
}
if(play.pressed()) {
play.basecolor=color(0,0,255);
song.play();
play_song = true;
}
}
}
//Button class
class Button
{
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if(over()) {
currentcolor = highlightcolor;
}
else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if(over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
}
//RectButton class
class RectButton extends Button
{
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
and this is the arduino code:
#include "tlc_config.h"
#include "Tlc5940.h"
byte input[8];
void setup() {
Tlc.init();
Serial.begin(115200);
}
void loop() {
if(Serial.available() > 0) {
for(int row = 0; row <8; row++) {
input[row] = Serial.read();
}
draw_matrix(input);
}
}
//checks weather a bit "n" from a byte is 1 or 0
boolean is_on(byte b, int ledNumber) {
if ((b & 1 << ledNumber) != 0) return true; else return false;
}
void draw_matrix(byte row[]) {
//each row of the matrix
for(int i = 0; i < 8; i++) {
for(int a = 0; a < 8; a++) {
int led_num = (i+1)*a;
if (is_on(row[i], a) && (led_num < 16)) Tlc.set(led_num, 4095); else Tlc.set(led_num, 0);
}
}
Tlc.update();
}
I hope this is not to much code.
Thanks
Alec