I am currently doing a project that requires me to use Matlab data to communicate with an Arduino. The Arduino, using this data then lights specific LEDs with 3 linked shift registers. The Arduino setup is very similar to that on the shift register tutorial on the Arduino site [http://arduino.cc/en/tutorial/ShiftOut]. I was able to use serial communication successfully to transmit the data and the Arduino correctly lights up the LEDs. However, after a few seconds, the LEDs will just shut off. I am not really sure what is happening except that I think it is a problem with the serial communication. Here is the code I am using:
MATLAB:
fclose('all');
close all
delete(instrfindall);
s = serial('/dev/tty.usbmodem1411');
s.BaudRate = 115200;
fopen(s);
pause(2);
numSR = 3; %number of Shift Registers
srDataArray = zeros(1, numSR);
data = [1,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1];
for i=1:numSR
offset = 8*(i-1);
byte = data((1+offset):(8+offset));
str_x = num2str(byte);
str_x(isspace(str_x)) = '';
srDataArray(i) = bin2dec(str_x);
end
disp(srDataArray);
dataString = mat2str(srDataArray);
fprintf(s,dataString);
disp('Lights should be on...');
fclose(s);
Arduino:
#include <SPI.h>
const int ssPin = 10;
void setup(){
pinMode(ssPin,OUTPUT);
digitalWrite(ssPin,HIGH);
SPI.begin();
SPIwrite(0,0,0); //FLUSH SHIFT REGISTERS
Serial.begin(115200);
}
void loop(){
if(Serial.available()>0){
int a = Serial.parseInt();
int b = Serial.parseInt();
int c = Serial.parseInt();
SPIwrite(a,b,c);
}
}
void SPIwrite(int x, int y, int z){
digitalWrite(ssPin,LOW);
SPI.transfer(x);
SPI.transfer(y);
SPI.transfer(z);
digitalWrite(ssPin,HIGH);
}
I should mention that my Arduino code uses the built in SPI library