Azzera filtri
Azzera filtri

Matlab - Arduino Communication. Matlab sends only 7bit data to arduino. How can i increase that?

2 visualizzazioni (ultimi 30 giorni)
Hi all, I want to send data from matlab to arduino. But, matlab sends only 7 bit data(between 0-127). How can increase that? I want to send 14 bit data(between 0-16384).
here is my arduino code
const int ledpin=13;
int recValue;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available()>0)
{
recValue =Serial.read();
if (recValue == 12007) // If use will send value 100 from MATLAB then LED will turn ON
{
digitalWrite(ledpin, HIGH);
}
if(recValue == 10007) // If use will send value 101 from MATLAB then LED will turn OFF
{
digitalWrite(ledpin, LOW);
}
}
}

Risposta accettata

Walter Roberson
Walter Roberson il 29 Gen 2016
On the MATLAB side, you will need to send multiple bytes, such as using fwrite(s,TheValue,'uint16') . On the Arduino side you will need to do two Serial.read() and construct recValue from that, such as by using
firstbyte<<8 | secondbyte
Be careful about the byte order: MATLAB is probably going to send the least significant byte first.
  3 Commenti
Walter Roberson
Walter Roberson il 29 Gen 2016
The arduino gets 48-57 only if you used fprintf() with a numeric format such as %d or %f. If you use fwrite() then the byte values themselves would be sent.
If you do have the 48-57 range then subtract 48 to get the corresponding digit 0 to 9. You would probably need to put several of these digits together to build the decimal number. Binary (fwrite) is easier.
Cati Kati
Cati Kati il 30 Gen 2016
Modificato: Cati Kati il 30 Gen 2016
OMG, it works! thank you ever so much, I was dealing with it since 3 days. Thank you so muchh =)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by