Pages: [1]   Go Down
Author Topic: momentary switch with Visual Basic  (Read 1483 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello to everyone,i need your advice..seems stupid but i cant figure out what is the exact code.I want to make a simple aplic. in VB2010 that switch a Led only at the moment mouse click the button.So far i got it working fine like a toogle switch but i wanted to be momentary swich and there is no exemple anyware.The Arduino code (witch i think it showuld be like) is :

int ledPin13 = 13;
int data;

void setup() {
Serial.begin(9600);
pinMode(ledPin13, OUTPUT);
}

void loop() {

if (Serial.available() > 0) {

data = Serial.read();

if (data == '1') {
digitalWrite(ledPin13, HIGH);
} else {
digitalWrite(ledPin13, LOW);
}
}
}


With the MonitorSerial seems to work like i want but i have no clue for the VB
For the VB code i want to start serial comunicacion on Form1_Load and to keep it open until the aplicacion is closed...something like:



Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Dim cambio13 As Boolean = True

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
        SerialPort1.PortName = "com17" 'Change port COM
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort1.Write("1")
    End Sub

End Class

Thank you,for helping
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have a Click event. That makes the icon do something when pressed and released while the mouse is over the graphic button image.

Perhaps you should look at the other events available for the Button1 object. MouseDown and MouseUp are probably more appropriate.
Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you for reply Paul,you are right,i tried before i post but still cant find the right code...anyway i will keep trying until i get it right.Never lose my hope smiley-grin
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do your Button1_MouseDown and Button1_MouseUp event handlers look like? Did you delete the Button1_Click handler when you added the mouse up and down handlers?
Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

He He i had a little error but it,s fixed smiley-grin if someone needs the code here it is:


Arduino code :



int ledPin13 = 13;
int ledPin12 = 12;
int dato;

void setup() {
Serial.begin(9600);
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}

void loop() {

if (Serial.available() > 0) {

dato = Serial.read();

if (dato == '1') {
digitalWrite(ledPin13, HIGH);
}
if (dato == '2') {
digitalWrite(ledPin13, LOW);
}
}
}

------------------------------------------------------->

VB code :

Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Dim Mypin13 As Boolean = True

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com17" 'Chage to your COM
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
        SerialPort1.Open()
        SerialPort1.Write("2")
        SerialPort1.Close()
    End Sub
End Class

...simple isnt it¿ ... trying is the key
thank you again Paul and all of this forum
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

juycce I understand how your VB code works but can't import into visual Studio 2010 Express which I'm just beginning with.
Any suggestion on how this should be imported or copied/pasted into VS would be appreciated.
Regards
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but can't import into visual Studio 2010 Express
Why can't you?

Of course, the posted code is only the code behind the form definition code, which was not posted.

You need to define a form with a button on it, called Button1. Then, select the button and look at the properties. At the top of the properties tab, there is a lightening bolt that shows events. Double click in the field next to the MouseDown event. The handler will be created, and you will be switched to the code behind page. Switch back to the form tab, and do the same for the MouseUp event.

Then, switch back to the form view, and double click on the form to add the Load event handler.

Copy and paste the code into the stubs.

Don't forget to add a SerialPort object to the form.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS for the hint I'll give it a go....

For those wanting a good I/O GUI from Visual Basic to Arduino the following link I've discovered looks promising:
http://mathias-wilhelm.de/arduino/projects/arduino-dashboard/

I've had a bit of a play with it late last night but there may be a bit of trap with the original and later version of MsTimer2.h as downloadable from this link http://arduino.cc/playground/Main/MsTimer2  I've still to spend more time on it but the original version of MsTimer2 seems to be compatible with the published application code.
Regards all
Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

exactly rbright u need to add to the form a "serialport1"  and one single "button".I will keep postin my next projects with arduino-Vb ..this has a huge potencial..u can do millions of things with it smiley-grin
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks juycce I now have the searialport added from the toolbox and have successfully duplicated your code and even more.

I've now also had time to get the Arduino Dashboard working as well (see link in my previous post) - a lot of interesting VB code that includes I/O monitoring & control of Arduino including graphing - I'd encourage tinkerers to look at that.
Cheers   
Logged

Pages: [1]   Go Up
Jump to: