MATLAB Answers

0

How do I display different sensor readings from aduino on different static texts on MATLAB GUI?

Asked by Patrick Avena on 20 Aug 2015
Latest activity Commented on by Walter Roberson
on 5 Apr 2016
As the title says, how do I display different sensor readings from aduino on different static texts on MATLAB GUI? The idea is when I press a pushbutton on matlab gui, each readings will be displayed on their respective static texts. Is there another syntax than 'fscanf' because it seems fscanf gets all the data from arduino. I'll show the code on my arduino and matlab. Below is the arduino code:
const int trig1 = 6;
const int echo1 = 7;
const int trig2 = 8;
const int echo2 = 9;
const int trig3 = 10;
const int echo3 = 11;
long time, dist, tank1, tank2, tank3;
void setup()
{
pinMode(trig1,OUTPUT);
pinMode(echo1,INPUT);
pinMode(trig2,OUTPUT);
pinMode(echo2,INPUT);
pinMode(trig3,OUTPUT);
pinMode(echo3,INPUT);
Serial.begin(9600);
}
void loop()
{
SonarSensor(trig1,echo1);
tank1 = dist;
SonarSensor(trig2,echo2);
tank2 = dist;
SonarSensor(trig3,echo3);
tank3 = dist;
Serial.print(tank1);
Serial.print(" cm");
Serial.print(tank2);
Serial.print(" cm");
Serial.print(tank3);
Serial.println(" cm");
delay(50);
if(Serial.available()!=0)
{
char command = Serial.read();
if(command=='w')
digitalWrite(13,HIGH);
if(command=='x')
digitalWrite(13,LOW);
}
}
void SonarSensor(int trig,int echo)
{
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
time = pulseIn(echo, HIGH);
dist = (time/2) / 29.1;
}
I want to display the readings of tank1, tank2 and tank3. This is what my MATLAB code looks like (at the press of a pushbutton):
a = serial('COM5');
fopen(a);
for i=1:20
p = fscanf(a);
set(handles.text1,'String',p);
end
I would like to display the readings of sensor1 in static text1, sensor2 in static text2, and sensor3 in static text 3. Please help me guys or at least give me an idea how to do it. Hoping for your kind considerations. :\

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 15 Sep 2015
 Accepted Answer

for i=1:20
p123 = fscanf(a,'%d cm%d cm%d cm');
set(handles.text1,'String',p123(1));
set(handles.text2,'String',p123(2));
set(handles.text3,'String',p123(3));
end

  5 Comments

Hi, I have a similar problem here, but I didn't understand how I would stop the while loop. My goal is to save the values to a variable (instead of showing them, so I wouldn't use the "pause". But I can't stop my while loop, would you show me how is the functions that saves the stop_tooglebutton? thanks!!
Filipe Nonato Rocha Coelho : use drawnow() instead of pause()
Your stop_togglebutton does not need a callback. It will be assigned the value 1.0 when it is "on" and it will be assigned the value 0.0 when it is "off". It will continue to hold that value until it is changed, either by the user clicking on the button again, or by the code using
st(handles.stop_togglebutton, 'Value', 0) %or 1

Sign in to comment.