Pages: 1 [2] 3 4   Go Down
Author Topic: arduino+vb6  (Read 8858 times)
0 Members and 1 Guest are viewing this topic.
Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Woops! got the "<" and ">" backwards on my last post. They are not consistent with my previous code... sorry. turn them around and try it.
Logged

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

wow very awesome RPCoyle..great smiley-surprise!. i salute you all guys smiley-wink

now when i run the vb application after running the arduino i can access the switch when i press it, then the LED will flash which i find hard before on how to access it..thank you so much RPCoyle smiley...

one more lacking is to fill the color of the circle; i just use the code you posted RPCoyle then i inserted the cases..here is the code, perhaps you can see if what is the lacking of this.

Code:
Private Sub MSComm1_OnComm()

MSComm1.Settings = "9600,N,8,1"
FoundIt = False
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
  If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = n
   On Error Resume Next
   MSComm1.InputLen = 0
   MSComm1.PortOpen = True
   If MSComm1.CommID > 0 Then
     For i = 1 To 100
      Str = ""
      Str = MSComm1.Input
      If InStr(Str, "<") > 0 And InStr(Str, ">") > 0 Then GoTo jump:  ' in this case the output from Your Arduino  would be "< R >" or  "< Y >" of < G > "....
      Sleep 50
     Next i
  End If
Next n
If MSComm1.PortOpen = False Then MsgBox "Controller Not functioning , Please check USB connection", vbCritical, "": ComFail = True: Exit Sub

jump:
     '   your case statement
      Debug.Print Str;
     Select Case (Str) ' set a break point here so you can step through the case statement
   Case "A"
     Call LEDOn(vbRed)
   Case "B"
     Call LEDOn(vbGreen)
   Case "C"
     Call LEDOn(vbBlue)
   Case "D"
     Call LEDOn(vbYellow)
   Case Else
      Call LEDOff
End Sub

Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col
End Sub

Private Sub LEDOff()
End Sub
Shape1.FillColor = vbWhite
End Sub

on my arduino code, i just replace the serial.println< "R5"> into < "A" , < "B" > and so on...as what you noted also.
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

too many end subs... did this compile?
Code:
Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col
End Sub

Private Sub LEDOff()
End Sub
Shape1.FillColor = vbWhite
End Sub 


On the arduino side you need to output

Code:
    Serial.print("< ");
    Serial.print(ColorStr);       // output to computer USB port
    Serial.println(">");

On the VB side you need to get the vale back out of the string coming in from the Arduino. Then use ColorStr in your case statement
Code:
  strt = InStr(str, "<") + 1
  fin = InStr(str, ">")
  If (fin - strt) <= 0 Then
     Exit Function
  Else
      ColorStr =  Mid(str, strt, (fin - strt))
  End If

You should be able to piece this together and get it working. Get one piece working at a time. learn to set breakpoints and step through the VB code. It will help a lot also if you use lots of debug statements till you get the thing worked out
Logged

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

hello RPCoyle..sorry for this delay information i just found out when i use to compile the vb application an error message appear which is " Argument not optional" that points on the code
 
Code:
Str = ""

1. i can't understand the what does it means..

Code:
Private Sub LEDOn(col As Long)
End Sub
Shape1.FillColor = col      //in this code the error appears
End Sub

Private Sub LEDOff()
End Sub
Shape1.FillColor = vbWhite
End Sub 

2. also this, when i compile an error appear stating " Only comments may appear after End Sub, End Function, or End Property" which points the above code which i commented.

3.
Quote
learn to set breakpoints and step through the VB code

as what i understand on my research it has something to do with pausing the running mode base on your cursor pointing the code..i don't think if i can really understand it..i was so confuse. smiley-sad-blue


4. Finally, i can't find if where on the code the shape appear.. smiley-sweat

Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Private Sub LEDOn(col As Long)
End Sub                        // this is theoretically correct, but makes no sense
Shape1.FillColor = col      //in this code the error appears   , because VB was told Sub LEDOn has ended already
End Sub

I added a comment as well.
Same for Sub LEDOff below.
This was the reason for RPCoyle's comment "too many End Subs... did this compile?".

Quote
1. i can't understand the what does it means..
It's a feature of VB6 to allow lazy programming. Is
Code:
Dim Str as String
clearer to you?
the latter declares a variable and its type, RPCoyle's code implicitly declares it by filling data (An empty sring) in, just to determine its data type.
 
But besides that, you might benefit a lot by learning a bit VB6 usage ( Stepping through code, etc. )
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 hi Glennmars ... just like michael_x says... It's time you slowed down and started to learn to use the VB and Arduino IDE's

If you don't know even how to set a break point or view a debug window, then you must learn.

If what you are doing is just a one time project, then I think you might be able to work your way through it by just using examples off the internet. If you want to continue you just have to learn your code and that takes time and a hell of a lot of patience.

You already have what you need for the communication between VB and Arduino. That is kind of the hard part. The rest is learning to code in VB6

Good luck
Logged

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

Thanks guys..i appreciate your helpfulness  smiley-kitty

I can’t imagine how I passed by that code without noticing that a double end sub appears,  smiley-roll-sweat well thanks again..

RPCoyle thanks, I understand what you mean..hehehe, I just can’t fully understand those other terms in vb that’s why I keep on asking..sorry for that.!. smiley-slim

Anyways, I’m still confuse on the code you posted..just feel free to make this clear to me but if not I understand..
Hmmp, about this code:
Code:
strt = InStr(str, "<") + 1
  fin = InStr(str, ">")
  If (fin - strt) <= 0 Then
     Exit Function
  Else
      ColorStr =  Mid(str, strt, (fin - strt))
  End If

You should be able to piece this together and get it working.

do you mean that, that code is inserted  one by one or they belong to a separate places of the code then i should only determine if where this code fits.?. smiley-zipper

Finally, the code of my arduino goes like this:
Code:
Serial.println ("<Y>");
Serial.print(ColorStr);

is this right.?.or it should be like this:
Code:
Serial.print("< ");
Serial.println ("<Y>")
 Serial.print(ColorStr);       // output to computer USB port
Serial.println(">");

i was confused for that..
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is some VB6 code I put together from your code...
I added a text box as Text1 so that you would know what the pressure is

Code:
Option Explicit
' this is added to allow the sleep call
Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)


Private Sub Form_Load()
  With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 7 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True
  End With
 
End Sub

Private Sub LEDOn(col As Long)

 Shape1.BackColor = col
End Sub

Private Sub LEDOff()

 Shape1.BackColor = &HFFFFFF
End Sub


Private Sub MSComm1_OnComm()
Dim strData As String
Static strBuffer As String
Dim strWords() As String
Dim intPos As Integer
Dim boComplete As Boolean
Dim mystr As String

        Sleep 1000 ' this waits one second between readings from the Arduino
        mystr = "" ' set the string to nothing
        mystr = MSComm1.Input
     
   
            intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3) ' strip off the CR/LF
                    Case "<Y>"
                        Text1.Text = ("The pressures value is 80 psi")
                        Call LEDOn(&HFFFF&) ' this is in hex
                    Case "<B>"
                        Text1.Text = ("The pressures value is 60 psi")
                        Call LEDOn(&HFF0000) ' this is in hex
                    Case "<R>"
                        Text1.Text = ("The pressures value is 20 psi")
                        Call LEDOn(&HFF&) ' this is in hex
                    Case "<G>"
                        Text1.Text = ("The pressures value is 40 psi")
                        Call LEDOn(&HFF00&) ' this is in hex
                    Case Else
                       Call LEDOff
                End Select
         End If

End Sub

here is the Arduino code I used to send the info to the VB code, based on your code.
I eliminated some of the logic in your rev because I did not know what it did, and I wonder if you did. It seemed to have njot much to do with what you are looking for. You can put it back in is you need it.

Code:

void setup()
{               
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(7, OUTPUT);
  Serial.begin (9600);
}
void loop()
{

  int a = digitalRead (2);
  int b = digitalRead (3);
  int c = digitalRead (4);
  int d = digitalRead (5);
  //digitalWrite (7,LOW);
  {
    if (a == HIGH)
    {
      digitalWrite (9, HIGH);
      Serial.println ("<R>");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);

    }
    else if (a == LOW)
    {
      digitalWrite (9, LOW);

    }

    if (b == HIGH)
    {
      digitalWrite (10, HIGH);
      Serial.println ("<G>");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
     
    }
    else if(b==LOW)
    {
      digitalWrite (10, LOW);
     
    }

    if (c == HIGH)
    {
      digitalWrite (11, HIGH);
      Serial.println ("<B>");
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
      delay (200);
      digitalWrite (7, HIGH);
      delay (400);
      digitalWrite (7, LOW);
     
    }
    else if (c==LOW)
    {
      digitalWrite (11, LOW);
     
    }

    if (d == HIGH)
    {
      digitalWrite (12, HIGH);
      Serial.println ("<Y>");
      delay (200);
      digitalWrite (7, HIGH);

    }
    else if (d==LOW)
    {
      digitalWrite (12, LOW);
 
    }

  }
}

It works for me... it should work for you... give it a try!
Logged

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

thanks for your reply RPCoyle..how great you are smiley-wink

what you posted on the code on my arduino is same as mine too, but i was so confused about this code that's why i clarify it to you..
Code:
Serial.print(ColorStr);

Code:
Private Declare Sub Sleep Lib "kernel32" ( _
    ByVal dwMilliseconds As Long)

yes, i know this declaration, well thanks for specifying.. smiley

Code:
strt = InStr(str, "<") + 1
  fin = InStr(str, ">")
  If (fin - strt) <= 0 Then
     Exit Function
  Else
      ColorStr =  Mid(str, strt, (fin - strt))
  End If

i just want to clarify if the code above should be still inserted or this code below will do it's function:
Code:
intPos = InStr(mystr, "<")
            If intPos > 0 Then
           
                Select Case Mid(mystr, 1, 3)


thanks smiley-wink
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just use the code that I posted last... the other code snippets were examples based on some of the things I did to get a temperature reading to control a kiln
Logged

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

okay thanks RPcoyle smiley-draw

i'm so excited to try the code..heheheh, thanks again smiley-grin
Logged

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

hi again.!.

RPCoyle, your right that i should add the code you posted in the code below because when i use to debug this it never finds the comport1, (i already change the commport 7 to commport 1)..let us say that i should close the arduino first before it runs..now, i edit the code and add your code but when i debug the vb application an error prompts me that the < FoundIt does not define.> i search through the net some samples on how it was going to be declared but still i find nothing..Perhaps, you know this much will you please help me again for this.. smiley-sad-blue
Code:
With MSComm1
        If .PortOpen Then .PortOpen = False
        .CommPort = 7 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************
        .Settings = "9600,N,8,1"
        .DTREnable = True
        .RTSEnable = True
        .RThreshold = 4
        .SThreshold = 3
        .PortOpen = True

Thanks!
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which is your COM port then ?

You see/set it in the Arduino IDE, Serial Monitor, and see it when you locate your Arduino among "Printers, Devices" in your Windows Control Panel.

For e.g. COM3 you should modify that line to

        .CommPort = 3 ' *************** YOU MAY NEED TO CHANGE THIS OR ADD MY CODE TO FIND THE COM PORT************************


Alternatively you might try to understand the hint
Quote
ADD MY CODE TO FIND THE COM PORT

this refers to
Code:
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
  If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = n
  ...
in Entry 16. Can you guess what these lines are doing ?
Logged

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

hi again!..i feel sorry for the delay..

michael_x..i can see already the comport number when i use to plug the arduino it's comport 1...yet, i set the comport as a .CommPort = 1 and as what you said that i should change or add your code for me to find the comport but when i add your code it prompts an error on:
Code:
FoundIt = False          // in this code an error occur  stating that it was not being defined...
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16


Code:
' find USB port
ComFail = False
FailErr = False
For n = 1 To 16
  If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
   On Error Resume Next
   MSComm1.CommPort = n
  ...

base on my understanding, this line of codes set for finding the comport...

Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is hard to believe that your board shows up as COM 1. The computer usually has something at COM 1.

but  if the Arduino IDE says it is on COM 1 and you are able to see your output coming through over the serial monitor then it is on COM 1 and the code I gave you should work as long as the output is unclosed in "<" and ">"

If you are mixing the code I gave you with the code Glen gave you, then you might have problems, even though his code is OK. He just uses a "With" statement to streamline the code and it might be a little harder for you to figure out, even though you started out that way. I don't think you understand the fine points.


If you are just using my code, then i really have no idea what the problem might be.

Logged

Pages: 1 [2] 3 4   Go Up
Jump to: