7 years, 6 months ago.

How to convert this arduino code to kl25z mbed?

Good day programmers! I would like to ask for help on how to convert this arduino code to mbed. This code is for a Dissolved Oxygen sensor. Reference: https://www.atlas-scientific.com/_files/code/Arduino-Uno-DO-sample-code.pdf

  1. include <SoftwareSerial.h> we have to include the SoftwareSerial library, or else we can't use it
  2. define rx 2 define what pin rx is going to be
  3. define tx 3 define what pin tx is going to be

SoftwareSerial myserial(rx, tx); define how the soft serial port is going to work

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_string_complete = false; have we received all the data from the PC boolean sensor_string_complete = false; have we received all the data from the Atlas Scientific product float DO; used to hold a floating point number that is the DO

void setup() { set up the hardware Serial.begin(9600); set baud rate for the hardware serial port_0 to 9600 myserial.begin(9600); set baud rate for the software serial port 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 }

void serialEvent() { if the hardware serial port_0 receives a char inputstring = Serial.readStringUntil(13); read the string until we see a <CR> input_string_complete = true; set the flag used to tell if we have received a completed string from the PC }

void loop() { here we go...

if (input_string_complete){ if a string from the PC has been received in its entirety myserial.print(inputstring); send that string to the Atlas Scientific product myserial.print('\r'); add a <CR> to the end of the string inputstring = ""; clear the string input_string_complete = 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; add the char to the var called sensorstring if (inchar == '\r') { if the incoming character is a <CR> sensor_string_complete = true; set the flag } }

if (sensor_string_complete== true) { if a string from the Atlas Scientific product has been received in its entirety Serial.println(sensorstring); send that string to the PC's serial monitor if (isdigit(sensorstring[0])) { if the first character in the string is a digit DO = sensorstring.toFloat(); convert the string to a floating point number so it can be evaluated by the Arduino if (DO >= 6.0) { if the DO is greater than or equal to 6.0 Serial.println("high"); print "high" this is demonstrating that the Arduino is evaluating the DO as a number and not as a string } if (DO <= 5.99) { if the DO is less than or equal to 5.99 Serial.println("low"); print "low" this is demonstrating that the Arduino is evaluating the DO as a number and not as a string } } sensorstring = ""; clear the string sensor_string_complete = false; reset the flag used to tell if we have received a completed string from the Atlas Scientific product } }

Please edit the post to use code tags e.g.

<<code>>
#include <SoftwareSerial.h>
...
<</code>>

So that the formatting is preserved and we can tell which bits are comments.

posted by Andy A 04 May 2017

1 Answer

7 years, 6 months ago.

HI,

the code could look like these:

#include "mbed.h"
#include <ctype.h>
 
Serial serial(USBTX, USBRX);		// Define the serial port to the PC
Serial myserial(PTE22, PTE23);		// Define how the serial port to the Disolved Oxygen Sensor

char	inputString[10] 		= "";		// A char array to hold incoming data from the PC. set asaide some bytes.
char 	sensorString[30]		= "";		// A char array to hold the data from the Atlas Scientific product. set asaide some bytes.
int		input_string_complete	= 0;		// have we received all the data from the PC
int		sensor_string_complete	= 0;		// have we received all the data from the Atlas Scientific product
float	DO						= 0.0;		// used to hold a floating point number that is the DO
 
int main()
{
    serial.baud(9600);
	myserial.baud(9600);
	
 
    while (true) {		// here we go
		
		if (serial.readable()) {						// if the hardware serial port receiver a char
			char inchar = serial.getc()					// read the char
			strcat(inputString, inchar);				// concatenate the char array
			if (inchar == 13) {							// if the char is equal to 13 <CR>
				input_string_complete = 1.0;			// set the flag used to tell if we hace received a completed string from the PC
			}
		}
		
		if (input_string_complete){						// if a string from the PC has been received in its entirety			
			myserial.printf("%s\r\n", inputString);		// send that string to the Atlas Scientific Product add CR to the end of the string
			inputString[0] = "\0";						// clear the string
			input_string_complete = 0;					// reset the flag used to tell if we have received a completed string from the PC
		}
		
		if (myserial.readable()) {						// if we see that the Atlas Scientific Product has sent a character
			char inchar = myserial.getc()				// get the char we just received
			strcat(sensorString, inchar);				// add the char to the var called sensorString
			if (inchar == '\r') {						// if the incoming character is a <CR>
				sensor_string_complete = 1;				// set the flag
			}
		}
		
		if (sensor_string_complete) {					// if a string from the Atlas Scientific product has been received in its entirety
				serial.printf("%s",sensorString);		// send that string to the PC's Serial Monitor
				
				if (isdigit(sensorString[0])) {			// if the first character in the string is a digit
					DO = atof(sensorString);			// convert the string to a floating point number so it can be evaluated by the mbed
					
					if (DO >= 6.0) {					// if the DO is greater than or equal to 6.0
						serial.printf("high");			// print "high" this is demonstrating that the mbed is evaluating the DO as a number
					} else {							// and not as a string. Else (if the DO is less than 6.00) print "low" this is demonstrating 
						serial.printf("low");			// than the mbed is evaluating the DO as a number and not as a string
					}
				}
				
				sensorString[0] = "\0";					// clear the string
				sensor_string_complete = 0;				// reset the flag used to tell if we have received a completed string from the
		}												// Atlas Scientific product
		
        wait_ms(100); 		// wait a small period of time
        myled = !myled; 	// toggle a led
    }
}

IMPORTANT NOTES: I don't know if the "ctype.h" is available on mbed. Not sure if it will work I don't have a KL25Z to test. I only have nucleo boards availables.

I would prefer to change several things on that code but you got an idea.

Best regards.

Accepted Answer

thank you it was a big help :)

posted by Vann Leo Sadsad 05 May 2017