# RGBW LEDs control through Matlab

Hello guys
I need a hand. For a project I have to send a sequence of bytes from Matlab to Arduino that control 36 RGBW LEDs with a precise time scan.

In particular, every 6 bytes must be sent every 2ms, so that my 36 LEDs (plus 1 byte to decide the color ) are kept switched on/off according to the precise pattern each 2ms.

The data is sent correctly but I don’t understand why LEDs do not precisely follow the time scan. It takes less than 2ms to send 6 bytes right? To send the entire array of bytes I send takes more than three times what It should take. Do you have any idea what the error might be or how I can get accurate time management?

``````  Nstep=32400;
Nled=48;
p=zeros(Nstep, Nled);

for j=1:Nstep-1
if(mod(idivide(j,int16(108)),2)==0)
for i=10:2:Nled
p(j+1,i)=1;
end
end
if(mod(idivide(j,int16(108)),2)~=0)
for i=9:2:Nled
p(j+1,i)=1;
end
end
if(j==1)
for i=10:2:Nled
p(1,i)=1;
end
end
end

logp=logical(p);

NumBytesp=size(logp,2)/8*size(logp, 1);
% reshape the matrix into an array, where each row repsresents one byte
Array23 = reshape(logp', [8, NumBytesp])';
% now convert it into a cell array, where each cell contains a matrix of 8
% elements
Cell23 = num2cell(Array23, 2);
% now we take a detour over string represenation of the values to create a
% byte variable in each cell entry

Bytes23 = cellfun(@(x) uint8(bin2dec(num2str(x))), Cell23);

BytesWp=Bytes23;

for j=1:6:size(Bytes23,1)
Bytes23(j)=1;
end

BytesRp=Bytes23;

for j=1:6:size(Bytes23,1)
Bytes23(j)=2;
end

BytesGp=Bytes23;

for j=1:6:size(Bytes23,1)
Bytes23(j)=3;
end

BytesBp=Bytes23;

delete(instrfindall);
arduinocom = serial('COM3', 'BaudRate', 500000);
fopen(arduinocom);
pause(2);

for k=1:NumBytesp
fprintf(arduinocom, '%c', char(BytesWp(k)));
if(mod(k-1,6)==0)
pause(0.002)
end
end

fclose(arduinocom);
``````

arduino:

``````#include <Wire.h>
#include <SPI.h>
#include <Servo.h>
//RCLK
int Pinlatch = 8;
////SER
int Pindata = 11;
//SRCLK
int Pinclock = 12;
int PIN;
byte datashift1=0;
byte datashift2=0;
byte datashift3=0;
byte datashift4=0;
byte datashift5=0;
String matlab;
int rc=0;

void setup()
{
pinMode(Pinlatch, OUTPUT);
Serial.begin(500000);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
digitalWrite(2,LOW); //R
digitalWrite(3,LOW); //G
digitalWrite(4,LOW); //B
digitalWrite(5,LOW); //W

}

void loop() {
// send data
if (Serial.available()) {

if(rc==0) {
if(matlab==0 || matlab==128)
PIN=5;
else if(matlab==1 || matlab==129)
PIN=2;
else if(matlab==2 || matlab==130)
PIN=3;
else if( matlab==3 || matlab == 131 )
PIN=4;
}
if(rc==1) {
datashift1=matlab;
}
if(rc==2) {
datashift2=matlab;
}
if(rc==3) {
datashift3=matlab;
}
if(rc==4) {
datashift4=matlab;
}
if(rc==5) {
datashift5=matlab;

pinMode(PIN,OUTPUT);
digitalWrite(PIN,HIGH);
digitalWrite(Pinlatch, 0);
shiftOut(Pindata, Pinclock, datashift1);
shiftOut(Pindata, Pinclock, datashift2);
shiftOut(Pindata, Pinclock, datashift3);
shiftOut(Pindata, Pinclock, datashift4);
shiftOut(Pindata, Pinclock, datashift5);
delay(2);
digitalWrite(Pinlatch, 1);
rc=-1;
}
rc++;
}
}

void shiftOut(int insDataPin, int insClockPin, byte insDataOut) {

int i=0;
int State;
pinMode(insClockPin, OUTPUT);
pinMode(insDataPin, OUTPUT);
digitalWrite(insDataPin, 0);
digitalWrite(insClockPin, 0);

for (i=7; i>=0; i--)  {
digitalWrite(insClockPin, 0);
if ( insDataOut & (1<<i) ) {
State= 1;
}
else {
State= 0;
}

digitalWrite(insDataPin, State);
digitalWrite(insClockPin, 1);
digitalWrite(insDataPin, 0);
}
//stop shifting
digitalWrite(insClockPin, 0);
}
``````

Thx guys!