Hey all you lovely knowledgeable folk,
I need a code that logs and saves the data coming from a sensor I have connected to my arduino (a conductivity probe).
Info about the data:
The data comes as a string, and to the best of my limited knowledge cannot be converted to an integer
Reading it from the serial monitor MIGHT be a bit of a problem because it started outputing with a weird character in front of it... (I don't know why) after I added some code to send it to a program that would graph it in real time. this goes away if I mute the line that says " plot(Splot); "
I'm working with a pretty hectic deadline and would be incredibly grateful for help asap!
I would prefer to save it to a computer (no an SD card) as our project has a budget that we have already surpassed).
Thanks!!
-Olive
#include <Servo.h>
#include <SoftwareSerial.h> // add the soft serial library
#define rxpin 2 // set the RX pin to pin 0
#define txpin 3 // set the TX pin to pin
int N = 50; // number of beads to dispense <----- DEFINE N HERE
int X = 5000; // 500mL in 32 seconds (with container filled to 48 oz // time which solenoid valve will be open for <----- DEFINE X HERE
int t = 1; // time
int buffer[20];
Servo servo;
SoftwareSerial myserial(rxpin, txpin); // enable the soft serial port
String Sstring = "";
String inputstring = ""; // a string to hold incoming data from the PC
String sensorstring = ""; // a string to hold the data from the EC probe
boolean input_stringcomplete = false; // has all data from the PC been recieved?
boolean sensor_stringcomplete = false; // has all data from the EC probe been recieved?
boolean S_stringcomplete = false;
void setup(){
pinMode(13, OUTPUT); // set the pin holding the solenoid's resistor to
servo.attach(9); // let the arduino know a servo is connect to pin
Serial.begin(9600); // set the baud rate for the hardware serial port
myserial.begin(9600); // set the baud rate for the software serial port
inputstring.reserve(50); // set aside some bytes for PC data
sensorstring.reserve(50); // set aside some bytes for EC probe data
}
void serialEvent(){ // if the hardware port recives a character
char inchar = (char)Serial.read(); // get the character recieved
inputstring += inchar; // add it to the inputString
if(inchar == '\r'){ // if the incoming character is a <CR>, set the flag
input_stringcomplete = true;
}
}
void loop(){
/*for( int n = 0; n < 1; n++){
delay(5000); // wait 5 seconds
digitalWrite(13, HIGH); // open solenoid valve
delay(X); // keep open for X number of MILIseconds
digitalWrite(13, LOW); // close solenod valve
for( n; n < (N/2); n++){ // N/2 number of 360 degree (+180,-180) servo sweeps
Serial.print(n);
for(int angle = 0; angle <= 180; angle++){ // turn bead mover from 0 to 180 degrees...from collecting the SAP to dropping it
servo.write(angle);
delay(15);
}
for(int angle = 180; angle >= 0; angle--){ // turn bead mover from 180 to 0 degrees...from dropping the SAP to collecting another
servo.write(angle);
delay(15);
}
}
delay(10800000);
}
}
//*/
if(input_stringcomplete){ // if all of a string from the PC has been recieved
myserial.print(inputstring); // send the string to the EC probe
inputstring = ""; // clear the string
input_stringcomplete = false; // reset the flag used to indicate whether a completed string has been recieved from the PC
}
while(myserial.available()){ // while a character is holding in the serial buffer
char inchar = (char)myserial.read(); // get the new character
sensorstring += inchar; // add it to the sensorString
if(inchar == '\r'){ // if the incoming character is a <CR>, set the flag
sensor_stringcomplete = true;
char S1 = sensorstring[0];
int S11 = S1 - '0';
char S2 = sensorstring[1];
int S22 = S2 - '0';
char S3 = sensorstring[2];
int S33 = S3 - '0';
char S4 = sensorstring[3];
int S44 = S4 - '0';
if(S2 == '.'){
if(S33 >= 5){
S11 += 1;
int Splot = S11;
Serial.println(Splot);
plot(Splot);
}
else{
int Splot = S11;
Serial.println(Splot);
plot(Splot);
}
}
else{
if(S44 >= 5){
S22 += 1;
S2 = S22 + '0';
Sstring += S1;
Sstring += S2;
int Splot = Sstring.toInt();
Serial.println(Splot);
plot(Splot);
}
else{
Sstring += S1;
Sstring += S2;
int Splot = Sstring.toInt();
Serial.println(Splot);
plot(Splot);
}
}
}
}
if(sensor_stringcomplete){ // if all of a string from the EC probe has been recieved
Serial.println(sensorstring); // use the hardware serial port to send the data to the PC
sensorstring = ""; // clear the string
sensor_stringcomplete = false; // reset the flag used to indicate whether a completed string has been recieved from the EC probe
Sstring = "";
}
}
void plot(int Splot){
int pktSize;
buffer[0] = 0xCDAB;
buffer[1] = 1*sizeof(int);
buffer[2] = Splot;
pktSize = 2 + 2 + (1*sizeof(int));
Serial.write((uint8_t * )buffer, pktSize);
}
//*/