Setup
All you need for this project is the Getting Started Kit.
What Is Morse Code?
Morse Code is a method that is used to encode text as a sequence of signals. These signals are called dots and dashes (sometimes dits and dahs), and originally, they were sent as pulses of electrical current across wires.
International Morse Code (shown below) is the most common encoding. Each character has a specific sequence of dots and dashes, and there are rules governing timing between dots, dashes, letters and words.
Aside from electrical pulses, Morse Code can be sent in a variety of ways (sound, light, noise, etc.). In this lesson, you will use your LEDs and push buttons to transmit and receive Morse Code.
Morse Code Encoder
You will start with a Morse Code Encoder. The job of the encoder is to take text and convert it into Morse Code.
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
//Add Phidgets Library
import com.phidget22.*;
import java.util.Scanner;
public class MorseEncoder {
//Define
static DigitalOutput greenLED;
//Define delay
static int delay = 250;
//Handle Exceptions
public static void main(String[] args) throws Exception {
//Create
Scanner scan = new Scanner(System.in);
greenLED = new DigitalOutput();
//Address
greenLED.setHubPort(4);
greenLED.setIsHubPortDevice(true);
//Open
greenLED.open(1000);
//Use your Phidgets
System.out.println("Enter the word you would like to encode:");
String userInput = scan.nextLine();
String word = userInput.toLowerCase();
System.out.print("Translating... Look at the green LED." + "\n");
Thread.sleep(2000);
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a') {
A();
System.out.print(" ");
} else if (word.charAt(i) == 'b') {
B();
System.out.print(" ");
}
if (word.charAt(i) == ' ')//7 units of delay between words
{
Thread.sleep(delay * 7);
System.out.print("/ ");
} else {
Thread.sleep(delay * 3);//3 units between letters
}
}
}
//Flash LED corresponding to dot
public static void dot() throws Exception {
System.out.print(".");
greenLED.setState(true);
Thread.sleep(delay);
greenLED.setState(false);
Thread.sleep(delay);
}
//Flash LED corresponding to dash
public static void dash() throws Exception {
System.out.print("-");
greenLED.setState(true);
Thread.sleep(delay * 3);
greenLED.setState(false);
Thread.sleep(delay);
}
//Assigns Morse code to letters with appropriate delay
public static void A() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void B() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
}
package morseencoder;
//Add Phidgets Library
import com.phidget22.*;
import java.util.Scanner;
public class MorseEncoder {
//Define
static DigitalOutput greenLED;
//Define delay
static int delay = 250;
//Handle Exceptions
public static void main(String[] args) throws Exception {
//Create
Scanner scan = new Scanner(System.in);
greenLED = new DigitalOutput();
//Address
greenLED.setHubPort(4);
greenLED.setIsHubPortDevice(true);
//Open
greenLED.open(1000);
//Use your Phidgets
System.out.println("Enter the word you would like to encode:");
String userInput = scan.nextLine();
String word = userInput.toLowerCase();
System.out.print("Translating... Look at the green LED." + "\n");
Thread.sleep(2000);
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a') {
A();
System.out.print(" ");
} else if (word.charAt(i) == 'b') {
B();
System.out.print(" ");
} else if (word.charAt(i) == 'c') {
C();
System.out.print(" ");
} else if (word.charAt(i) == 'd') {
D();
System.out.print(" ");
} else if (word.charAt(i) == 'e') {
E();
System.out.print(" ");
} else if (word.charAt(i) == 'f') {
F();
System.out.print(" ");
} else if (word.charAt(i) == 'g') {
G();
System.out.print(" ");
} else if (word.charAt(i) == 'h') {
H();
System.out.print(" ");
} else if (word.charAt(i) == 'i') {
I();
System.out.print(" ");
} else if (word.charAt(i) == 'j') {
J();
System.out.print(" ");
} else if (word.charAt(i) == 'k') {
K();
System.out.print(" ");
} else if (word.charAt(i) == 'l') {
L();
System.out.print(" ");
} else if (word.charAt(i) == 'm') {
M();
System.out.print(" ");
} else if (word.charAt(i) == 'n') {
N();
System.out.print(" ");
} else if (word.charAt(i) == 'o') {
O();
System.out.print(" ");
} else if (word.charAt(i) == 'p') {
P();
System.out.print(" ");
} else if (word.charAt(i) == 'q') {
Q();
System.out.print(" ");
} else if (word.charAt(i) == 'r') {
R();
System.out.print(" ");
} else if (word.charAt(i) == 's') {
S();
System.out.print(" ");
} else if (word.charAt(i) == 't') {
T();
System.out.print(" ");
} else if (word.charAt(i) == 'u') {
U();
System.out.print(" ");
} else if (word.charAt(i) == 'v') {
V();
System.out.print(" ");
} else if (word.charAt(i) == 'w') {
W();
System.out.print(" ");
} else if (word.charAt(i) == 'x') {
X();
System.out.print(" ");
} else if (word.charAt(i) == 'y') {
Y();
System.out.print(" ");
} else if (word.charAt(i) == 'z') {
Z();
System.out.print(" ");
}
if (word.charAt(i) == ' ')//7 units of delay between words
{
Thread.sleep(delay * 7);
System.out.print("/ ");
} else {
Thread.sleep(delay * 3);//3 units between letters
}
}
}
//Flash LED corresponding to dot
public static void dot() throws Exception {
System.out.print(".");
greenLED.setState(true);
Thread.sleep(delay);
greenLED.setState(false);
Thread.sleep(delay);
}
//Flash LED corresponding to dash
public static void dash() throws Exception {
System.out.print("-");
greenLED.setState(true);
Thread.sleep(delay * 3);
greenLED.setState(false);
Thread.sleep(delay);
}
//Assigns Morse code to letters with appropriate delay
public static void A() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void B() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void C() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void D() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void E() throws Exception {
dot();
Thread.sleep(delay);
}
public static void F() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void G() throws Exception {
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void H() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void I() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void J() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void K() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void L() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void M() throws Exception {
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void N() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void O() throws Exception {
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void P() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
}
public static void Q() throws Exception {
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void R() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void S() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
public static void T() throws Exception {
dash();
Thread.sleep(delay);
}
public static void U() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void V() throws Exception {
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void W() throws Exception {
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void X() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void Y() throws Exception {
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
}
public static void Z() throws Exception {
dash();
Thread.sleep(delay);
dash();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
dot();
Thread.sleep(delay);
}
}
//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 *
from Phidget22.Devices.DigitalOutput import *
#Required for sleep statement
import time
#Define delay (seconds)
delay = 0.25
#Flash LED corresponding to a dot
def dot():
print(".", end = ' ')
greenLED.setState(True)
time.sleep(delay)
greenLED.setState(False)
time.sleep(delay)
#Flash LED corresponding to a dash
def dash():
print("-", end = ' ')
greenLED.setState(True)
time.sleep(delay * 3)
greenLED.setState(False)
time.sleep(delay)
#Assigns Morse code to letters with appropriate delay
def A():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def B():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def C():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
def D():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def E():
dot()
time.sleep(delay)
def F():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
def G():
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
def H():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def I():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def J():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
def K():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def L():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def M():
dash()
time.sleep(delay)
dash()
time.sleep(delay)
def N():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
def O():
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
def P():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
def Q():
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def R():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
def S():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
def T():
dash()
time.sleep(delay)
def U():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def V():
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def W():
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
def X():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
def Y():
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dash()
time.sleep(delay)
dash()
time.sleep(delay)
def Z():
dash()
time.sleep(delay)
dash()
time.sleep(delay)
dot()
time.sleep(delay)
dot()
time.sleep(delay)
#Create
greenLED = DigitalOutput()
#Address
greenLED.setHubPort(4)
greenLED.setIsHubPortDevice(True)
#Open
greenLED.openWaitForAttachment(1000)
#Use your Phidgets
#Asks user for word to encode, puts into lowercase to help case sensitivity.
userInput = input("Enter the word you would like to encode: ")
word = userInput.lower()
print("Translating... Look at the green LED." + "\n")
time.sleep(2)
#Loop that goes through the word and encodes it.
for i in range(len(word)):
if (word[i] == 'a'):
A()
print(" ", end = '')
elif (word[i] == 'b'):
B()
print(" ", end = '')
elif (word[i] == 'c'):
C()
print(" ", end = '')
elif (word[i] == 'd'):
D()
print(" ", end = '')
elif (word[i] == 'e'):
E()
print(" ", end = '')
elif (word[i] == 'f'):
F()
print(" ", end = '')
elif (word[i] == 'g'):
G()
print(" ", end = '')
elif (word[i] == 'h'):
H()
print(" ", end = '')
elif (word[i] == 'i'):
I()
print(" ", end = '')
elif (word[i] == 'j'):
J()
print(" ", end = '')
elif (word[i] == 'k'):
K()
print(" ", end = '')
elif (word[i] == 'l'):
L()
print(" ", end = '')
elif (word[i] == 'm'):
M()
print(" ", end = '')
elif (word[i] == 'n'):
N()
print(" ", end = '')
elif (word[i] == 'o'):
O()
print(" ", end = '')
elif (word[i] == 'p'):
P()
print(" ", end = '')
elif (word[i] == 'q'):
Q()
print(" ", end = '')
elif (word[i] == 'r'):
R()
print(" ", end = '')
elif (word[i] == 's'):
S()
print(" ", end = '')
elif (word[i] == 't'):
T()
print(" ", end = '')
elif (word[i] == 'u'):
U()
print(" ", end = '')
elif (word[i] == 'v'):
V()
print(" ", end = '')
elif (word[i] == 'w'):
W()
print(" ", end = '')
elif (word[i] == 'x'):
X()
print(" ", end = '')
elif (word[i] == 'y'):
Y()
print(" ", end = '')
elif (word[i] == 'z'):
Z()
print(" ", end = '')
if (word[i] == ' '):
time.sleep(delay * 7) #7 units of delay between words
print("/ ", end = '')
else:
time.sleep(delay * 3) #3 units between letters
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
//Add Phidgets Library
using Phidget22;
namespace MorseEncoder
{
class MorseCode
{
//Define
static DigitalOutput greenLED;
//Define delay (milliseconds)
static int delay = 250;
static void Main(string[] args)
{
//Create
greenLED = new DigitalOutput();
//Address
greenLED.HubPort = 4;
greenLED.IsHubPortDevice = true;
//Open
greenLED.Open(1000);
//Use your Phidgets
System.Console.WriteLine("Enter the word you would like to encode:");
string userInput = System.Console.ReadLine();
string word = userInput.ToLower();
System.Console.WriteLine("Translating... Look at the green LED." + "\n");
System.Threading.Thread.Sleep(2000);
//Looks through user unput and translates it to morse code.
for (int i = 0; i < word.Length; i++)
{
if (word[i] == 'a')
{
A();
System.Console.Write(" ");
}
else if (word[i] == 'b')
{
B();
System.Console.Write(" ");
}
else if (word[i] == 'c')
{
C();
System.Console.Write(" ");
}
else if (word[i] == 'd')
{
D();
System.Console.Write(" ");
}
else if (word[i] == 'e')
{
E();
System.Console.Write(" ");
}
else if (word[i] == 'f')
{
F();
System.Console.Write(" ");
}
else if (word[i] == 'g')
{
G();
System.Console.Write(" ");
}
else if (word[i] == 'h')
{
H();
System.Console.Write(" ");
}
else if (word[i] == 'i')
{
I();
System.Console.Write(" ");
}
else if (word[i] == 'j')
{
J();
System.Console.Write(" ");
}
else if (word[i] == 'k')
{
K();
System.Console.Write(" ");
}
else if (word[i] == 'l')
{
L();
System.Console.Write(" ");
}
else if (word[i] == 'm')
{
M();
System.Console.Write(" ");
}
else if (word[i] == 'n')
{
N();
System.Console.Write(" ");
}
else if (word[i] == 'o')
{
O();
System.Console.Write(" ");
}
else if (word[i] == 'p')
{
P();
System.Console.Write(" ");
}
else if (word[i] == 'q')
{
Q();
System.Console.Write(" ");
}
else if (word[i] == 'r')
{
R();
System.Console.Write(" ");
}
else if (word[i] == 's')
{
S();
System.Console.Write(" ");
}
else if (word[i] == 't')
{
T();
System.Console.Write(" ");
}
else if (word[i] == 'u')
{
U();
System.Console.Write(" ");
}
else if (word[i] == 'v')
{
V();
System.Console.Write(" ");
}
else if (word[i] == 'w')
{
W();
System.Console.Write(" ");
}
else if (word[i] == 'x')
{
X();
System.Console.Write(" ");
}
else if (word[i] == 'y')
{
Y();
System.Console.Write(" ");
}
else if (word[i] == 'z')
{
Z();
System.Console.Write(" ");
}
if (word[i] == ' ')
{//7 units of delay between words
System.Threading.Thread.Sleep(delay * 7);
System.Console.Write("/ ");
}
else
{
System.Threading.Thread.Sleep(delay * 3);
}
}
}
//Flash LED corresponding to dot
public static void dot()
{ //Light instruction for a dot of Morse code
System.Console.Write(".");
greenLED.State = true;
System.Threading.Thread.Sleep(delay);
greenLED.State = false;
System.Threading.Thread.Sleep(delay);
}
//Flash LED corresponding to dash
public static void dash()
{ //Light instruction for a dash of Morse code
System.Console.Write("-");
greenLED.State = true;
System.Threading.Thread.Sleep(delay * 3);
greenLED.State = false;
System.Threading.Thread.Sleep(delay);
}
//Assigns Morse Code to the letters with the delay needed.
public static void A()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void B()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void C()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void D()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void E()
{
dot();
System.Threading.Thread.Sleep(delay);
}
public static void F()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void G()
{
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void H()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void I()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void J()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void K()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void L()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void M()
{
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void N()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void O()
{
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void P()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
}
public static void Q()
{
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void R()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void S()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
public static void T()
{
dash();
System.Threading.Thread.Sleep(delay);
}
public static void U()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void V()
{
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void W()
{
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void X()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void Y()
{
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
}
public static void Z()
{
dash();
System.Threading.Thread.Sleep(delay);
dash();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
dot();
System.Threading.Thread.Sleep(delay);
}
}
}
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
When you enter a word into your program, you will see the result of the encoding via the LED.
Morse Code Decoder
Next, you will create a Morse Code Decoder. The job of the decoder is to take input and convert it into Morse Code.
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
//Add Phidgets Library
import com.phidget22.*;
public class MorseDecoder {
//Used to store times
static long startPress;
static double timePressed;
//Used to store input
static String userInput = "";
//Convert user input from morse code to characters
public static char Decoder(String code) {
String[] morseCode = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"Unknown Code"};
int i = 0;
while (morseCode[i].equals("Unknown Code") == false) {
if (code.equals(morseCode[i])) {
return Character.toUpperCase((char) (97 + i)); //Character 'a' starts at 97.
}
i++;
}
return '?'; //unknown code
}
//Handle Exceptions
public static void main(String[] args) throws Exception {
//Create
DigitalInput redButton = new DigitalInput();
DigitalOutput redLED = new DigitalOutput();
DigitalInput greenButton = new DigitalInput();
DigitalOutput greenLED = new DigitalOutput();
//Address
redLED.setHubPort(1);
redLED.setIsHubPortDevice(true);
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
greenLED.setHubPort(4);
greenLED.setIsHubPortDevice(true);
//Event
redButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
try {
redLED.setState(e.getState()); //Turn on red LED
if (e.getState()) { //Once red button is pushed, Decoder method is called for the users input.
System.out.print(" = ");
System.out.print(Decoder(userInput));
System.out.println();
userInput = ""; //Clears userInput for next code
}
} catch (PhidgetException ex) {
System.out.println("Failure: " + ex);
}
}
});
//Event
greenButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
try {
greenLED.setState(e.getState()); //Turn on green LED
if (e.getState()) {
startPress = System.currentTimeMillis(); //Grabs time when the button is first pressed
} else {
timePressed = ((double) (System.currentTimeMillis() - startPress));//Grabs time when button released, subtracts start time.
if ((timePressed > 100) && (timePressed < 600)) {
userInput += ".";
System.out.print("."); //Prints a dot to the screen
} else if (timePressed > 600) {
userInput += "-";
System.out.print("-"); //Prints a dash to the screen
}
}
} catch (PhidgetException ex) {
System.out.println("Failure: " + ex);
}
}
});
//Open
redLED.open(1000);
greenLED.open(1000);
redButton.open(1000);
greenButton.open(1000);
//Use your Phidgets
System.out.println(
"Start entering into the decoder." + "\n" + "Press the green button for a dot, hold for a dash." + "\n"
+ "Press the red button after you enter a letter.");
userInput = "";
//Wait for input
while (true) {
Thread.sleep(10);
}
}
}
package morsedecoder;
//Add Phidgets Library
import com.phidget22.*;
public class MorseDecoder {
//Used to store times
static long startPress;
static double timePressed;
//Used to store input
static String userInput = "";
//Convert user input from morse code to characters
public static char Decoder(String code) {
String[] morseCode = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"Unknown Code"};
int i = 0;
while (morseCode[i].equals("Unknown Code") == false) {
if (code.equals(morseCode[i])) {
return Character.toUpperCase((char) (97 + i)); //Character 'a' starts at 97.
}
i++;
}
return '?'; //unknown code
}
//Handle Exceptions
public static void main(String[] args) throws Exception {
//Create
DigitalInput redButton = new DigitalInput();
DigitalOutput redLED = new DigitalOutput();
DigitalInput greenButton = new DigitalInput();
DigitalOutput greenLED = new DigitalOutput();
//Address
redLED.setHubPort(1);
redLED.setIsHubPortDevice(true);
redButton.setHubPort(0);
redButton.setIsHubPortDevice(true);
greenButton.setHubPort(5);
greenButton.setIsHubPortDevice(true);
greenLED.setHubPort(4);
greenLED.setIsHubPortDevice(true);
//Event
redButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
try {
redLED.setState(e.getState()); //Turn on red LED
if (e.getState()) { //Once red button is pushed, Decoder method is called for the users input.
System.out.print(" = ");
System.out.print(Decoder(userInput));
System.out.println();
userInput = ""; //Clears userInput for next code
}
} catch (PhidgetException ex) {
System.out.println("Failure: " + ex);
}
}
});
//Event
greenButton.addStateChangeListener(new DigitalInputStateChangeListener() {
public void onStateChange(DigitalInputStateChangeEvent e) {
try {
greenLED.setState(e.getState()); //Turn on green LED
if (e.getState()) {
startPress = System.currentTimeMillis(); //Grabs time when the button is first pressed
} else {
timePressed = ((double) (System.currentTimeMillis() - startPress));//Grabs time when button released, subtracts start time.
if ((timePressed > 100) && (timePressed < 600)) {
userInput += ".";
System.out.print("."); //Prints a dot to the screen
} else if (timePressed > 600) {
userInput += "-";
System.out.print("-"); //Prints a dash to the screen
}
}
} catch (PhidgetException ex) {
System.out.println("Failure: " + ex);
}
}
});
//Open
redLED.open(1000);
greenLED.open(1000);
redButton.open(1000);
greenButton.open(1000);
//Use your Phidgets
System.out.println(
"Start entering into the decoder." + "\n" + "Press the green button for a dot, hold for a dash." + "\n"
+ "Press the red button after you enter a letter.");
userInput = "";
//Wait for input
while (true) {
Thread.sleep(10);
}
}
}
//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 *
from Phidget22.Devices.DigitalOutput import *
#Required for sleep statement
import time
#Used to store times
start_press = 0
time_pressed = 0
#Used to store input
user_input = ""
#Convert user input from morse code to characters
def Decoder(code):
morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"Unknown Code"]
i = 0
while (morseCode[i] != "Unknown Code"):
if (code == morseCode[i]):
return (chr(97 + i)).upper() #Character a starts at 97.
i+=1
return '?' # unknown code
#Event
def onRedButton_StateChange(self, state):
global user_input
redLED.setState(state)
if (state): #Once red button is pushed, Decoder method is called for the users input
print(" = ", end = '')
print(Decoder(user_input))
print()
user_input = ""
#Event
def onGreenButton_StateChange(self, state):
global start_press
global time_pressed
global user_input
greenLED.setState(state) #Turns on green LED
if (state):
start_press = time.time() #Store current time
else:
time_pressed = (time.time() - start_press) #Determine how long button was pressed
if ((time_pressed > 0.1) and (time_pressed < 0.6)):
print(".", end = '') #Prints dot to screen
user_input+= "."
elif (time_pressed > 0.6):
print("-", end = '') #Prints dash to screen
user_input+= "-"
#Create
redButton = DigitalInput()
redLED = DigitalOutput()
greenButton = DigitalInput()
greenLED = DigitalOutput()
#Address
redButton.setHubPort(0)
redButton.setIsHubPortDevice(True)
redLED.setHubPort(1)
redLED.setIsHubPortDevice(True)
greenButton.setHubPort(5)
greenButton.setIsHubPortDevice(True)
greenLED.setHubPort(4)
greenLED.setIsHubPortDevice(True)
#Subscribe to Events
redButton.setOnStateChangeHandler(onRedButton_StateChange)
greenButton.setOnStateChangeHandler(onGreenButton_StateChange)
#Open
redLED.openWaitForAttachment(1000)
greenLED.openWaitForAttachment(1000)
redButton.openWaitForAttachment(1000)
greenButton.openWaitForAttachment(1000)
print("Start entering into the decoder." + "\n" + "Press the green button for a dot, hold for a dash." + "\n" + "Press the red button after you enter a letter.")
user_input = "" #reset because state change events execute after buttons are opened
while(True): #Wait for input
time.sleep(0.1)
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
//Add Phidgets Library
using Phidget22;
using System.Diagnostics;
namespace MorseDecoder
{
class Program
{
//Create
static DigitalOutput redLED;
static DigitalOutput greenLED;
//Used to store times
static double timePressed;
//Used to track time difference
static Stopwatch stopwatch = new Stopwatch();
//Used to store input
static string userInput = "";
//Convert user input from morse code to characters
static char Decoder(string code)
{
System.String[] morseCode = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "Unknown Code" };
int i = 0;
while (morseCode[i].Equals("Unknown Code") == false)
{
if (code.Equals(morseCode[i]))
{
return System.Char.ToUpper((char)(97 + i)); //Character 'a' starts at 97.
}
i++;
}
return '?'; //unknown code
}
//Event
private static void redButton_StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
{
redLED.State = e.State; //Turn on redLED
if (e.State)
{ //Once red button is pushed, Decoder methid is called for the users input.
System.Console.Write(" = ");
System.Console.Write(Decoder(userInput));
System.Console.WriteLine();
userInput = ""; //Clears userInput for next code
}
}
//Event
private static void greenButton_StateChange(object sender, Phidget22.Events.DigitalInputStateChangeEventArgs e)
{
greenLED.State = e.State; //Turn on green LED
if (e.State){
stopwatch.Start(); //Grab time when the button is first pressed
}
else{
timePressed = (stopwatch.ElapsedMilliseconds); //Grabs time when the button is released, and suctracts start time.
stopwatch.Reset();
if (timePressed < 600 && timePressed > 100)
{
userInput += ".";
System.Console.Write(".");
}
else if ((timePressed > 600) && (timePressed < 5000))
{
userInput += "-";
System.Console.Write("-");
}
}
}
static void Main(string[] args)
{
//Create stopwatch
stopwatch = new Stopwatch();
//Create
DigitalInput redButton = new DigitalInput();
redLED = new DigitalOutput();
DigitalInput greenButton = new DigitalInput();
greenLED = new DigitalOutput();
//Address
redLED.HubPort = 1;
redLED.IsHubPortDevice = true;
redButton.HubPort = 0;
redButton.IsHubPortDevice = true;
greenButton.HubPort = 5;
greenButton.IsHubPortDevice = true;
greenLED.HubPort = 4;
greenLED.IsHubPortDevice = true;
//Subscribe to Events
redButton.StateChange += redButton_StateChange;
greenButton.StateChange += greenButton_StateChange;
//Open
redLED.Open(1000);
greenLED.Open(1000);
redButton.Open(1000);
greenButton.Open(1000);
System.Console.WriteLine("Start entering into the decoder." + "\n" + "Press the green button for a dot, hold for a dash." + "\n" + "Press the red button after you enter a letter.");
userInput = "";
//wait for input
while (true)
{
System.Threading.Thread.Sleep(10);
}
}
}
}
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
When you enter a word into your program, you will see the result of the encoding via the LED.
Practice
- Modify both sets of code so numbers can be used.
- Try modifying the Encoder code to make the program shorter.
- Use the Power Plug Phidget with a lamp to make a larger scale encoder.