Setup
All you need for this project is the Getting Started Kit.
Write code (Java)
Copy the code below into a new Java project. If you need a reminder of how to do this, revisit the Getting Started Course.
Not your programming language? Set your preferences so we can display relevant code examples
import com.phidget22.*;
public class True {
public static void main(String[] args) throws Exception{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
//Open
redButton.open(1000);
greenButton.open(1000);
//Questions/answers
String[] questions = {"2 + 2 = 4","9 + 6 = 16"};
boolean[] answers = {true, false};
//Work through questions one by one
for(int i = 0; i < questions.length; i++){
System.out.println(questions[i]);
//tells the while loop when to stop polling buttons
boolean answered = false;
while(!answered){
if(redButton.getState()){
if(answers[i] == false){ //red is always false
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
answered = true;
}
else if(greenButton.getState()){ //green is always true
if(answers[i] == true){
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
answered = true;
}
Thread.sleep(150);
}
}
}
}
package pkgtrue;
import com.phidget22.*;
public class True {
public static void main(String[] args) throws Exception{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
//Open
redButton.open(1000);
greenButton.open(1000);
//Questions/answers
String[] questions = {"2 + 2 = 4","9 + 6 = 16"};
boolean[] answers = {true, false};
//Work through questions one by one
for(int i = 0; i < questions.length; i++){
System.out.println(questions[i]);
//tells the while loop when to stop polling buttons
boolean answered = false;
while(!answered){
if(redButton.getState()){
if(answers[i] == false){ //red is always false
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
answered = true;
}
else if(greenButton.getState()){ //green is always true
if(answers[i] == true){
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
answered = true;
}
Thread.sleep(150);
}
}
}
}
//Add Phidgets Library
import com.phidget22.*;
//Define
DigitalInput redButton;
DigitalInput greenButton;
//Questions/answers
String[] questions = {"2 + 2 = 4\n","9 + 6 = 16\n"};
boolean[] answers = {true, false};
boolean answered = false;
int i = 0;
void setup(){
try{
//Create
redButton = new DigitalInput();
greenButton = new DigitalInput();
//Address
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
//Open
redButton.open(1000);
greenButton.open(1000);
}catch(Exception e){
//Handle Exceptions
e.printStackTrace();
}
}
void draw(){
try{
if(i < questions.length){
if(i == 0 || answered){
print(questions[i]);
i += 1;
answered = false;
}
}
if(redButton.getState() && !answered){
if(answers[i-1] == false){
print("Correct\n");
}
else{
print("Incorrect\n");
}
answered = true;
}
else if(greenButton.getState() && !answered){
if(answers[i-1] == true){
print("Correct\n");
}
else{
print("Incorrect\n");
}
answered = true;
}
delay(150);
}catch(Exception e){
//Handle Exceptions
e.printStackTrace();
}
}
Write code (Python)
Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course.
Not your programming language? Set your preferences so we can display relevant code examples
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.DigitalInput import *
#Required for sleep statement
import time
#Create
redButton = DigitalInput()
greenButton = DigitalInput()
#Address
redButton.setHubPort(0)
redButton.setIsHubPortDevice(True)
greenButton.setHubPort(5)
greenButton.setIsHubPortDevice(True)
#Open
redButton.openWaitForAttachment(1000)
greenButton.openWaitForAttachment(1000)
#Questions/Answers
questions = ["2 + 2 = 4","9 + 6 = 16"]
answers = [True, False]
#Work through questions one by one
for i in range(0, len(questions)):
print(questions[i])
#tells the while loop when to stop polling buttons
answered = False
while(not answered):
if(redButton.getState()):
if(answers[i] == False): #red is always false
print("Correct")
else:
print("Incorrect")
answered = True
elif(greenButton.getState()):
if(answers[i] == True): #green is always true
print("Correct")
else:
print("Incorrect")
answered = True
time.sleep(0.15)
Write code (C#)
Copy the code below into a new C# project. If you need a reminder of how to do this, revisit the Getting Started Course.
Not your programming language? Set your preferences so we can display relevant code examples
using System;
using Phidget22;
namespace True
{
class Program
{
static void Main(string[] args)
{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.HubPort = 0;
redButton.IsHubPortDevice = true;
greenButton.HubPort = 5;
greenButton.IsHubPortDevice = true;
//Open
redButton.Open(1000);
greenButton.Open(1000);
//Questions/answers
String[] questions = { "2 + 2 = 4", "9 + 6 = 16" };
bool[] answers = { true, false };
//Work through questions one by one
for(int i = 0; i < questions.Length; i++)
{
Console.WriteLine(questions[i]);
//tells the while loop when to stop polling buttons
bool answered = false;
while (!answered)
{
if (redButton.State)
{
if(answers[i] == false) //red is always false
{
Console.WriteLine("Correct");
}
else
{
Console.WriteLine("Incorrect");
}
answered = true;
}
else if (greenButton.State)
{
if (answers[i] == true) //green is always true
{
Console.WriteLine("Correct");
}
else
{
Console.WriteLine("Incorrect");
}
answered = true;
}
System.Threading.Thread.Sleep(150);
}
}
}
}
}
Write code (Swift)
Copy the code below into a new Swift project. If you need a reminder of how to do this, revisit the Getting Started Course.
Not your programming language? Set your preferences so we can display relevant code examples
Coming Soon!
Run your program. You will see a question printed to your screen. Use the green button to answer true and the red button to answer false.
Practice
- Using a notepad or word processor, explain how the code above works.
- Modify the code so there is a total of 10 questions.
- Log the users score as they progress through the list of questions. At the end of the game, display the total score.
- Use button events instead of polling to improve the functionality of your game.