Get two values ​​in arduino and display in C#

Question:

I have two Ultrasonics connected to my arduino, and I'm using this code to read their values ​​normally:

#include <Ultrasonic.h>

Ultrasonic ultrasonic(11, 12);
Ultrasonic ultrasonic1(8,9);  

const int TRIG = 8;
const int ECHO = 9;

const int TRIG2 = 11;
const int ECHO2 =12;

void setup() {

  Serial.begin(9600);

  pinMode (TRIG,OUTPUT);
  pinMode (ECHO,INPUT);

  pinMode (TRIG2,OUTPUT);
  pinMode (ECHO2,INPUT);

}


void loop() {

 int data = GetUltra(TRIG,ECHO);
 int data2 = GetUltra(TRIG2,ECHO2);
 char recep = data+"_"+data2;
 Serial.println(recep);
delay(200);

}

double GetUltra(int trig, int echo){

   digitalWrite (trig,LOW);
   delayMicroseconds(2);
   digitalWrite (trig,HIGH);
   delayMicroseconds(8);
   digitalWrite (trig,LOW);

       double distance = (pulseIn(echo, HIGH) )*343.2 / 20000;
       return distance;

}

And now, I'm trying to read the values ​​from my little system in Visual Studio, using Split. However, returns the value of only 1 number

private void button1_Click(object sender, EventArgs e)
{
    message = porta.ReadLine();
    Receptor = message.ToString().Split('_');
    MessageBox.Show("message2" + Receptor[0]);
    MessageBox.Show("message2" + Receptor[1]);

}

Did I do something wrong? Am I not following the right thought?

Answer:

I believe the problem is here on this line:

char recep = data+"_"+data2;

You are using the "char" type when you should actually use the "string" type.

Scroll to Top