Go Down

Topic: momentary switch with Visual Basic (Read 1 time) previous topic - next topic

juycce

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

PaulS

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.

juycce

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 :D

PaulS

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?

juycce

He He i had a little error but it,s fixed :D 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

Go Up