VB.NET If statement based on Arduino Serial Output

I am building a VB.NET Win Forms app that is reading the serial output of an Arduino and I want the VB.Net app to do something based on what it reads from the Arduino. The problem I am encountering is that even though I can read from the Arduino I can't seem to write it into an if statement.

The Arduino is a simple 3 button schematic. A red, yellow, and green button attached to digital pins 2,3, and 4. When one button is pressed the Arduino outputs the text "Red Button On", "Yellow Button On", or "Green Button On" depending on which button is pressed.

In the VB.Net app I have a label named lblHintRequest. I can load the arduino serial output onto this label. However, once I load the arduino serial output onto this label I can't seem to do anything with it. What I need is an if statement that will read which button is pressed and then do something depending on the button pressed. I have also tried adding a timer that ticks ever 500 ms that would read the label and this still did not work. The vb.net app just keeps skipping over the if statement.

The last thing I can think of trying is to use a textbox instead of a label and maybe that would help? But I really want this to be a label.

Thanks,

--------------------VB.NET Code---------------------

Imports System.IO.Ports
Public Class frmRoom

Private Sub frmRoom_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 SerialPort1.Open()
End Sub

 Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    'Searching for data input
    Dim ReadCode As String
    ReadCode = SerialPort1.ReadLine

    'Print the button press
    lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing)


'This code will not run even though when I invoke the lblhintrequest it shows re button on
    If lblHintRequest.Text = "Red Button On" Then 
        lblHintRequest.ForeColor = Color.Red
    End If

'This if statement does not work either
    If lblHintRequest.Invoke(New MethodInvoker(Sub() lblHintRequest.Text = ReadCode), Nothing) = "Red Button On" Then
        MsgBox("hit")
    End If


End Sub
'----------Code below added for solution response on stack overflow ---
  
Private Sub UpdateHintRequest(code As String)

    If lblHintRequest.InvokeRequired Then
        lblHintRequest.Invoke(Sub() UpdateHintRequest(code))

--Getting stuff to run here was the solution.

 If lblHintRequest.Text = "Red Button On" Then
            lblHintRequest.ForeColor = Color.Red
        End If
    Else

       
    End If

    End Class



------------------Arduino Code----------------

    const int RedButton = 2;
    const int YellowButton = 3;
    const int GreenButton = 4;
    const int ledpin = 13;
    
    int buttonstateRed =0;
    int buttonstateYellow =0;
    int buttonstateGreen =0;
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
    pinMode(ledpin,OUTPUT);
    pinMode(RedButton, INPUT);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    buttonstateRed = digitalRead(RedButton);
    if (buttonstateRed == HIGH){
      digitalWrite(ledpin, HIGH);
      Serial.println("Red Button On");
    }
    
    buttonstateYellow = digitalRead(YellowButton);
    if (buttonstateYellow == HIGH){
      digitalWrite(ledpin, HIGH);
      Serial.println("Yellow Button On");
    }
    
    
    buttonstateGreen = digitalRead(GreenButton);
    if (buttonstateGreen == HIGH){
      digitalWrite(ledpin, HIGH);
      Serial.println("Green Button On");
    }
    
    if (buttonstateRed == LOW && buttonstateYellow == LOW && buttonstateGreen == LOW){
      digitalWrite(ledpin, LOW);
      //Serial.println("No Button Pressed");
    }
    
    Serial.flush();
    delay(500);
    
    //Serial.println("Hello");
    //delay(250);
    }

I don't know VB, but does it automatically remove the CR/LF at the end of the printed string?

This is an Arduino forum. There may be someone here that can answer your VB.NET question, but do not expect that to happen. Your question would be better asked on a more appropriate forum.

I also posted it on stack overflow. I found some solid questions similar to mine on this forum. Hopefully someone can give me a hand.

It does. When the label loads the Serial output of the arduino string it does not include the CR/LF.

Thought I had a solution but it did not work

I'm not familiar with VB.Net, but C# should translate well. I've uploaded a simple project which shows how you can interface with your Arduino using the .Net framework. Receiving is handled on separate thread to keep the GUI responsive.

SerialColorThingie.zip (11.5 KB)
SerialThingie

Shown in the gif is the result with the following code running on the Arduino.

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Red Button On");
  delay(1000);

  Serial.println("Yellow Button On");
  delay(1000);

  Serial.println("Green Button On");
  delay(1000);
}

You'll have to change the port parameters to match your own setup. Let me know if you have any questions.

I have the code working now. Please see the original post for solution.

Why? What's changed?

(See the problem? Please don't do that again)