Hi everyone, I'm pretty new to programming but understand a lot of the concepts because of reading this forum and watching videos on YouTube! One thing I cannot figure out is if there is a command that allows for autonomous repeated input into a serial monitor, for instance I'm programming Atlas-Scientific D.O. probe and want to be able to use my DO value in an if else loop to turn on or off a air pump, so I'm using theI2C protocol where a user has to enter r in the serial monitor to initiate the probe to take a reading. The only problem is I would like the probe to read for long amounts of time and run without supervision so I cannot figure out how to get the probe to read continuously and allow me to use the value of DO in the water. Any help or suggestions would be great!
I don't have sure, but I think that you are using a sketch provided by Atlas-Scientific, right? If so, you only need to change the sketch in order to make that continuous reads. If not, you need to explain what is happen because I don't understand.
Typically you would remove the part of the code that waits for serial input before taking a reading. If you show the sketch it would be easier to say what part to remove.
You may find some useful ideas in Several Things at a Time
...R
gboyle:
Hi everyone, I'm pretty new to programming but understand a lot of the concepts because of reading this forum … and want to be able to use my DO value in an if else loop to turn on or off a air pump, so I'm using theI2C protocol where a user has to enter r in the serial monitor to initiate the probe to take a reading. The only problem is I would like the probe to read for long amounts of time and run without supervision
loop() {
if(there is input ready on Serial) {
handle it;
}
if(the probe is supposed to be doing some reading) {
do some reading;
}
}
So I been hitting my head against the wall trying to figure out this loop and I can't really figure out, here is my code, what if I delete the break at the end of the serial print loop?
#include <Wire.h> //enable I2C.
#define address 97 //default I2C ID number for EZO D.O. Circuit.
char computerdata[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other.
byte received_from_computer=0; //we need to know how many characters have been received.
byte serial_event=0; //a flag to signal when data has been recived from the pc/mac/other.
byte code=0; //used to hold the I2C response code.
char DO_data[20]; //we make a 20 byte character array to hold incoming data from the D.O. circuit.
byte in_char=0; //used as a 1 byte buffer to store in bound bytes from the D.O. Circuit.
byte i=0; //counter used for DO_data array.
int time=1400; //used to change the delay needed depending on the command sent to the EZO Class D.O. Circuit.
float DO_float; //float var used to hold the float value of the DO.
char *DO; //char pointer used in string parsing.
char *sat; //char pointer used in string parsing.
float do_float; //float var used to hold the float value of the dissolved oxygen.
float sat_float; //float var used to hold the float value of the saturation percentage.
int nitro=6; //this is my nitro tank relay
int air=7; //this is my air stone realy
void setup() //hardware initialization.
{
Serial.begin(9600); //enable serial port.
Wire.begin(); //enable I2C port.
pinMode (nitro, OUTPUT);
pinMode (air, OUTPUT);
}
void serialEvent(){ //this interrupt will trigger when the data coming from the serial monitor(pc/mac/other) is received.
received_from_computer=Serial.readBytesUntil(13,computerdata,20); //we read the data sent from the serial monitor(pc/mac/other) until we see a . We also count how many characters have been received.
computerdata[received_from_computer]=0; //stop the buffer from transmitting leftovers or garbage.
serial_event=1;
}
void loop(){ //the main loop.
if(serial_event){ //if the serial_event=1.
if(computerdata[0]=='c'||computerdata[0]=='r')time=1400; //if a command has been sent to calibrate or take a reading we wait 1400ms so that the circuit has time to take the reading.
else time=300; //if any other command has been sent we wait only 300ms.
Wire.beginTransmission(address); //call the circuit by its ID number.
Wire.write(computerdata); //transmit the command that was sent through the serial port.
Wire.endTransmission(); //end the I2C data transmission.
delay(time); //wait the correct amount of time for the circuit to complete its instruction.
Wire.requestFrom(address,20,1); //call the circuit and request 20 bytes (this may be more then we need).
code=Wire.read(); //the first byte is the response code, we read this separately.
switch (code){ //switch case based on what the response code is.
case 1: //decimal 1.
Serial.println("Success"); //means the command was successful.
break; //exits the switch case.
case 2: //decimal 2.
Serial.println("Failed"); //means the command has failed.
break; //exits the switch case.
case 254: //decimal 254.
Serial.println("Pending"); //means the command has not yet been finished calculating.
break; //exits the switch case.
case 255: //decimal 255.
Serial.println("No Data"); //means there is no further data to send.
break; //exits the switch case.
}
while(Wire.available()){ //are there bytes to receive.
in_char = Wire.read(); //receive a byte.
DO_data*= in_char; //load this byte into our array.*
-
i+=1; //incur the counter for the array element.*
-
if(in_char==0){ //if we see that we have been sent a null command.*
-
i=0; //reset the counter i to 0.*
-
Wire.endTransmission(); //end the I2C data transmission.*
-
break; //exit the while loop. WHAT IF I DELETE THIS BREAK, WOULD THE PROBE READ CONTINUOUS??*
-
}*
-
}*
-
void loop();*
-
Serial.println(DO_data); //print the data.*
-
serial_event=0; //reset the serial event flag.*
-
if(computerdata[0]=='r') string_pars(); //Uncomment this function if you would like to break up the comma separated string*
-
}*
} -
void string_pars(){ //this function will break up the CSV string into its 2 individual parts, DO and %sat .*
-
//this is done using the C command “strtok”. *
-
sat=strtok(DO_data, ","); //let's pars the string at each comma.*
-
DO=strtok(NULL, ","); //let's pars the string at each comma.*
-
Serial.print("DO:"); //We now print each value we parsed separately.*
-
Serial.println(DO); //this is the D.O. value.*
-
Serial.print("Sat:"); //We now print each value we parsed separately.*
-
Serial.println(sat); //this is the % saturation value. *
-
DO_float=atof(DO);*
-
sat_float=atof(sat);*
if (sat_float<5) { //Here I bring down the O2 saturation by injecting nitro -
digitalWrite(nitro,LOW);*
} else { -
digitalWrite(nitro,HIGH);*
-
delay(500);*
-
digitalWrite(nitro, LOW);*
-
//need to figure out how to loop this over 2 hours to bring saturation to desired level at 5% increments*
-
//and for amount of time then start bring back up with air*
if (sat_float <5){
digitalWrite(air,HIGH);
delay(500);
digitalWrite(air, LOW); // this brings the O2 sat back up and need to do that over an hour in 5% increments
} else { -
digitalWrite(air, LOW); //trying to figure out coding to loop this and bring down O2% by increments and how to input R in serial monitor without having to press R,*
-
//then bring back up with air without having to rewrite code in the middle of experiment.*
}}}
Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button.
Do us a favor, describe the desired behavior of your program. If it is just reading a sensor and waiting for serial port input, why can't it run repeatedly?
Please use code tags.
Read this before posting a programming question
Please edit your post, select the code, and put it between [code] ... [/code] tags.
You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>
Around line #68 you have:
void loop();
which is probably not good on two levels. First, when you call a function, you do not put the function type specifier (e.g., void) before the function call name. Second, because you placed this within loop(), it appears that you are trying to call loop() recursively, which I don't think you want to do. I think you want to remove that call.