Arduino to VB.Net how to connect my codes

Hi guys please help me i dont know how to make this work :sweat_smile: I want to connect my arduino to vb.net and I am using visual studio 2013 SerialPort component. What I want is if the sensor in my arduino is triggered the level 1 (Button_Click1) will automatically show on vb app and same with the other sensors on my arduino too. Thank you

Arduino Code:

const int buttonPin = 2;
const int buttonPin1 = 4;
const int buttonPin2 = 6;
const int ledPin = 12;// the number of the pushbutton pin
const int ledPin1 = 13;
const int ledPin2 = 11;
const int buzzerPin = 8;// the number of the LED pin

// variables will change:
int buttonState = 0;
int buttonState2 = 0;// variable
int buttonState3 = 0;

void setup() {
  // initialize the LED pin as an output:
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buzzerPin, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT); 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  buttonState2 = digitalRead(buttonPin1);
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);  
  } 
  else {
    // turn LED off:
   
    digitalWrite(ledPin1, LOW); 
  }
  buttonState3 = digitalRead(buttonPin2);
  if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);
    digitalWrite(buzzerPin, HIGH);  
  } 
  else {
    // turn LED off:
   
    digitalWrite(ledPin2, LOW); 
    digitalWrite(buzzerPin, LOW);
  }
  
}

VB.Net Code:

Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Btnimage1.Click
        label1.Text = "ALERT LEVEL!!!"
        Timer1.Start()
        Label2.Text = " Level 1 = Alert Level, Monitor the River for the possible Flood in Barangay."


        Btnimage1.BackColor = Color.GreenYellow


        Picmyimage.Image = My.Resources.nangka_plain_1

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Btnimage2.Click
        label1.Text = "WARNING LEVEL!!!"
        Timer1.Start()

        Btnimage2.BackColor = Color.RoyalBlue

        Label2.Text = " Level 2 = Warning Level, Anouncing a Warning and Evacuation to all residence of Barangay."

        Picmyimage.Image = My.Resources.nangka_plain_2
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles Picmyimage.Click

    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles label1.Click

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Btnexit.Click
        End
    End Sub

    Private Sub Btnimage3_Click(sender As Object, e As EventArgs) Handles Btnimage3.Click
        label1.Text = "CRITICAL LEVEL!!!"
        Timer1.Start()

        Btnimage3.BackColor = Color.Red

        Label2.Text = " Level 3 = Critical Level, Forced Evacuation of all Residence in Barangay will be implemented."

        Picmyimage.Image = My.Resources.nangka_plain_3
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If label1.ForeColor = Color.Black Then
            label1.ForeColor = Color.DimGray
        ElseIf label1.ForeColor = Color.DimGray Then
            label1.ForeColor = Color.Red
        ElseIf label1.ForeColor = Color.Red Then
            label1.ForeColor = Color.Gray
        ElseIf label1.ForeColor = Color.Gray Then
            label1.ForeColor = Color.Black

        End If

    End Sub
End Class

What I want is if the sensor in my arduino is triggered the level 1 (Button_Click1) will automatically show on vb app and same with the other sensors on my arduino too.

Not going to happen.

You can write code in the serial data callback that calls the same function(s), though.

thanks for your reply sir. ahmm... how about if the sensor is triggered it will pop up a picture box on vb.net app? is that possible?

hi roy123 1. did you initialize serial communication like in this examplehttp://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/? 2.i would add to the if statement in the arduino code something like: Serial.println("a"); and in the vb net code ; SerialPort1.Open() SReader = SerialPort1.ReadLine() // serial read = to a string SerialPort1.Close()

and then if SReader=a ...

please ask if you have more questions/ didnt understand my reply

thanks for your reply sir. i will try this :) btw my project is a simple water level indicator so what i want is if the level1 sensor is triggered it will pop up a picture box on vb.net app automatically without clicking any button on app. is that possible?

yes it is. check this tutorial http://waihung.net/arduino-controlled-by-visual-basic-program/ it seem similar to what you want to do.

try modifying it that instead of the buttons a timer http://vb.net-informations.com/gui/timer-vb.htm will check the serial input for the water level indicator.

bombom: yes it is. check this tutorial http://waihung.net/arduino-controlled-by-visual-basic-program/

That VB.NET program is for controlling a LED on the Arduino Uno board. The OP wants the reverse.

As PaulS wrote about previously, the OP will need to use callbacks.

@ the OP, You have asked this question in various forms THREE times already. It is very rude to do that. Your posts: http://forum.arduino.cc/index.php?action=profile;u=286268;sa=showPosts

Thank you very much for your helps guys :) I will try this and hope to make it work.. I am very sorry for being so annoying in this forum but I am learning from you guys :D. Thanks a lot.

Thanks guys for your helps.
I will use Firmata instead because i couldn’t get my output using this. :sweat_smile:
Please help and guide me to convert this code into Firmata.
Thanks a lot.

Arduino Code:

const int buttonPin = 2;
const int buttonPin1 = 4;
const int buttonPin2 = 6;
const int ledPin = 12;// the number of the pushbutton pin
const int ledPin1 = 13;
const int ledPin2 = 11;
const int buzzerPin = 8;// the number of the LED pin

// variables will change:
int buttonState = 0;
int buttonState2 = 0;// variable
int buttonState3 = 0;

void setup() {
  // initialize the LED pin as an output:
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buzzerPin, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT); 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  buttonState2 = digitalRead(buttonPin1);
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);  
  } 
  else {
    // turn LED off:
   
    digitalWrite(ledPin1, LOW); 
  }
  buttonState3 = digitalRead(buttonPin2);
  if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);
    digitalWrite(buzzerPin, HIGH);  
  } 
  else {
    // turn LED off:
   
    digitalWrite(ledPin2, LOW); 
    digitalWrite(buzzerPin, LOW);
  }
  
}

Please help and guide me to convert this code into Firmata.

Firmata is a sketch in it's own right. You do NOT convert that sketch to anything.

Of course you can't connect that code to a VB.net application because you never open, read from, or write to the serial port.

I think you should have a look at this demo code for communicating with a PC using Python. The Arduino code will obviously work with any similar PC program and also with the Arduino serial monitor.

It might be a good starting point to write a VB.net program that does the same as the Python program as then you will know that the Arduino code works and any problems are on the PC side.

The Python code should run under Windows if you put in the correct COM port reference.

...R

PaulS:
Of course you can’t connect that code to a VB.net application because you never open, read from, or write to the serial port.

Sir how can i implement those on my arduino code can you teach me? i have no idea on how to implement those on my code.
thanks.

roy123: how can i implement those on my arduino code can you teach me? i have no idea on how to implement those on my code.

That was the purpose of Reply #10

..R