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

Visual Basic Tutoriales: Arduino

This document discusses using Visual Basic (VB.NET) to capture proximity sensor measurements from an Arduino board connected to a computer. The code samples show how to read measurements from the sensor pin on the Arduino and display the readings and corresponding color on a label based on the distance detected. The code establishes serial communication with the Arduino, reads the sensor value as a string, converts it to centimeters, and outputs it to the serial port and label with a color indicating the distance.

Uploaded by

rls rls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Visual Basic Tutoriales: Arduino

This document discusses using Visual Basic (VB.NET) to capture proximity sensor measurements from an Arduino board connected to a computer. The code samples show how to read measurements from the sensor pin on the Arduino and display the readings and corresponding color on a label based on the distance detected. The code establishes serial communication with the Arduino, reads the sensor value as a string, converts it to centimeters, and outputs it to the serial port and label with a color indicating the distance.

Uploaded by

rls rls
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Compartir 0 Más Siguiente blog» Crear un blog Acceder

VISUAL BASIC TUTORIALES


Etiquetas

ARDUINO ANDROID
APP
06- Captura de Sensor de Proximidad (PING) con Visual Basic INVENTOR APPINVENTOR ARDUINO
(VB.NET).Arduino ASP

Se trata de una pequeña aplicacion que permite capturar las mediciones de un


BLOC DE NOTAS
Sensor de Proximidad (PING), a traves de una placa Arduino, conectada al
ordenador. CAPTURA
CHAT CHATING
COLAS

COMUNICACION
CONEXION
CONTROL

DETECCION
DIBUJO

DRAWING

ESCRITORIO

Codigo: FISICA

VB.NET
GRAFICOS
Form1 GRAPHICS
IMAGEN
Public Class Form1 IMAGENES
INTERNET IP
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles LED
Button1.Click
Try
Button1.Visible = False
SerialPort1.PortName = TextBox3.Text MOVIMIENTO
SerialPort1.Open()
Timer1.Start()
NETWORKING
Catch ex As Exception
MsgBox(ex.Message) PANTALLA
Button1.Visible = True PILAS
End Try

End Sub
PUERTO SERIAL

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles


Timer1.Tick RECONOCIMIENTO VOZ
Dim VALOR As String = SerialPort1.ReadLine
If VALOR < 15 Then REMOTO
Label1.ForeColor = Color.Red
ElseIf VALOR <= 30 Then
Label1.ForeColor = Color.Yellow
ElseIf VALOR > 30 Then
SERVO
Label1.ForeColor = Color.Lime
End If
Label1.Text = VALOR SONIDO
SPEECH RECOGNITION
End Sub
TCP
End Class TEXTO

ARDUINO
UDP
*/ USB VIDEO
VIDEOCAMARA VIDEOCONFERENCIA
// this constant won't change. It's the pin number VIDEOLLAMADA
// of the sensor's output: VIDEOVIGILANCIA
const int pingPin = 7; VOZ WEB
WEBCAM
void setup() {
// initialize serial communication:
Serial.begin(9600); Páginas
}
Página principal
void loop() ANDROID
{ ARDUINO
// establish variables for duration of the ping, BASE DATOS
// and the distance result in inches and centimeters: CHAT, WEBCAM
long duration, cm;
CHAT, VIDEOCONFERENCIA
CIENCIAS
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
ESCRITORIO REMOTO 1
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT); GRAFICOS
digitalWrite(pingPin, LOW); MISCELANEA
delayMicroseconds(2); MVB
digitalWrite(pingPin, HIGH); NETWORKING
delayMicroseconds(5); RECONOCIMIENTO DE VOZ
digitalWrite(pingPin, LOW); VBSCRIPT

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending Archivo del blog
// of the ping to the reception of its echo off of an object.
▼ 2014 (40)
pinMode(pingPin, INPUT);
► abril (4)
duration = pulseIn(pingPin, HIGH);
▼ marzo (17)
// convert the time into a distance Resaltar, Remarcar y Guardar el Texto
de un RichTe...
cm = microsecondsToCentimeters(duration); 08-Hablando con Arduino en Visual
Basic (VB.NET). ...
05- Imagenes del Escritorio en la Web
Serial.print(cm); con ASP y V...
Serial.println(); 07- Control Remoto (Via Web) de 3
Servos con Visua...
delay(1000); 04- Imagenes desde Webcam a Pagina
} Web con ASP y ...
03- Visualizador de Imagenes en la
long microsecondsToCentimeters(long microseconds) Web con ASP y V...
{ 02- Galeria de Imagenes en la Web con
// The speed of sound is 340 m/s or 29 microseconds per centimeter. ASP y Visual...
// The ping travels out and back, so to find the distance of the Conversor de Sistemas Numericos con
// object we take half of the distance travelled. Visual Basic (...
return microseconds / 29 / 2;
Informacion Basica de las Unidades de
} Memoria con ...
18- Copiar y Guardar Parte de una
Imagen con Visua...
Extraer USB (Pendrive) con Seguridad
05- LED Encendido mientras se presiona un Boton con Visual Basic (VB.NET) en VIsual Bas...
Activar y Desactivar el Firewall de
Se trata de una pequeña aplicacion que mantiene encendido un LED solo Windows7 con V...
mientras se presiona un boton.
UDP Basico con Visual Basic (VB.NET)
TCP Basico con Visual Basic (VB.NET)
06- Captura de Sensor de Proximidad
(PING) con Vis...
Visualizar la Señal de un Microfono
(Sonido) con V...
03- Jugando con el Reconocimiento de
Voz en Visual...

► febrero (14)
► enero (5)

► 2013 (69)

Datos personales

EGODISTONICO

Ver todo mi perfil


Codigo:

VB.NET

Form1:

Public Class Form1

Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs)


Handles Button1.MouseDown
Button1.BackColor = Color.Red
Button1.Text = "ENCENDIDO"
SerialPort1.Write("1")
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs)
Handles Button1.MouseUp
Button1.BackColor = Color.Black
Button1.Text = "APAGADO"
SerialPort1.Write("0")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles


Button2.Click
Button1.Visible = True
Button2.Visible = False

Try
SerialPort1.PortName = TextBox1.Text
SerialPort1.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub Form1_FormClosing(sender As Object, e As


FormClosingEventArgs) Handles Me.FormClosing
SerialPort1.Close()
End Sub
End Class

ARDUINO:

void setup ()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
int recibido;
recibido=Serial.read();
if(recibido== '1')
{
digitalWrite(13,HIGH);
}
else if (recibido=='0')
{
digitalWrite(13,LOW);
}
}

04- Control de 4 Servomotores con Visual Basic (VB.NET)

Se trata de una pequeña aplicación que permite controlar un servomotor con


Visual Basic a través de una placa Arduino.

NOTA IMPORTANTE:

En el codigo para Arduino se ha escrito servoX.write(0),


servoX.write(90),servoX.write(180), por necesidades de la simulacion en
Proteus, cuando segun la documentacion de Arduino se debe escribir:
servoX.write(-90), servoX.write(0),servoX.write(+90). Tenedlo en cuenta.
Codigo:

VB.NET
Form1
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = TextBoxPUERTO.Text
End Sub
Private Sub ButtonCONECTAR_Click(sender As Object, e As EventArgs)
Handles ButtonCONECTAR.Click
Try
SerialPort1.PortName = TextBoxPUERTO.Text
SerialPort1.Open()
ButtonCONECTAR.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles
TrackBar1.Scroll

Try

If ButtonCONECTAR.Enabled = False Then


If TrackBar1.Value >= -90 And TrackBar1.Value <= -45 Then
SerialPort1.Write("0") '-90
ElseIf TrackBar1.Value > -45 And TrackBar1.Value <= 45 Then
SerialPort1.Write("1") '0
ElseIf TrackBar1.Value > 45 And TrackBar1.Value <= 90 Then
SerialPort1.Write("2") '90
End If
Label1.Text = TrackBar1.Value
Else
MsgBox("DEBES CONECTAR EL PUERTO")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs) Handles


TrackBar2.Scroll
Try
If ButtonCONECTAR.Enabled = False Then
If TrackBar2.Value >= -90 And TrackBar2.Value <= -45 Then
SerialPort1.Write("A") '-90
ElseIf TrackBar2.Value > -45 And TrackBar2.Value <= 45 Then
SerialPort1.Write("B") '0
ElseIf TrackBar2.Value > 45 And TrackBar2.Value <= 90 Then
SerialPort1.Write("C") '90
End If
Label2.Text = TrackBar2.Value
Else
MsgBox("DEBES CONECTAR EL PUERTO")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TrackBar3_Scroll(sender As Object, e As EventArgs) Handles
TrackBar3.Scroll
Try
If ButtonCONECTAR.Enabled = False Then
If TrackBar3.Value >= -90 And TrackBar3.Value <= -45 Then
SerialPort1.Write("a") '-90
ElseIf TrackBar3.Value > -45 And TrackBar3.Value <= 45 Then
SerialPort1.Write("b") '0
ElseIf TrackBar3.Value > 45 And TrackBar3.Value <= 90 Then
SerialPort1.Write("c") '90
End If
Label6.Text = TrackBar3.Value
Else
MsgBox("DEBES CONECTAR EL PUERTO")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TrackBar4_Scroll(sender As Object, e As EventArgs) Handles
TrackBar4.Scroll
Try
If ButtonCONECTAR.Enabled = False Then
If TrackBar4.Value >= -90 And TrackBar4.Value <= -45 Then
SerialPort1.Write("X") '-90
ElseIf TrackBar4.Value > -45 And TrackBar4.Value <= 45 Then
SerialPort1.Write("Y") '0
ElseIf TrackBar4.Value > 45 And TrackBar4.Value <= 90 Then
SerialPort1.Write("Z") '90
End If
Label8.Text = TrackBar4.Value
Else
MsgBox("DEBES CONECTAR EL PUERTO")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
ARDUINO:

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int recibido ;
void setup()
{
Serial.begin(9600);
servo1.attach(10);
servo2.attach(11);
servo3.attach(6);
servo4.attach(5);

servo1.write (90);
servo2.write (90);
servo3.write (90);
servo4.write (90);

}
void loop()
{

if (Serial.available() > 0) {
// lee el byte entrante:
recibido = Serial.read();
switch (recibido)
{

case '0':servo1.write (0); break;


case '1':servo1.write (90);break;
case '2':servo1.write (180);break;

case 'A':servo2.write (0);break;


case 'B':servo2.write (90);break;
case 'C':servo2.write (180);break;

case 'a':servo3.write (0); break;


case 'b':servo3.write (90);break;
case 'c':servo3.write (180);break;

case 'X':servo4.write (0);break;


case 'Y':servo4.write (90);break;
case 'Z':servo4.write (180);break;

}
}

03- Control de Servomotor con Visual Basic (VB.NET) y también con


Potenciómetro. Arduino

Se trata de una pequeña aplicación que permite controlar un servomotor con


Visual Basic a través de una placa Arduino. Se añade el código para el mismo
control pero con potenciómetro en lugar de VB.NET.
NOTA IMPORTANTE:

En el codigo para Arduino se ha escrito servoX.write(0),


servoX.write(90),servoX.write(180), por necesidades de la simulacion en
Proteus, cuando segun la documentacion de Arduino debe escribir:
servoX.write(-90), servoX.write(0),servoX.write(+90). Tenedlo en cuenta.

Codigos:

VB.NET

Form1

Public Class Form1


Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles
TrackBar1.Scroll

Try
If ButtonCONECTAR.Enabled = False Then
If TrackBar1.Value >= -180 And TrackBar1.Value <= -90 Then
SerialPort1.Write("0") '-180
ElseIf TrackBar1.Value > -90 And TrackBar1.Value <= 90 Then
SerialPort1.Write("1") '0
ElseIf TrackBar1.Value > 90 And TrackBar1.Value <= 180 Then
SerialPort1.Write("2") '180

End If
Label1.Text = TrackBar1.Value
Else
MsgBox("DEBES CONECTAR EL PUERTO")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

Private Sub ButtonCONECTAR_Click(sender As Object, e As EventArgs)


Handles ButtonCONECTAR.Click
Try
SerialPort1.PortName = TextBoxPUERTO.Text
SerialPort1.Open()
ButtonCONECTAR.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = TextBoxPUERTO.Text
End Sub
End Class
ARDUINO: (VB.NET)

#include <Servo.h>
Servo servo1;
int angulo ;
int recibido ;
void setup()
{

servo1.attach(9);

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

if (Serial.available() > 0) {
// lee el byte entrante:
recibido = Serial.read();
switch (recibido)
{

case '0':angulo = 0; break;


case '1':angulo = 90;break;
case '2':angulo = 180;break;
}
servo1.write (angulo);
}
}

ARDUINO: (POTENCIOMETRO )

#include <Servo.h>
Servo servo1;
int pot1=0;
int valor;
int valoranterior=0;
int diferencia;
int threshold=30;
void setup()
{

servo1.attach(9);
}
void loop()
{
valor=analogRead(pot1);
diferencia=valor-valoranterior;
if(diferencia <-threshold ||diferencia > threshold)
{
valoranterior=valor;
valor= map (valor,0,1023,0,179);
servo1.write(valor);
}
delay (10);
}

02-Control de Luminosidad de un LED con Visual Basic (VB.NET).Arduino

Se trata de una pequeña aplicacion que permite controlar la luminosidad de un


LED desde el ordenador. (No es PWM)

Codigo:

Form1

Public Class Form1

Private Sub ButtonCONECTAR_Click(sender As Object, e As EventArgs)


Handles ButtonCONECTAR.Click
Try

SerialPort1.PortName = TextBoxPUERTO.Text
SerialPort1.Open()

ButtonCONECTAR.Enabled = False
Timer1.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Form1_FormClosing(sender As Object, e As


FormClosingEventArgs) Handles Me.FormClosing
SerialPort1.Close()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles


MyBase.Load
SerialPort1.Close()
End Sub

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles


TrackBar1.Scroll

If TrackBar1.Value <= 50 Then


SerialPort1.Write("0")
ElseIf TrackBar1.Value > 50 And TrackBar1.Value <= 100 Then
SerialPort1.Write("1")
ElseIf TrackBar1.Value > 100 And TrackBar1.Value <= 150 Then
SerialPort1.Write("2")
ElseIf TrackBar1.Value > 150 And TrackBar1.Value <= 200 Then
SerialPort1.Write("3")
ElseIf TrackBar1.Value > 200 And TrackBar1.Value <= 235 Then
SerialPort1.Write("4")
Else
SerialPort1.Write("5")

End If

Label1.Text = TrackBar1.Value
End Sub
End Class

ARDUINO

int led = 9; // the pin that the LED is attached to


int brillo = 0; // how bright the LED is
int recibido = 0; // how many points to fade the LED by

// the setup routine runs once when you press reset:


void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);

Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {
// envia datos solamente cuando recibe datos
if (Serial.available() > 0) {
// lee el byte entrante:
recibido = Serial.read();

switch (recibido)
{
case '0':brillo = 0;break;
case '1':brillo = 50;break;
case '2':brillo = 100;break;
case '3':brillo = 150;break;
case '4':brillo = 200;break;
case '5':brillo = 255;break;
}
analogWrite(led, brillo);
}
}
01- Encender y apagar un LED con Visual Basic (VB.NET).Arduino

Primer video de una serie dedicada al control de Arduino con Visual Basic.
Esta es la típica aplicación que permite encender y apagar un LED desde
VB.NET.
En el video se utiliza la aplicación Webcam como Videocámara Remota,
disponible en este Blog.

Codigo:

Form1:

Public Class Form1


Private Sub ButtonCONECTAR_Click(sender As Object, e As EventArgs)
Handles ButtonCONECTAR.Click
Try
SerialPort1.PortName = TextBoxPUERTO.Text
SerialPort1.Open()
ButtonENCENDER.Enabled = True
ButtonAPAGAR.Enabled = True
ButtonCONECTAR.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
Private Sub ButtonENCENDER_Click(sender As Object, e As EventArgs)
Handles ButtonENCENDER.Click
SerialPort1.Write("1")
End Sub
Private Sub ButtonAPAGAR_Click(sender As Object, e As EventArgs)
Handles ButtonAPAGAR.Click
SerialPort1.Write("0")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As
FormClosingEventArgs) Handles Me.FormClosing
SerialPort1.Close()
End Sub
End Class

ARDUINO

void setup ()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
int recibido;
recibido=Serial.read();
if(recibido== '1')
{
digitalWrite(13,HIGH);
}
else if (recibido=='0')
{
digitalWrite(13,LOW);
}
}
Recomendar esto en Google

No hay comentarios:
Publicar un comentario en la entrada

Introduce tu comentario...

Comentar como: Cuenta de Google

Publicar Vista previa

Página principal

Suscribirse a: Entradas (Atom)

SPAM

Plantilla Simple. Con la tecnología de Blogger.

You might also like