Change C source code to Arduino

So i got this C source code from Blood Pressure Sensor - Serial output [1437] : Sunrom Electronics and can some adapt this to arduino language? i'm really a newbie in arduino.. I'd really appreciate your help.. thanks :slight_smile:

The Arduino's language is C, well in fact C++ so there is not much to do.
Just replace the main function with loop and have a blank setup function.

Why do you want to convert a ‚Äúdo nothing program‚ÄĚ to Arduino?

If you want to create a program that ‚Äúhas no output‚ÄĚ, you could easily upload the ‚ÄúBareMinumum‚ÄĚ example sketch to your board.

Which Arduino board are you using?
Has it more than one Serial port?

‚ÄúSerial‚ÄĚ is the port which is connected to your USB/PC serial port when connecting an Arduino board using USB.

Does your board have an additional Serial1, Serial2 or Serial3 port for your sensor?
Or do you want to emulate the serial port in software only where you want to connect your sensor?

Arduino board used? Circuit diagram?

Here is a demo program adapted to ‚ÄúArduino MEGA2560‚ÄĚ board, and your sensor connected to ‚ÄúSerial1‚ÄĚ (RX1 = Pin-19), Code is NOT TESTED with a sensor connected:

// Demo program for Arduino MEGA2560 board and sensor with serial output
char sbuffer[30], ch;
unsigned char pos;
unsigned char read1, read2, read3;

void setup(){
  Serial.begin(9600); // Serial is used for output on PCs "Serial Monitor"
  Serial1.begin(9600); // Serial1 is used for serial input from connected sensor
} 

char mygetchar(void)
{ //receive serial character from sensor (blocking while nothing received)
  while (!Serial1.available());
  return Serial1.read();
}

 
void loop()
{
  ch = mygetchar(); //loop till character received
  if(ch==0x0A) // if received character is <LF>, 0x0A, 10 then process buffer
  {
      pos = 0; // buffer position reset for next reading
       
      // extract data from serial buffer to 8 bit integer value
      // convert data from ASCII to decimal
      read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
      read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
      read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
       
      // Do whatever you wish to do with this sensor integer variables
      // Show on LCD or Do some action as per your application
      // Value of variables will be between 0-255
      
      // example: send demo output to serial monitor on "Serial"
      Serial.print(read1);
      Serial.print('\t');
      Serial.print(read2);
      Serial.print('\t');
      Serial.print(read3);
      Serial.print('\t');
      Serial.println();
  } else { //store serial data to buffer
      sbuffer[pos] = ch;
      pos++;
  }
}// end loop
1 Like

Hello Jurs,

It works for Arduino Uno with little change in code.

Connect the SunRom’s Tx output to Rx 0 pin. That’s it.

// Demo program for Arduino MEGA2560 board and sensor with serial output
char sbuffer[30], ch;
unsigned char pos;
unsigned char read1, read2, read3;

void setup(){
 Serial.begin(9600); // Serial is used for output on PCs "Serial Monitor"
 //Serial1.begin(9600); // Serial1 is used for serial input from connected sensor
}

char mygetchar(void)
{ //receive serial character from sensor (blocking while nothing received)
 while (!Serial.available());
 return Serial.read();
}


void loop()
{
 ch = mygetchar(); //loop till character received
 if(ch==0x0A) // if received character is <LF>, 0x0A, 10 then process buffer
 {
     pos = 0; // buffer position reset for next reading
     
     // extract data from serial buffer to 8 bit integer value
     // convert data from ASCII to decimal
     read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
     read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
     read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
     
     // Do whatever you wish to do with this sensor integer variables
     // Show on LCD or Do some action as per your application
     // Value of variables will be between 0-255
     
     // example: send demo output to serial monitor on "Serial"
     Serial.print(read1);
     Serial.print('\t');
     Serial.print(read2);
     Serial.print('\t');
     Serial.print(read3);
     Serial.print('\t');
     Serial.println();
 } else { //store serial data to buffer
     sbuffer[pos] = ch;
     pos++;
 }
}// end loop