Pages: [1]   Go Down
Author Topic: Visual Basic 2010 + Arduino Serial  (Read 1663 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi all..

i'm noob here..

sorry if i bother you all..

noob question here..

i am wondering how to put the gas sensor in arduino to my listbox in visual basic..

this is my VB code.

Code:
[code]
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim valGas As Integer

    Sub EventHandler(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim datareceived As Boolean
        Dim valGas As String

        datareceived = False
        valGas = " "

        While Not datareceived


            valGas = SerialPort1.ReadByte()
            If valGas = 10 Or valGas = 13 Then
                datareceived = True
            Else
                valGas = valGas + Chr(valGas)
            End If

        End While
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com3" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
    End Sub


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


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
        lblTime.Hide()
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblTime.Text = TimeOfDay
    End Sub

 
    Private Sub EventLog1_EntryWritten(ByVal sender As System.Object, ByVal e As System.Diagnostics.EntryWrittenEventArgs) Handles EventLog1.EntryWritten

    End Sub
End Class
[/code]

and this is my arduino coding..

Code:
int clock = 998;
int hour = 9;
int min = 55;
int sec = 0;
int lamp1 = 13;
int lamp2 = 4;
int valgas = 10;

void setup()
{
  Serial.begin(9600); //Set serial baud rate to 9600 bps
  pinMode(lamp1, OUTPUT);
  pinMode(lamp2, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(lamp1, LOW); //turn off LED
  digitalWrite(lamp2, LOW); //turn off LED
 
}
void loop ()
{
  valgas = analogRead(0);

  if(valgas>300)
    {
      digitalWrite (10,HIGH);
      digitalWrite (lamp1,LOW);
      digitalWrite (lamp2,LOW);
      Serial.println(valgas);
     
    }
    else
    {
      digitalWrite (10,LOW);
    }
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
if (val == 1) { // test for command 1 then turn on LED

Serial.println("LED on");
digitalWrite(lamp1, HIGH); // turn on LED
digitalWrite(lamp2, HIGH);
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("LED OFF");
digitalWrite(lamp1, LOW); // turn off LED
digitalWrite(lamp2, HIGH);


}
else // if not one of above command, do nothing

 
  delay(clock);
  sec = sec + 1 ;
  if (sec <= 40)
  {
    digitalWrite(lamp1, HIGH);
    digitalWrite(lamp2, HIGH);
  }
  if (sec > 40)
  {
    digitalWrite(lamp1, LOW);
    digitalWrite(lamp2, LOW);
  }

  /* if (min > 20 && min < 40) {
   digitalWrite(lamp, HIGH);
   } else {
   digitalWrite(lamp, LOW);
   } */

  // time keeping stuff

  if (sec > 59) {
    sec = 0;
    min = min + 1;
  }
  if (min > 59) {
    min = 0;
    hour = hour + 1;
  }
  if (hour > 23) {
    hour = 0;

 
  }
 
  Serial.print(hour);
  Serial.print(":");
  Serial.print(min);
  Serial.print(":");
  Serial.print(sec);
  Serial.print("s");
  }




i want to put the value of "valgas" in visual basic...

hope u  guys can help me correcting my errors..really appreciate it..thanx in advance..
Logged

Australia
Offline Offline
Sr. Member
****
Karma: 13
Posts: 451
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Split the problem into two.

In the Arduino sketch replace:

Code:
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
if (val == 1) { // test for command 1 then turn on LED

with

Code:
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
val = 1;
if (val == 1) { // test for command 1 then turn on LED

This will force the sketch to output the data without receiving the command to do so.
Open serial monitor in the Arduino IDE, setting the correct baud rate and watch the data come up on the screen.
If it does not come up as expected, put in some Serial.print  commands through the sketch to track the problem.
When fixed, remove the "val = 1;" and try using serial monitor to pass the "1" command to the sketch and see if it works as expected.
Fix, if necessary using method above.

If this works but it is not working with your VB program go to somewhere like http://www.vbforums.com/ to resolve the VB problem.
(When testing serial input into VB programs I use Terminal or Putty to put characters out one serial port and into the another linked to the VB program. This way you know for sure what is being fed into the VB program and you can track its reception using the in-line debugging. You could just leave the "val = 1" in the arduino sketch (after you have debugged it) so you have a constant stream into your VB program and track it using in line break points.) 
« Last Edit: April 09, 2012, 02:42:14 am by lemming » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi all..

i'm noob here..

sorry if i bother you all..

noob question here..

i was wondering how to display the adxl335 sensor data in arduino serial monitor to my label in visual basic 2010...
i need to receive the data by using bluetooth modules skkca-21...

I really2 need your help...
Somebody,please help me...
It is very urgent....


Logged

Germany
Online Online
Faraday Member
**
Karma: 59
Posts: 3072
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The answer is as above:

Try to get it working in the Serial Monitor tool of the Arduino IDE.
When that works, and you're interested in combining Arduino and VB, have a look at the above example
(Which will require some tuning, I fear)
Or experiment on your own with VB's SerialPort objects.

And better start a new thread with a meaningful title, at least if your question is about the sensor or bluetooth
Logged

Pages: [1]   Go Up
Jump to: