15
Visual Basic 2010 y Arduino – Servo control con LCD display. En el post anterior presente como controlar un servo utilizando un Slider en Visual BASIC 2010, ahora les traigo una actualización para mejorar su apariencia y añadir nuevas funciones. Espero les guste, para que les funcione idéntico tienen que instalar un tipo de letra digital si no la tienen los dígitos no lucirán iguales, lo encontraran en el mismo sitio que este archivo. (11569_DIGITAL.ttf ) Enlace a la publicación anterior: Arduino – Visual Basic 2010 Servo control con LCD display

Contorl de Servo Co Visual

Embed Size (px)

DESCRIPTION

Contorl de Servo Co Visual

Citation preview

Page 1: Contorl de Servo Co Visual

Visual Basic 2010 y Arduino – Servo control con LCD display. En el post anterior presente como controlar un servo utilizando un Slider en Visual BASIC 2010, ahora les traigo una actualización para mejorar su apariencia y añadir nuevas funciones. Espero les guste, para que les funcione idéntico tienen que instalar un tipo de letra digital si no la tienen los dígitos no lucirán iguales, lo encontraran en el mismo sitio que este archivo. (11569_DIGITAL.ttf ) 

Enlace a la publicación anterior: 

Arduino – Visual Basic 2010 Servo control con LCD display

 

Arduino Sketch

Page 2: Contorl de Servo Co Visual

_______________________________________________________________________

// apcexpert.wordpress.com// servo control con LCD display position#include <Servo.h>#include <LiquidCrystal.h>Servo myservo;int servoPosition = 1;LiquidCrystal lcd(8, 9, 4, 5, 6, 7);void setup() {lcd.begin(16, 2);lcd.print("Servo Position:");myservo.attach(13);Serial.begin(9600);}void loop() {static int val = 0;lcd.setCursor(0, 1);if(Serial.available()) {char ch = Serial.read();switch(ch) {case '0'...'9':val = val * 10 + ch - '0';break;case 's':myservo.write(val);lcd.print((float)val);val = 0;break;}}}

_____________________________________________________________

Aquí les dejo el código fuente para que puedan comparar las diferencias: 

Public Class ServoControllerPrivate serialPort As New IO.Ports.SerialPort Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TryWith serialPort.PortName = “COM3”.BaudRate = 9600.Parity = IO.Ports.Parity.None

Page 3: Contorl de Servo Co Visual

.DataBits = 8

.StopBits = IO.Ports.StopBits.OneEnd With serialPort.Open()serialPort.Write(“0s”) Catch ex As ExceptionMsgBox(ex.ToString)End Try End Sub Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll serialPort.Write(TrackBar1.Value & “0s“)ProgressBar1.Value = (TrackBar1.Value * 10) TextBox1.Text = TrackBar1.Value * 10 & (” “)End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click End SubEnd Class 

Page 4: Contorl de Servo Co Visual

Arduino servo control. En este proyecto se utiliza un servo que estará controlado por una PC atabes de una aplicación creada en VB2010, Donde un Slider controlara la posición del servo y será desplegada en un LCD display. Los servos vienen con dos códigos de colores: 1) Amarillo = señal Rojo = Voltaje+ Marrón = Tierra 2) Blanco = señal Rojo = Voltaje+ Negro = Tierra Conexión del servo, Señal = pin13 Conexiones del LiquidCrystal lcd(8,9,4,5,6,7); 

Arduino servo control Sketch 

// apcexpert.wordpress.com// servo control con LCD display position#include <Servo.h>#include <LiquidCrystal.h>Servo myservo;int servoPosition = 1;LiquidCrystal lcd(8, 9, 4, 5, 6, 7);void setup() {lcd.begin(16, 2);lcd.print("Servo Position:");myservo.attach(13);Serial.begin(9600);}void loop() {static int val = 0;lcd.setCursor(0, 1);if(Serial.available()) {char ch = Serial.read();switch(ch) {case '0'...'9':val = val * 10 + ch - '0';break;case 's':myservo.write(val);lcd.print((float)val);val = 0;break;}}}

Page 5: Contorl de Servo Co Visual

Código fuente Visual Basic 2010 (ServoController1) 

Public Class ServoControllerPrivate serialPort As New IO.Ports.SerialPort Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TryWith serialPort.PortName = “COM3”.BaudRate = 9600.Parity = IO.Ports.Parity.None.DataBits = 8.StopBits = IO.Ports.StopBits.OneEnd With serialPort.Open()serialPort.Write(“0s”)Catch ex As ExceptionMsgBox(ex.ToString)End Try End Sub Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll serialPort.Write(TrackBar1.Value & “0s”)End Sub End Class 

/* * created by Rui Santos, http://randomnerdtutorials.wordpress.com * Control a servo motor with Visual Basic * 2013 */ #include <Servo.h> Servo myservo; // create servo object to control a servo

Page 6: Contorl de Servo Co Visual

void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object Serial.begin(9600); //begins serial communication} void loop() { int pos; if (Serial.available()){ delay(100); while(Serial.available()>0){ pos=Serial.read(); //reads the value sent from Visual Basic if(pos=='0') myservo.write(90); //rotates the servo 90 degrees (Left) else if(pos=='1') myservo.write(-90); //rotates the servo 90 degrees (right) else if(pos=='2') myservo.write(180); //rotates the servo 180 degrees (Left) else if(pos=='3') myservo.write(-180); //rotates the servo 180 degrees (right) } } }

// By: 100fubu100

int ledR= 8; int ledG= 9; int ledB= 10;

void setup(){ pinMode(ledR,OUTPUT); pinMode(ledG,OUTPUT); pinMode(ledB,OUTPUT); Serial.begin(9600); }

void loop(){ int val, r, g, b; if (Serial.available()) { delay(100); while (Serial.available ()>9) { val=Serial.read(); int number[8] = {0,0,0,0,0,0,0,0};

Page 7: Contorl de Servo Co Visual

for (int i = 0; i < 9; i = i++) { number[i]= Serial.read(); } int num0 = number[0]-'0'; int num1 = number[1]-'0'; int num2 = number[2]-'0'; r= num0*100+ num1*10 + num2; int num3= number[3]-'0'; int num4 = number[4]-'0'; int num5 = number[5]-'0'; g= num3*100+ num4*10 + num5; int num6 = number[6]-'0'; int num7= number[7]-'0'; int num8= number[8]-'0'; b= num6*100+ num7*10 + num8; color( r, g, b); } Serial.flush(); }}

void color(int red, int green, int blue){ analogWrite(ledR, red); analogWrite(ledG, green); analogWrite(ledB, blue);}Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1.BaudRate = 9600 SerialPort1.DataBits = 8 SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.PortName = "COM3" SerialPort1.Open()

TextBox_R.Text = 0 TextBox_G.Text = 0 TextBox_B.Text = 0 TextBox_RGB.Text = 0 '( 1000000000 + R*1000000 + G*1000 + B) TextBox_RGB.Hide()

End Sub

Page 8: Contorl de Servo Co Visual

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_R.Scroll TextBox_R.Text = TrackBar_R.Value Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)

End SubPrivate Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_G.Scroll TextBox_G.Text = TrackBar_G.Value Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)

End Sub

Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_B.Scroll TextBox_B.Text = TrackBar_B.Value Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnviarDatos.Click TextBox_RGB.Text = 1000000000 + TextBox_R.Text * 1000000 + TextBox_G.Text * 1000 + TextBox_B.Text SerialPort1.Write(TextBox_RGB.Text) 'System.Threading.Thread.Sleep(200) 'TextBox5.Text = SerialPort1.ReadExisting() End Sub

Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

End Sub

Private ClD As New ColorDialog Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If ClD.ShowDialog = Windows.Forms.DialogResult.OK Then Panel1.BackColor = ClD.Color Dim tmpBrush As New SolidBrush(Panel1.BackColor) With tmpBrush.Color TextBox_B.Text = .B TextBox_G.Text = .G TextBox_R.Text = .R TrackBar_R.Value = TextBox_R.Text TrackBar_G.Value = TextBox_G.Text TrackBar_B.Value = TextBox_B.Text End With 'Without the With statement, you need to put in:

'for example. End If End Sub Private Sub Delet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delet.Click

Page 9: Contorl de Servo Co Visual

Panel1.BackColor = Color.Black TextBox_B.Text = 0 TextBox_G.Text = 0 TextBox_R.Text = 0 TrackBar_R.Value = 0 TrackBar_G.Value = 0 TrackBar_B.Value = 0 SerialPort1.Write(1000000000) End SubEnd Class

Requirements

1. Any Arduino (I am using Arduino Uno)2. Visual Basic Express 2010 You can download VB from here3. RGB Led4.Step 1: Create application

In this instructable, we will be making a program to control RGB Led.

Follow the following steps:

Page 10: Contorl de Servo Co Visual

Click on New Project Select New Windows From Application Enter a name for your application

Step 2: Create application and upload code to Arduino

Page 12: Contorl de Servo Co Visual

Go ahead according to these pictures.

1. You open Arduino-Code-RGB.txt and copy this code to Arduino and compile it.

2. Then you open RGB-VB and change your port.

3. Then connect pin 8 to the color blue, pin 9 to the color green, pin 10 to the color red and finally connect GND.

Arduino-Code-RGB-LED.txt1 KB

Code-RGB-LED.txt2 KB

Step 3: Testing

Page 13: Contorl de Servo Co Visual

1. Open the VB program that you created.2. Click on the buttons to see what happens.

3. I'm trying to control a servo with arduino uno and C# but the servo doesn't move

4. Here is the Arduino code:5. #include <Servo.h>6. Servo servoT1;7.8. void setup()9. {10. Serial.begin(9600);11. servoT1.attach(9); 12. }13. int i=0;14. void loop()15. {16. if (Serial.available() > 0)17. {18. i=Serial.read();19. i=map(i, 0, 100, 0, 179);20. servoT1.write(i);21. }22. }

Page 14: Contorl de Servo Co Visual

23. Here is the C# code:24. using System;25. using System.Collections.Generic;26. using System.ComponentModel;27. using System.Data;28. using System.Drawing;29. using System.Linq;30. using System.Text;31. using System.Threading.Tasks;32. using System.Windows.Forms;33. using System.IO.Ports;34.35. namespace arduino_throttle36. {37. public partial class Form1 : Form38. {39. SerialPort uno1 = new SerialPort("COM3", 9600);40.41. public Form1()42. {43. InitializeComponent();44. }45.46. private void hScrollBar1_Scroll(object sender,

ScrollEventArgs e)47. {48. uno1.Open();49. uno1.Write(HScroll.ToString());50. uno1.Close();51. }52. }53. }