64 button shield & arduino with matlab
1 view (last 30 days)
Show older comments
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
//
0 Comments
Answers (1)
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
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
See Also
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!