A Basic Phidget Program

From Phidgets Support
Revision as of 20:24, 9 April 2019 by Mparadis (talk | contribs)
 Phidget Programming Basics: A Basic Phidget ProgramTOC Icon.png Table of Contents

Nav Back Arrow.png Nav Back Hover.png WhiteTab1.png HoverTab1.jpg WhiteTab2.png HoverTab2.jpg WhiteTab3.png HoverTab3.jpg WhiteTab4.png HoverTab4.jpg WhiteTab5.png HoverTab5.jpg WhiteTab6.png HoverTab6.jpg WhiteTab7.png HoverTab7.jpg WhiteTab8.png HoverTab8.jpg WhiteTab9.png HoverTab9.jpg WhiteTab10.png HoverTab10.jpg GreenTab11.png WhiteTab12.png HoverTab12.jpg WhiteTab13.png HoverTab13.jpg WhiteTab14.png HoverTab14.jpg WhiteTab15.png HoverTab15.jpg WhiteTab16.png HoverTab16.jpg Nav Next Arrow.png Nav Next Hover.png


11 . A Basic Phidget Program

Putting all this information together may seem a bit daunting at first, but the end result can actually be quite simple.

For example, the following program will read the state of a button for a time, as seen in the flowchart to the right:

from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
import time

def onAttachHandler(self):
    print("Phidget Attached!")

def onStateChangeHandler(self, state):
    print("State %f" % state)

def main():
    ch = DigitalInput()

    #Set Any Addressing Parameters Here

    ch.setOnAttachHandler(onAttachHandler)
    ch.setOnStateChangeHandler(onStateChangeHandler)

    ch.openWaitForAttachment(5000)

    # Do stuff with your Phidgets here.
    time.sleep(10)

    ch.close()

main()


Simple Phidget Program Flowchart.png

Click Flowchart to Enlarge