Stap 4: Code
Ik gebruikte de code van de demonstratie van wetenschappelijke Atlas voor de pH-circuit, omdat het werkt geweldig. Het is belangrijk op te merken dat u moet proefondervindelijk valideren uw pomp. Om dit te doen, haak ene uiteinde van uw pomp naar een container van het water, en het andere uiteinde op een geijkte cilinder. Vertellen de pomp te lopen voor 4000 stappen en record het volume in excel. Doe dit minstens 3 keer, maar de meer paden, hoe nauwkeuriger uw pomp. Zodra u tevreden met uw punten bent, het gemiddelde van alle uw getabelleerde volumes en dat delen door 4000. Dit is het volume van één stap van uw motor.
//By:Kahveh Saramout - Chemical Engineer, Cand// 12/10/2015 //pH code contributed by atlas scientific // MakeCourse - University of South Florida
#include //we have to include the SoftwareSerial library, or else we can't use it. #define rx 4 //define what pin rx is going to be. #define tx 5 //define what pin tx is going to be.
SoftwareSerial myserial(rx, tx); //define how the soft serial port is going to work.
int dirPin2 = 6; int steppin2 = 7; int dirpin = 1; int steppin = 9; int prep = 8; int x = 1; int y=0; float vol_step = 0.205;
String inputstring = ""; //a string to hold incoming data from the PC String sensorstring = ""; //a string to hold the data from the Atlas Scientific product boolean input_stringcomplete = false; //have we received all the data from the PC boolean sensor_stringcomplete = false //have we received all the data from the Atlas Scientific product float ph; //used to hold a floating point number that is the pH.
void setup() { Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600 myserial.begin(9600); //set baud rate for software serial port_3 to 9600 inputstring.reserve(10); //set aside some bytes for receiving data from the PC sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
pinMode(dirpin, OUTPUT); pinMode(steppin, OUTPUT); pinMode(prep, INPUT); pinMode(dirPin2, OUTPUT); pinMode(steppin2, OUTPUT); } void serialEvent() { //if the hardware serial port_0 receives a char char inchar = (char)Serial.read(); //get the char we just received inputstring += inchar; //add it to the inputString if (inchar == '\r') { input_stringcomplete = true; //if the incoming character is a , set the flag } }
void loop() { if (x <= 1){ Serial.println(y); long j;
for (j = 0; j<8360; j++) // Iterate for 8360 microsteps. { digitalWrite(dirPin2, HIGH); digitalWrite(steppin2, LOW); // This LOW to HIGH change is what creates the digitalWrite(steppin2, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(150); // This delay time is close to top speed for this }y++;} if (input_stringcomplete) { //if a string from the PC has been received in its entirety myserial.print(inputstring); //send that string to the Atlas Scientific product inputstring = ""; //clear the string input_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the PC }
if (myserial.available() > 0) { //if we see that the Atlas Scientific product has sent a character. char inchar = (char)myserial.read(); //get the char we just received sensorstring += inchar;
if (inchar == '\r') { sensor_stringcomplete = true; //if the incoming character is a , set the flag } }
if (sensor_stringcomplete) { //if a string from the Atlas Scientific product has been received in its entirety Serial.println("pH: "); //Serial.println(sensorstring); //send that string to the PC's serial monitor ph = sensorstring.toFloat(); Serial.println(ph);//convert the string to a floating point number so it can be evaluated by the Arduino
long i;
for (i = 0; i<1928; i++) // Iterate for 1928 microsteps. { digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(200); // This delay time is close to top speed for this } delay(1000);// particular motor. Any faster the motor stalls. x++; Serial.println("Number of cycles: "); Serial.println(x); //prints 3 of cycles Serial.println("Volume: "); Serial.println(vol_step*x); //calculates our total volume
if (ph <= 6.999) { //if the pH is less than or equal to 6.999 //Serial.println("low"); //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string } Serial.println(" --------------------- "); sensorstring = ""; //clear the string: sensor_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product } if (x >= 60) { // 60 cycles and we are finished! this is how we wrap up. long k; for (k = 0; k<50370; k++) // Iterate for 50370 microsteps. { digitalWrite(dirPin2, LOW); digitalWrite(steppin2, LOW); // This LOW to HIGH change is what creates the digitalWrite(steppin2, HIGH); // "Rising Edge" so the easydriver knows to when to step. delayMicroseconds(150); // This delay time is close to top speed for this } while (x >= 60) { // Do nothing };}