Hi. Before discussing my problem, here is the Arduino program code for "temperature monitoring" using DHT 11 Sensor and a Real Time Plot from Matlab. The "switch" statement is used for doing 2 distinct operations: (1) storing the value of the temperature to be maintained in the Arduino Global variable (temperature); and (2) for the real-time plot of the temperature read by a DHT11 sensor.
#include <dht.h>
dht dht11_Sensor;
const int pin_HeatUp = 2;
const int LED_HeatUp = 3;
const int pin_loopBreak = 4;
const int pin_fan = 13;
const int pin_dhtRead = 14;
const int num_Bytes = 2;
double temperature;
double dhtRead;
int conv_to_number(int, int);
void idle_blink();
void pin_reset();
void setup()
{
Serial.begin(9600);
while(!Serial){;}
pinMode(pin_HeatUp,OUTPUT);
pinMode(LED_HeatUp,OUTPUT);
pinMode(pin_loopBreak,INPUT);
pinMode(pin_dhtRead,INPUT);
pinMode(pin_fan,OUTPUT);
}
void loop()
{
int *val_readBytes = new int(num_Bytes);
int valSerial;
if(Serial.available() >= 2)
{
for(int i = 0; i < num_Bytes; i++)
{ *(val_readBytes+i) = Serial.read(); }
valSerial = conv_to_number(*(val_readBytes),*(val_readBytes+1));
Serial.println(valSerial);
switch(valSerial)
{
case 1:
while(Serial.available() < 2)
{ idle_blink(); }
delay(500);
digitalWrite(LED_HeatUp,LOW);
val_readBytes = new int(num_Bytes);
for(int i = 0; i < num_Bytes; i++)
{ *(val_readBytes+i) = Serial.read(); }
temperature = conv_to_number(*(val_readBytes),*(val_readBytes+1));
Serial.println(temperature);
break;
case 2:
while(true)
{
dht11_Sensor.read11(pin_dhtRead);
dhtRead = dht11_Sensor.temperature;
Serial.println(dhtRead);
Serial.println(temperature);
if(dhtRead < temperature)
{ digitalWrite(pin_HeatUp,HIGH);
digitalWrite(LED_HeatUp,HIGH); }
if(digitalRead(pin_loopBreak) == HIGH)
{
Serial.println(-1);
pin_reset();
break;
}
delay(1000);
}
break;
}
}
}
int conv_to_number(int firstByte, int secondByte)
{
return firstByte;
}
void idle_blink()
{
digitalWrite(LED_HeatUp, LOW);
delay(250);
digitalWrite(LED_HeatUp,HIGH);
delay(250);
}
void pin_reset()
{
digitalWrite(pin_HeatUp,LOW);
digitalWrite(LED_HeatUp,LOW);
digitalWrite(pin_fan,LOW);
}
The problem I encounter is that the value of the global variable "temperature" becomes 0 when I close the serial in Matlab. I tried troubleshooting from the MATLAB by executing these commands in MATLAB
>> A = serial('COM12')
>> fopen(A);
>> fprintf(A,1); % for the Switch Statement
>> fprintf(A,40); % the amount of temperature for the global variable
>> fclose(A)
After the command "fprintf(A,4)", I check the BytesAvailable() of Serial and check if the values "1" and "40" are received by the Arduino and sent back to Matlab for confirmation. The values I read serially by this time are 1 and 40. I closed the Matlab serial object and tried opening it again. This time I send serially the value of 2 to enter the case 2 of the switch statement. I tried printing the value of temperature, however the value of the temperature becomes 0. I don't know but the value of the temperature should still be 40 since it is declared as a global variable and its value is stored on the previous commands.
Help is very much appreciated. Thanks in advance