64 button shield & arduino with matlab

1 view (last 30 days)
Donald
Donald on 26 Nov 2011
I'm trying to get the arduino to send the button data to matlab to work with my GUI which is simulating an alarm panal.
The button code works fine and when I open the com port I see the button press.
What i need help on is how do I have Matlab receive this in my GUI?
The code for the 64 button shield is:
void setup()
{
Serial.begin(115200); // Only used to send data to PC for this DEMO
delay(1000);
attachInterrupt(0, SPI64B, FALLING); // Required for 64 Button Shield (SPI Only)
}
//*******************************************************************************************************************
// Main Loop
//*******************************************************************************************************************
void loop()
{
if(Button > 0) // If Button is > 0, then it was pressed or released (SPI only)
{
Serial.print("Button: ");
if(Button > 128) // Example of how to decode the button press
{
Button = Button - 128; // A pressd button is the button number + 128
Serial.print(Button, DEC);
Serial.print(" - Pressed");
}else
{
Serial.print(Button, DEC); // A released button is from 1 to 64
Serial.print(" - Released");
}
Serial.println(" ");
Button = 0;
}
}
//*******************************************************************************************************************
// Required for 64 Button Shield (SPI Only) Functions & Subroutines
//*******************************************************************************************************************
//
// This void is called if the Interrupt 0 is triggered (digital pin 2).
//
void SPI64B()
{
Button = 0;
volatile uint8_t val = 0;
volatile uint8_t clk = 0;
#define DataInPin 3
#define ClkPin 4
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
for(volatile int i =0; i<8;i++)
{
val = digitalRead(DataInPin);
clk = digitalRead(ClkPin);
while(clk == HIGH)
{
clk = digitalRead(ClkPin);
}
if(val == HIGH)
{
Button = Button +1;
}
if(i != 7)
{
Button = Button << 1;
}
else
{
break;
}
clk = digitalRead(ClkPin);
while(clk == LOW)
{
clk = digitalRead(ClkPin);
}
}
} // End of SPI64B void
//

Answers (1)

Walter Roberson
Walter Roberson on 27 Nov 2011
serial() to create the object and set the speed. set() the Terminator at least, and possibly the BytesAvailableFcnMode and BytesAvailableFcn (a callback that is generated when data is ready -- or you can poll for data too.) fopen() the port.
Then, each time you want data from it, fgetl() the data. This applies even if you used the BytesAvailableFcn callback, as that callback is triggered when data is ready but you have to request the transfer of the data from the buffer to your program.
Suggestion: if you were to reformat your message so that the action was first instead of last, such as
Pressed Button 35
Released Button 119
then you would only have to examine the first character to determine the major mode in an obvious manner. You can determine your major mode with the current message format by only examining a single character, but you would have to examine the 4th-last and check 's' (indicating Pre_s_sed) vs 'a' (indicating Rele_a_sed).
  2 Comments
Donald
Donald on 27 Nov 2011
I'm really only needing the number that was pressed.
Here is what I'm using on the Matlab side but I get a warning.
Warning: A timeout occurred before the Terminator was reached.
code:
clear all;
s1 = serial('COM7'); %define serial port
s1.BaudRate=115200; %define baud rate
%open serial port
fopen(s1);
clear data;
for i= 1:5 %acquisition of 100 points
data=fscanf(s1);%read sensor
end
% close the serial port!
fclose(s1);
It worked once but I don't know why and I got the button that was pressed.
Walter Roberson
Walter Roberson on 27 Nov 2011
Did you revise your loop() routine to only send the button numbers and only when they are pressed? If not, then fscanf() is going to get "stuck" when it encounters the text.
The timeout would occur if the terminator being sent by your loop() code does not match that expected on the MATLAB side. It appears that Serial.println() sends linefeed (char(10)); my memory is that that is not the default for the Terminator property of serial connections.
This file contribution might help: http://www.mathworks.com/matlabcentral/fileexchange/26711

Sign in to comment.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!