Read Button
In this lesson, you’ll learn how to write a program that gets input from a button!




Write Code (Java)
Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.
Not your programming language? Set your preferences so we can display relevant code examples
Write Code (Python)
Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.
Not your programming language? Set your preferences so we can display relevant code examples
Write Code (C#)
Copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.
Not your programming language? Set your preferences so we can display relevant code examples
Write Code (Swift)
Create a label in your window and copy the code below into the project you created. If you don't have a project or forgot how to create one, revisit the Configure section.
Not your programming language? Set your preferences so we can display relevant code examples
Create a label
Run Your Program
When you press the red button, you will see the button state change.
Practice
- Modify the loop to repeat every 10ms, 1000ms, 5000ms. Note how the behavior of your program changes with the different times.
- Modify your code to print the state of your green button (Hint: Address).
- Modify your code to only print when the button state changes.
Practice
- Modify the loop to repeat every 10ms, 1000ms, 5000ms. Note how the behavior of your program changes with the different times.
- Modify your code to print the state of your green button (Hint: Address).
- Modify your code to only print when the button state changes.
Practice
- Modify the loop to repeat every 10ms, 1000ms, 5000ms. Note how the behavior of your program changes with the different times.
- Modify your code to print the state of your green button (Hint: Address).
- Modify your code to only print when the button state changes.
Practice
- Modify your program to change the label to “Pressed” and “Released” instead of true and false.
- Modify your code to print the state of your green button (Hint: Address).
Troubleshoot
I am getting a "Timed Out" exception.
- Make sure the USB cable from your VINT Hub to your computer is attached properly.
- Make sure you are connected to the correct port on your VINT Hub. In this case your red button should be connected to port 0.
- Make sure no other program is running that uses Phidgets. If a Phidget is already in use in another program, it will be busy and won't respond to this one.
The button is not working properly.
- Check the button assembly. If the switch and button are not assembled correctly your program will not work properly. The plastic plunger should align with the activator as shown below.
Still having issues?
Visit the Advanced Troubleshooting Page or contact us (education@phidgets.com).