Take two values from Arduino and display in C#

Asked

Viewed 138 times

4

I have two Ultrasonics linked in my Alpha, 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 in my 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? I’m not following the right thought?

  • 2

    What is the value of message received at C#?

  • Well returned me a very odd value with characters and number(ex: @#sadv4355345@@#$%@),I’m suspicious of a short that is coming in Arduino,I’ll take some equipment and redo the test

  • Your code is correct. You have checked whether the serial monitor given is shown correctly?

  • @Iurypereira is getting the shape characters strange, This indicates that the speed of the serial port configured in Arduino is different from that configured in C#. Another important detail, the message may not be complete, so I recommend having a message finalizer. If I need to pass you my communication code.

1 answer

0

I believe the problem is here on this line:

char recep = data+"_"+data2;

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.