Global Variable (Value Read Serially from Matlab) Becomes 0

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

Opening the serial port can reset the Arduino.

Thanks for your reply. Is there a workaround for this so that the data for a variable will remain even if I manipulate the serial object from the Matlab. Thanks.

Is there a workaround for this so that the data for a variable will remain even if I manipulate the serial object from the Matlab.

http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection

http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection
I'm too slow.

Nice....This is what I am looking for...Thanks

Hi. placing a 120 ohm resistor in between the Reset pin and the +5v pin does not work. It throws an error "avrdude: stk500_getsync(): not in sync: resp=0x00". I am using Gizduino Atmega 328. Help...Thanks in advance

johnvictorlim:
Hi. placing a 120 ohm resistor in between the Reset pin and the +5v pin does not work. It throws an error "avrdude: stk500_getsync(): not in sync: resp=0x00". I am using Gizduino Atmega 328. Help...Thanks in advance

When you defeat the auto-reset feature, YOU are required to reset the Arduino at the proper time!

When you defeat the auto-reset feature, YOU are required to reset the Arduino at the proper time!

Hi. I don't know what you mean by "resetting the Arduino at the proper time", do I need to push the reset button gizduino after connecting a 120 ohm resistor between the reset and the +5v pin. Thanks

do I need to push the reset button gizduino after connecting a 120 ohm resistor between the reset and the +5v pin.

Yes, after the IDE prints the message that compilation was successful, but before at actually starts the upload process.

Yes, after the IDE prints the message that compilation was successful, but before at actually starts the upload process.

Nice it actually works, Thanks so much....