Prerequisites
This project assumes you are familiar with the following
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 ChessClock {
//Define
static boolean redClock = false;
static boolean greenClock = false;
static boolean playingChess = true;
static int redPlayerTime = 5 * 60; //5 minutes
static int greenPlayerTime = 5 * 60; //5 minutes
public static void main(String[] args) throws Exception{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.setIsHubPortDevice(true);
redButton.setHubPort(0);
greenButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
//Event
redButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
greenClock = true;
redClock = false;
}
}
});
greenButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
redClock = true;
greenClock = false;
}
}
});
//Open
redButton.open(1000);
greenButton.open(1000);
while(playingChess){
if(redClock){
redPlayerTime -= 1;
}
else if(greenClock){
greenPlayerTime -= 1;
}
System.out.println("Red Time: " + redPlayerTime + " Green Time: " + greenPlayerTime);
if(redPlayerTime == 0 || greenPlayerTime == 0){
playingChess = false;
}
Thread.sleep(1000);
}
System.out.println("Player over time limit");
}
}
package chessclock;
import com.phidget22.*;
public class ChessClock {
//Define
static boolean redClock = false;
static boolean greenClock = false;
static boolean playingChess = true;
static int redPlayerTime = 5 * 60; //5 minutes
static int greenPlayerTime = 5 * 60; //5 minutes
public static void main(String[] args) throws Exception{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.setIsHubPortDevice(true);
redButton.setHubPort(0);
greenButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
//Event
redButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
greenClock = true;
redClock = false;
}
}
});
greenButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
redClock = true;
greenClock = false;
}
}
});
//Open
redButton.open(1000);
greenButton.open(1000);
while(playingChess){
if(redClock){
redPlayerTime -= 1;
}
else if(greenClock){
greenPlayerTime -= 1;
}
System.out.println("Red Time: " + redPlayerTime + " Green Time: " + greenPlayerTime);
if(redPlayerTime == 0 || greenPlayerTime == 0){
playingChess = false;
}
Thread.sleep(1000);
}
System.out.println("Player over time limit");
}
}
//Add Phidgets Library
import com.phidget22.*;
//Define
DigitalInput redButton;
DigitalInput greenButton;
boolean redClock = false;
boolean greenClock = false;
int redPlayerTime = 5 * 60; // 5 minutes
int greenPlayerTime = 5 * 60; //5 minutes
void setup(){
try{
//Create
redButton = new DigitalInput();
greenButton = new DigitalInput();
//Address
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
//Event
redButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
redClock = false;
greenClock = true;
}
}
});
//Event
greenButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
if(e.getState()){
greenClock = false;
redClock = true;
}
}
});
//Open
redButton.open(1000);
greenButton.open(1000);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
if(redClock){
redPlayerTime -= 1;
}
else if(greenClock){
greenPlayerTime -= 1;
}
println("Red Time: " + redPlayerTime + " Green Time: " + greenPlayerTime);
if(redPlayerTime == 0 || greenPlayerTime == 0){
println("Player over time limit");
exit();
}
delay(1000);
}
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 *
from Phidget22.Devices.DigitalOutput import *
#Required for sleep statement
import time
#Event
def onRedButton_StateChange(self, state):
global greenClock
global redClock
#if button pressed
if(state):
greenClock = True
redClock = False
#Event
def onGreenButton_StateChange(self, state):
global greenClock
global redClock
#if button pressed
if(state):
redClock = True
greenClock = False
#Define
redPlayerTime = 5 * 60 #5 minutes
greenPlayerTime = 5 * 60 #5 minutes
redClock = False
greenClock = False
playingChess = True
#Create
redButton = DigitalInput()
greenButton = DigitalInput()
#Address
redButton.setHubPort(0)
redButton.setIsHubPortDevice(True)
greenButton.setHubPort(5)
greenButton.setIsHubPortDevice(True)
#Subscribe to Events
redButton.setOnStateChangeHandler(onRedButton_StateChange)
greenButton.setOnStateChangeHandler(onGreenButton_StateChange)
#Open
redButton.openWaitForAttachment(1000)
greenButton.openWaitForAttachment(1000)
while(playingChess):
if(redClock):
redPlayerTime -= 1
elif (greenClock):
greenPlayerTime -= 1
print("Red Time: " + str(redPlayerTime) + " Green Time: " + str(greenPlayerTime))
if(redPlayerTime == 0 or greenPlayerTime == 0):
playingChess = False
time.sleep(1)
print("Player over time limit")
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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Phidget22;
namespace ChessClock
{
internal class Program
{
static bool redClock = false;
static bool greenClock = false;
static bool playingChess = true;
static int redPlayerTime = 5 * 60; //5 minutes
static int greenPlayerTime = 5 * 60; //5 minutes
//Event
private static void RedButton_StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
{
if (e.State)
{
redClock = false;
greenClock = true;
}
}
private static void GreenButton_StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
{
if (e.State)
{
greenClock = false;
redClock = true;
}
}
static void Main(string[] args)
{
//Create
DigitalInput redButton = new DigitalInput();
DigitalInput greenButton = new DigitalInput();
//Address
redButton.IsHubPortDevice = true;
redButton.HubPort = 0;
greenButton.IsHubPortDevice = true;
greenButton.HubPort = 5;
//Subscribe to Events
redButton.StateChange += RedButton_StateChange;
greenButton.StateChange += GreenButton_StateChange;
//Open
redButton.Open(1000);
greenButton.Open(1000);
while (playingChess)
{
if (redClock)
{
redPlayerTime -= 1;
}
else if (greenClock)
{
greenPlayerTime -= 1;
}
System.Console.WriteLine("Red Time: " + redPlayerTime + " Green Time: " + greenPlayerTime);
if(redPlayerTime == 0 || greenPlayerTime == 0)
{
playingChess = false;
}
System.Threading.Thread.Sleep(1000);
}
System.Console.WriteLine("Player over time limit");
}
}
}
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
Code not available.
Run Your Program
The clocks will begin when a button is pressed. When the time runs out, the users will be notified.
Practice
- Try adding more complex time controls. For more information, view this Wikipedia article.
- Try counting the number of moves a player has made.
- Try adding a graphical user interface (GUI) to your project.