Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do Until .... I go nuts

Status
Not open for further replies.

Alanukdude

Technical User
Nov 13, 2002
20
GB
Hi,

I've got this code below going to a servo attached to the com port...

'slowmove been dim'd as integer
slowmove = txtGripper1.Text
If txtGripper2.Text > txtGripper1.Text Then
Do Until slowmove = txtGripper2.Text
MSComm1.Output = Chr$(255)
MSComm1.Output = Chr$(4)
MSComm1.Output = Chr$(slowmove)
slowmove = slowmove + 1
Sleep 25
Loop
ElseIf txtGripper2.Text < txtGripper1.Text Then
Do Until slowmove = txtGripper2.Text
MSComm1.Output = Chr$(255)
MSComm1.Output = Chr$(4)
MSComm1.Output = Chr$(slowmove)
slowmove = slowmove - 1
Sleep 25
Loop
End If

when txtgripper2.text is greater than txtgripper1.text, this loop successfully sends a loop of the 3 lines above to the servo. incrementing 1 step at a time untill it equals txtgripper2. problem is when txtgripper2.text is less than txtgripper1.text. This should send the 3 lines to the com port with the 3rd one incremeting negatively to the same value as txtgripper2.text. But it don't it goes up and up until the servo reaches 254 where the program crashes.

txtgripper1 is 127 and this value is sent to the servo before this loop.

any thoughts on this as it is really getting to me.
 
you could simplify the loops a little bit by setting a variable that is your increment/decrement.

Code:
slowmove = txtGripper1.Text
If txtGripper2.Text > txtGripper1.Text Then 
    intChange = 1
else
    intchange = -1
end if
Do Until slowmove = txtGripper2.Text
    MSComm1.Output = Chr$(255)
    MSComm1.Output = Chr$(4)
    MSComm1.Output = Chr$(slowmove)
    slowmove = slowmove + intchange
    Sleep 25
Loop

This might help a little bit, but I can't see any reason why your code wouldn't work.

You could try putting a break point in the loop and watch the variables change?

Matt

If you can keep your head while those around you are losing theirs, you obviously haven't grasped the seriousness of the situation
 
Make sure that your are comparing numbers and not strings.

Code:
txtGripper1.Text = 2
txtGripper2.Text = 10 

txtGripper2.Text > txtGripper1.Text is false

CInt(txtGripper2.Text) > CInt(txtGripper1.Text) is true



zemp
 
Try this....

slowmove = val(txtGripper1.Text)
If val(txtGripper2.Text) > val(txtGripper1.Text) Then
Do Until slowmove = val(txtGripper2.Text)
MSComm1.Output = Chr$(255)
MSComm1.Output = Chr$(4)
MSComm1.Output = Chr$(slowmove)
slowmove = slowmove + 1
Sleep 25
Loop
ElseIf val(txtGripper2.Text) < val(txtGripper1.Text) Then
Do Until slowmove = val(txtGripper2.Text)
MSComm1.Output = Chr$(255)
MSComm1.Output = Chr$(4)
MSComm1.Output = Chr$(slowmove)
slowmove = slowmove - 1
Sleep 25
Loop
End If


Nick
 
sorry zemp thats what i was saying and you posting while i was writing..i apologise!
 
Forri... thank you. adding val worked. as zemp mentioned my code must be comparing strings rather than numbers.

thank you everyone.

right, i better get cracking. lots of code to write.
 
Forri, no worries, happens all the time.

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top