0% found this document useful (0 votes)
37 views

2.-Ahora Con Dos Botones y Dos LED: Visual Basic 2010

The document describes two programs that control LEDs using buttons. The first program uses two buttons to control two LEDs by sending character strings like "12 on" or "12 off" to an Arduino board over serial. The second program simplifies this to use single characters like "1" or "2" sent from buttons to control the LEDs. The Arduino program receives the serial data and turns the appropriate LED pin high or low based on the received character.

Uploaded by

flavio
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

2.-Ahora Con Dos Botones y Dos LED: Visual Basic 2010

The document describes two programs that control LEDs using buttons. The first program uses two buttons to control two LEDs by sending character strings like "12 on" or "12 off" to an Arduino board over serial. The second program simplifies this to use single characters like "1" or "2" sent from buttons to control the LEDs. The Arduino program receives the serial data and turns the appropriate LED pin high or low based on the received character.

Uploaded by

flavio
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

- Ahora con dos botones y dos LED


- Se trata de tener varios Botones para controlar varios LED.
Cuando pulsamos el Botn 1, se enciende el LED13, cuando
volvemos a pulsarlo se apaga el LED13
Cuando pulsamos el Botn 2, se enciende el LED12, cuando
volvemos a pulsarlo se apaga el LED12

Visual Basic 2010

Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim cambio12 As Boolean = True
Dim cambio13 As Boolean = True
Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com3" 'CAMBIA EL
PUERTO 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.Open()
If cambio13 = True Then
SerialPort1.Write("13 on")
Else
SerialPort1.Write("13 off")
End If
cambio13 = Not (cambio13)
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
SerialPort1.Open()
If cambio12 = True Then

SerialPort1.Write("12 on")
Else
SerialPort1.Write("12 off")
End If
cambio12 = Not (cambio12)
SerialPort1.Close()
End Sub
End Class

- En el Arduino creamos este cdigo.

Programa para el
Arduino
char inData[20]; // Allocate some
space for the string
char inChar=-1; // Where to store
the character read
byte index = 0; // Index into
array; where to store the
character
void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
char Comp(char* This) {
while (Serial.available() > 0) //
Don't read unless
// there you know there is data
{
if(index < 19) // One less than the
size of the array
{
inChar = Serial.read(); // Read a
character
inData[index] = inChar; // Store it
index++; // Increment where to
write next

inData[index] = '\0'; // Null


terminate the string
}
}
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("12 on")==0)
{ digitalWrite(12, HIGH);}
if (Comp("12 off")==0)
{digitalWrite(12, LOW);}
if (Comp("13 on")==0)
{ digitalWrite(13, HIGH);}
if (Comp("13 off")==0)
{digitalWrite(13, LOW);}
}

- Lo que hace el programa Visual Basic anterior es enviar la frase "12


on" al puerto serie (En este caso no es el puerto serie, se trata del USB),
el Arduino recoje la informacin que le llega por su puerto serie y
"descompone" las letras recibidas, si las letras "descompuestas" son 12
on, entonces se enciende el LED 12.

- Con otro cdigo...


- Ahora lo mismo pero con otro cdigo ms sencillo, dos botones y
dos LED.
- En este caso solo se enva un carcter ("1"), no una cadena "12
on")

Visual Basic
' VISUAL BASIC 2010 (dos Button y un
SerialPort)
' Programa realizado por Juan Antonio
Villalpando
' [email protected]
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim cambio12 As Boolean = True
Dim cambio13 As Boolean = True
Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com3" 'CAMBIA EL
PUERTO 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.Open()
If cambio13 = True Then
SerialPort1.Write("1")
Else
SerialPort1.Write("2")
End If
cambio13 = Not (cambio13)
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
SerialPort1.Open()
If cambio12 = True Then
SerialPort1.Write("3")
Else
SerialPort1.Write("4")
End If
cambio12 = Not (cambio12)
SerialPort1.Close()

You might also like