0% found this document useful (0 votes)
14 views2 pages

Raw Code

The document contains Arduino code that simulates a temperature control system with commands for querying device information, current temperature, and power settings. It generates random noise for temperature measurements and adjusts the simulated temperature based on applied power. The system responds to specific commands received via serial communication, allowing for power adjustments and status inquiries.

Uploaded by

kenjo138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Raw Code

The document contains Arduino code that simulates a temperature control system with commands for querying device information, current temperature, and power settings. It generates random noise for temperature measurements and adjusts the simulated temperature based on applied power. The system responds to specific commands received via serial communication, allowing for power adjustments and status inquiries.

Uploaded by

kenjo138
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

String Command;

String SubCommand;
String Data;

float Temp = 30; //Current Temperature


float Noise = 0; //Random noise in the simulated temperature measurement
float PWR = 0; //Current Set power of the external device. Always start at zero
when the arduino is first powered on
float PWRTemporary = 0; //Temp power setting do ensure requested value is in
range.
float TempError = 0; //Depending on how far from the "correct" simulated
temperature depending on applied power, this value will be added to the temperature
during each loop iteration.
float TempC = 30; //Calculated "correct" simulated temperature

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.setTimeout(50);

void loop() {
// put your main code here, to run repeatedly:

Noise = (random(-100, 100)) / 1000.00; //Generate random temperature noise


during each loop iteration

TempC = 0.66*PWR + 30; //Calculate what the temperature should be based on the
applied power.

TempError = TempC - Temp;


Temp = Temp + TempError/100 + Noise;

//Check for Messages coming from LabVIEW


Command = Serial.readString();
SubCommand = Command.substring(0, 5);
Data = Command.substring(5);

if (Command.length() == 0) { //If message is blank, do nothing


}

else { //If message is not blank, parse the message and respond
appropriately.

if (SubCommand == "*IDN?") //If receive command "*IDN?" respond with the


device identification information
{
Serial.print("Power Supply P100: S.Miller Industries: Serial# 012345\r");
}

else {
if (SubCommand == "Temp?") //If receive command "Temp?" respond with the
current temperature
{
Serial.print(Temp);
Serial.print("\r");
}
else {
if (SubCommand == "Powr?") //If receive command "Powr?" respond with the
current power setting
{
Serial.print(PWR);
Serial.print("\r");
}

else {
PWRTemporary = Data.toFloat();
if ((SubCommand == "Powr:") && (PWRTemporary <100.0001) && (PWRTemporary
>-0.0001)) //If receive command "Powr:", we need to change the current power
setting of the device but only if the request is in range (i.e. 0..100).
{

PWR = PWRTemporary;
Serial.print("Power Set");
Serial.print("\r");
}
else {
Serial.print("Invalid Command"); //If an unrecognized command is
received, ignore it and respond with an "Invalid Command" message.
Serial.print("\r");
}
}
}
}
}

delay(100);
}

You might also like