Professional Documents
Culture Documents
//It will allow you to control up to 4 Atlas Scientific devices through 1 soft s
erial RX/TX line.
//To open a channel (marked on the board as Y0 to Y3) send the number of the ch
annel, a colon and the command ending with a carriage return.
//0:r<CR>
//1:i<CR>
//2:c<CR>
//3:r<CR>
//To open a channel and not send a command just send the channel number followed
by a colon.
//1:<CR>
//3:<CR>
//This code uses the Altsoft softserial library. The library file can be downloa
ded here: http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
//This softserial library Automatically sets TX as pin 9 and RX as pin 8.
void loop()
{
unsigned long currentMillis = millis();
temp = read_temp();
//
// if(computer_bytes_received!=0)
// { //If computer_bytes_received does not equal zero
// channel=strtok(computerdata, ":"); //Let's parse the stri
ng at each colon
// cmd=strtok(NULL, ":"); //Let's parse the stri
ng at each colon
// open_channel(); //Call the function "o
pen_channel" to open the correct data path
// altSerial.print(cmd); //Send the command fro
m the computer to the Atlas Scientific device using the softserial port
// altSerial.print("\r"); //After we send the co
mmand we send a carriage return <CR>
// computer_bytes_received=0; //Reset the var comput
er_bytes_received to equal 0
//
// }
open_channel();
if (altSerial.available() > 0)
{ //If data has been transmitted from an Atlas Scientific device
sensor_bytes_received = altSerial.readBytesUntil(13, sensordata, 30); //we r
ead the data sent from the Atlas Scientific device until we see a <CR>. We also
count how many character have been received
sensordata[sensor_bytes_received] = 0; //we add a 0 to the spot in t
he array just after the last character we received. This will stop us from trans
mitting incorrect data that may have been left in the buffer
//Serial.println(sensordata);
//ORP= atof(sensordata);
//ORP= sensordata;
if (ubah == 0 )
{
// dataString+= "#";
// dataString= dataString + String(sensordata);
// dataString+=",";
ORP = "";
ORP = String(sensordata);
}
else if (ubah == 1)
{
// dataString= dataString + String(sensordata);
// dataString+=",";
PH = "";
PH = String(sensordata);
}
else if (ubah == 2)
{
// dataString= dataString + String(sensordata);
// dataString+=",";
EC = "";
EC = String(sensordata);
}
else if (ubah == 3)
{
// dataString= dataString + String(sensordata);
// dataString+=",";
// dataString= dataString + String(temp);
// dataString+="*";
suhu = "";
suhu = String(temp);
DO = "";
DO = String(sensordata);
}
ubah++;
if (ubah > 3)
ubah = 0;
//delay(1000);
//let s transmit the data received from the Atlas Scientific device to the ser
ial monitor
}
//sprintf(kirimsensor,"#%s,%s,%s,%s,%s*",ORP,PH,EC,DO,suhu);
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
dataString = "#" + ORP + "," + PH + "," + EC + "," + DO + "," + suhu + "*";
Serial.println(dataString);
}
}
void open_channel()
{ //This function controls what UART port is opened.
digitalWrite(en, LOW);
switch (ubah)
{ //Looking to see what channel to open
case 0: //If channel==0 then we open ch
annel 0
digitalWrite(s0, LOW); //S0 and S1 control what chan
nel opens
digitalWrite(s1, LOW); //S0 and S1 control what chan
nel opens
break; //Exit switch case
case 2:
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
break;
case 1:
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
break;
case 3:
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
break;
}
}
float read_temp(void)
{
float v_out;
float temp;
v_out = analogRead(0);
v_out *= 0.0048;
v_out *= 1000;
temp = (0.0512 * v_out) - 20.5128;
return temp;
}