Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Tk Buttons

by PilotinControl (Pilgrim)
on Nov 14, 2015 at 13:38 UTC ( [id://1147700]=note: print w/replies, xml ) Need Help??


in reply to Re: Tk Buttons
in thread Tk Buttons

The question is right here: Why is it that when you press that button again it starts counting where it left off when it should be reset to 0 to start counting by 10s again. Thanks.

Replies are listed 'Best First'.
Re^3: Tk Buttons
by Athanasius (Archbishop) on Nov 14, 2015 at 13:51 UTC

      It has been figured out....I needed to add another Bind function in order to set the opposite button back to speed stop 0....otherwise when pressed again it would continue onto the next sub routine in line

        That's weird. Why are you changing the binding? You should just change the speed. Here's how I understood your description:
        #!/usr/bin/perl use warnings; use strict; use Tk; { my $speed = 0; sub change_speed { my $diff = shift; my $state = $speed <=> 0; if (! $state || $state == ($diff <=> 0)) { $speed += $diff if abs($speed + $diff) <= 100; } else { $speed = 0; } } sub create_label { my $parent = shift; return $parent->Label(-textvariable => \$speed, -relief => 'sunken', )->pack(-ipadx => 5) } } my $mw = 'MainWindow'->new(-title => 'Motor'); my $mf = $mw->Frame->pack; $mf->Label(-text => 'Tk Motor Controller')->pack; my $l = create_label($mf); my $bf = $mf->Frame->pack; $bf->Button(-text => '<', -command => [ \&change_speed, -10 ], )->pack(-side => 'left'); $bf->Button(-text => '>', -command => [ \&change_speed, 10 ], )->pack(-side => 'right'); my $qb = $mf->Button(-text => 'Quit', -underline => 0, -command => sub { Tk::exit() }, )->pack( -pady => 10); $mw->bind('<Alt-q>', sub { $qb->invoke }); MainLoop();

        Update: Used closure over the $speed variable.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: Tk Buttons
by PilotinControl (Pilgrim) on Nov 14, 2015 at 13:58 UTC

    And to further explain, the buttons, when pressed control the speed and direction of a motor connected to the laptop via a usb cable using the Win32::SerialPort module....that part works. What is happening when the direction button is pressed it gets faster in the forward direction and when pressed again it gets faster and faster...and when the reverse button is pressed the motor stops and proceeds to going in the reverse direction and each time that button is pressed it gets faster and faster....here is what is happening....if the forward button is pressed again....instead of starting at speed 0....it picks up where the last forward button press left off...lets say it stopped at 40 when we first pressed the reverse button....when the forward button is pressed again it goes to speed 50 instead of starting back at zero....same holds true if the reverse button was pressed again.

      The info regarding the controlling of the motor and use of Win32::SerialPort is completely irrelevant to the problem you're asking about.

      As has already been mentioned, your posted code does not include anything that would change/rest the value. I will repeat the request that Athanasius made. Please post a short but complete script that demonstrates the problem you're having with the setting/resetting of the values so that we can run some tests. With that info we should be able to point out problem and solution. It would also be helpful if you would use proper code formatting so that your code is more readable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147700]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found