Network Your Group: Coding Challenge 1

Multiple sensors for multiple bioreactors.

Taking a look at the Serial Print section of the Bench Test 1 program:


// -------------------------------Print to Serial Monitor
Serial.print("Pin Reading = ");
Serial.print(Pval);
Serial.print("\t TEMPRATURE = ");
Serial.print(Tc);
Serial.print("*C \t");
Serial.print(Tf);
Serial.print("*F");
Serial.println();

This code results in the following computer monitor on-screen print.

Pin Reading = 42 TEMPRATURE = 20.51*C 68.91*F
Pin Reading = 43 TEMPRATURE = 21.00*C 69.79*F
Pin Reading = 42 TEMPRATURE = 20.51*C 68.91*F

Follow the code to see how this printout works. Note that ‘\t’ is a special code that sends a tab code to add spaces to align data in columns.

This code prints the input value at pin 1. To add additional analog inputs, assign variables to additional pins, pins 0 and 2.


int Pval0;      //Pin A0
int Pval1;		//Pin A1
int Pval2; 		//Pin A2

Then read these pins.


Pval0 = analogRead(0);            
Pval1 = analogRead(1);          
Pval2 = analogRead(2);  

Follow this by a conducting calculations and printing to the monitor.



/*          Temperature Sensor Network (LM35)
*                    Benchtest 2
*/
//--------------------------------- Declare variables
int Pval0;
int Pval1;
int Pval2;
float Tc0;
float Tc1;
float Tc2;
float Tf0;
float Tf1;
float Tf2;
void setup()
{
Serial.begin(9600);            // Start serial communication
}
void loop()
{
Pval0 = analogRead(0);           // Read Analog Pin A0 
Pval1 = analogRead(1);          // Read Analog Pin A1 
Pval2 = analogRead(2);          // Read Analog Pin A2 
Tc0 = Pval0 * 0.4882815;          // Convert reading to celcius scale
Tc1 = Pval1 * 0.4882815;
Tc2 = Pval2 * 0.4882815;
Tf0= (Tc0*9)/5 + 32;             // Convert celcius to fahrenheit scale
Tf1 = (Tc1*9)/5 + 32;
Tf2 = (Tc2*9)/5 + 32;
// -------------------------------Print to Serial Monitor
Serial.print("Pin Reading = ");
Serial.print(Pval0);
Serial.print("\t TEMPRATURE = ");
Serial.print(Tc0);
Serial.print("*C \t");
Serial.print(Tf0);
Serial.print("*F \t");
// --------------
Serial.print("Pin Reading 2 = ");
Serial.print(Pval1);
Serial.print("\t TEMPRATURE = ");
Serial.print(Tc1);
Serial.print("*C \t");
Serial.print(Tf1);
Serial.print("*F \t");
//-------------
Serial.print("Pin Reading = ");
Serial.print(Pval2);
Serial.print("\t TEMPRATURE = ");
Serial.print(Tc2);
Serial.print("*C \t");
Serial.print(Tf2);
Serial.print("*F");
Serial.println();
// -----------------------------------------------------
delay(1000);
}

Arrays can be used to reduce code length. Arrays also allow you to expand or scale a network from one to many with minor code changes. Arrays work by creating a loop.

For i = 1 to 3
Next

The variable ' i ' counts from 1 to 3 as the program loops. Variables can be assigned values in the loop.


for (byte i = 0; i < 3; i = i + 1) {
Pval[i] = analogRead(i);
}

This code reads pin A0, A1 and A2 and assigns the value to variables Pval[0], Pval[1] amd Pval[2].


/*          Temperature Sensor Network [array] (LM35)
*                    Bench test 3
*/
//--------------------------------- Declare variables
int Pval[3];
int Tc[3];
int Tf[3];
void setup()
{

Serial.begin(9600);            // Start serial communication
}
void loop()
{
for (byte i = 0; i < 3; i = i + 1) {
Pval[i] = analogRead(i); 
Tc[i] = Pval[i] * 0.4882815;
Tf[i] = (Tc[i]*9)/5 + 32; 


// -------------------------------Print to Serial Monitor
Serial.print("Pin Reading = ");
Serial.print(Pval[i]);
Serial.print("\t TEMPRATURE = ");
Serial.print(Tc[i]);
Serial.print("*C \t");
Serial.print(Tf[i]);
Serial.print("*F \t");
}
Serial.println();

// -----------------------------------------------------
delay(1000);
}