mechatronics i lab report 2 arduino morse encoder · mechatronics i lab report 2 arduino morse...

18

Upload: hanguyet

Post on 22-May-2018

253 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Mechatronics I

Lab Report 2

Arduino Morse Encoder

Instructor: Joseph Timothy Foley

Axel Bender & Elías Marel

September 27, 2012

Page 2: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Contents

Contents 1

1 Introduction 2

1.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Design 3

2.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 Testing 8

4 Usage 8

4.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Results and Discussion 9

6 Conclusion 9

6.1 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

7 References 11

8 Appendix 12

8.1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128.2 Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1

Page 3: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

1 Introduction

Communication issues between di�erent types of software is a problem every software andmechatronic engineer must face. It is often necessary to translate information between plat-forms, be it via analog or digital signals, and make it understandable for di�erent types ofhardware. In this lab exercise we are forced to deal with this well known issue to learn aboutit �rsthand. Another big factor of this experiment is to dive into real world implementationof functional programming. The design and development of the project will be implementedusing ideas and concepts of Test Driven Development (TDD).

A Morse code encoder will be designed using an Arduino Uno that is connected to a laptopvia a serial USB cable. The serial monitor of the Arduino development environment calledArduino 1.0.1 will be used to feed a message containing letters and symbols from a keyboardto the Arduino. The Arduino will then translate the message, if possible, and send it out inmorse code by blinking a LED.

1.1 Background

Morse code was in it's day the preferred method for long distance communication. Beforemen were able to transmit voice, radio transmissions were in the form of clicks, pulses andsilences. Morse code grew to popularity over many other transmission methods due to manyqualities, including the fact that one of it's creators, Alfred Vail researched the frequency of usefor each letter in the English language and designed the code for optimum transmission speed.

Samuel Morse only included numerals in his original morse code which dated back to the19th century. Morse proved in 1835 that signals could be transmitted through wires afterexperimenting with using electric current to move an electromagnet attached to a marker towrite dots and dashes on a piece of paper [1]. His partner Alfred Vail added the Englishalphabet to the code and eventually morse code would be used in the �rst radio transmissions.Even after vocal radio transmissions were possible morse code remained useful, especially inmilitary purposes. During World War II, for example, communication between ships and overlong distances was performed with morse code radio transmissions since voice radio had verylimited range. Today morse code is mostly used for emergency communication, mainly in airtra�c. The main reason for morse code's survival to date is that no special technology is neededto communicate in Morse code with a skilled listener, signals can be sent as blinking lights,knocks or other sounds.

Interestingly, morse code has been used by musicians to hide secret messages in their music.One example is The Strangler's 1978 song Enough Time which features traditional morse codebeeps that translates to: "SOS this is planet Earth - we are fucked - please advise". The mainguitar ri� in the Canadian Prog rock band Rush's instrumental song YYZ translates to Y YZ in morse code. And one of the most famous morse code messages in music is the start ofBeethoven's �fth symphony which incidentally spells out V in morse code, V being the Romannumeral for 5. This is mostly interesting because Beethoven wrote his �fth symphony in 1804- 1808, years before morse code even existed. The opening to the �fth symphony became thesymbol of allied victory in WW2 because of this, V being the radio transmission meaning"Victory" after a battle. [2]

2

Page 4: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Figure 1: A circuit diagram of the morse encoder.

2 Design

We started out developing a simple circuit design to convert the morse code, that wouldeventually be encoded by software written in C++ for the arduino, into the physical world.Our design consisted of an Arduino Uno to write our code on, a breadboard to build the circuiton, a potentiometer to control the frequency of the morse signal, a green LED to display ourmorse code with and some wires to connect the components to the arduino's output. Weexamined the available outputs on the arduino and decided that pins 12 and 13 were optimalto use for our outputs, for they are completely controllable. The idea for this setup came fromboth the lab instructions and further research into how the arduino sends signals to a circuit.

Upon some experimentation with the build of the circuit we came to the conclusion that inorder to get the desired 50 mA current through the circuit we would need to implement someresistors into the design. This is done to protect the Arduino unit. Next we built a separatecircuit with a button to manually override the encoding software, using a switch to go fromauto to manual.

At this point in the development of the project we decided that we wanted to implementsound to our circuit build, which we did by adding an electronic buzzer to the circuit in parallelwith the green LED. The last addition to the design came when we had started writing theencoder and had decided to include an error function, so when the user inserts any letters orsymbols outside the parameters we had set it would display an error. We connected a red LEDto the circuit to display when the encoder ran into an error. The �nished circuit can be seenin �gure 1.

2.1 Requirements

The requirements for our system is that it accepts a string input through the Arduino soft-ware's serial monitor and then systematically break it down into individual char variables thatare then translated into pulses of appropriate length. The pulses will be fed into our circuitand displayed via a green LED and sound from an electronic buzzer. The speed of the output

3

Page 5: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Figure 2: International morse code as de�ned by ITU-R [3].

Figure 3: A ASCII table.

signal must be controllable even though the program is running. It must also include a manualoverride that can be operated by a human and disregard anything the program is doing.

The Radiocommunication Sector of The International Telecommunication Union (ITU-R)issued a recommendation booklet about the proper use of morse code in 2009. The de�nitionof morse code that is listed in this document is used in this project. This version of theinternational morse code includes the English alphabet, Arabic numerals and a few punctuationmarks. The document also lists morse code for various transmission signs such as "Understood","Wait" and "End of work" that are not in the scope of this project since we are only dealingwith letters and symbols. We made a table where all the letters and symbols encodable withthe device are listed along with their morse code as de�ned by ITU-R [3]. The table can beseen in �gure 2. To encode theses letters and symbols we referred to the ASCII table [4] whichthe computer and therefore the Arduino 1.0.1 software use to represent letters and symbols.The table can be seen in �gure 3. If the input is lower case letters, they will be automaticallyconverted to upper case letters and then translated. If the input is a symbol that is not inthe table in �gure 2 it will not be translated and a error will be displayed, both in the serialmonitor of the Arduino 1.0.1 software and via a red LED in the circuit.

4

Page 6: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Figure 4: A Modular Dependency Diagram of the program.

To do this we need to create an array with char variables to use as a library to compare ourinput string with. This array will span symbols in the ASCII table from the value 34 to 90.Then we have to create a function that loops through the input string and analyses each charvariable, to be able to convert each letter to a series of short and long pulses. Depending onhow each letter corresponds to short and long, we make the loop function go into the desiredfunction to create the pulses and send as output from the arduino into our circuit. In orderto be able to control the frequency of the morse code while it is running we have to have thearduino emit a 5 V controlling voltage to the potentiometer and accept a changing analog signalto an input.

2.2 Software

This assignment is very software demanding. Therefore we focused our main attention onwriting the code for the system. A Modular Dependency Diagram of the program can be seenin �gure 4 and below is a pseudocode describing the program step by step. The actual C++code can be found in section 8.1, Appendix.

Decleration of variables

Set greenled and redled as output variables 12 and 13.Set minpulse = 50, the minimum pulse length (50 ms)Set scaleofpulse = 6, this controls the scale of the potentiometer.Set pot = A0, assigns an input variable for the potentiometer.Set serialinput = 0, giving variable an initial value.Set rounder = 0, to use with Serial.print to print out the letters we are turning into morse code.Set errortime = 500, to control the time of the error display.Set timer as an unsigned long variable, this will be used to count time for the error function.

Create morselib, a reference library which holds the corresponding morse code for the letterswe want to translate.

5

Page 7: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Setup function

{Set pin 12 as outputSet pin 13 as outputAssign the serial port, speed 9600 bit/s

}

Main looping function

{If the error timer is �nished.{

Turn o� the signal to redled to stop the error display.}If text is available for encoding.{

serialinput accepts data from the serial portPrint out the letter we are converting and corresponding morse code.Calls for the translator function with serialinput as input variable.Test: prints out the length of short and long morse outputs.

}}

Translator function

{Set index variable which selects the corresponding morse code from the morselib, relative towhich letter is being encoded.

If the symbol being translated is a space{

Calls for the space function}If the symbol being translated is not within the parameters of morselib{

Calls for the error function}Loops through each element of the character being encoded within the morselib{

If the element is for a short output{

Calls for the short function}If the element is for a long output{

Calls for the long function

}If the symbol being encoded is not accepted by the morselib{

Calls for the error function}

}

6

Page 8: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Calls for the letterspace function}

Short function

{Set pulse variable, controls the length of the delay.Print out " Short " on the serial window.Turns on the signal to greenled for the duration of pulse.Turns of the signal to greenled and waits for the duration of pulse.

}

Long function

{Set pulseon variable, controls the length of the delay.Set pulseo� variable, controls the length of the delay time after shutting o�.Print out " Long " on the serial window.Turns on the signal to greenled for the duration of pulseon.Turns of the signal to greenled and waits for the duration of pulse.

}

Error function

{Set pulse variable, controls the length of the delay.Print out " Incorrect input " on the serial window.Turns on the signal to redled.Starts the timer counting in milliseconds.

}

Letterspace function

{Set pulse variable, controls the length of the delay.Delays for the duration of pulse.

}

Space function

{Set pulse variable, controls the length of the delay.Delays for the duration of pulse.

}

2.3 Limitations

This project has its limitations as one might expect. To start o� our system only acceptscertain type of input variables, both lower and higher case letters and a handful of symbols.Now in order to avoid a system failure when the user picks a symbol not accepted by theprogram we used defensive programming to included an error function. Whether the symbol isnot within the parameters of the morselib array or it is an illegal symbol within the array thesystem defaults to an error function. The function sends a signal to a red diode on the circuitboard for a duration equal to a normal white-space so not to o�set the system. In short noenvironmental factors limit the usage of the device. Although it is advised to be indoors and

7

Page 9: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

keep food and beverages away from the arduino at all times.

Another thing to consider is that the system needs to be connected to a computer at alltimes. It would be optimal to have an external power supply mounted to system as well a wayto input data or text to be encoded.

3 Testing

While developing the software we relied heavily on test driven programming. Before startingour project we had to make sure that the Arduino Uno unit was not defective. To do this weuploaded the example Blink function to the arduino unit which resulted in a light on the unitto blink indicating that it was working correctly.

Our �rst objective was to program the arduino to send a steady signal through a selectedoutput to the circuit. To test this we placed a diode in the circuit we had built, uploaded thecode we had written for this purpose and when the diode lid up we had con�rmation that thissection was functional.

Next section of the project that needed to be tested was whether or not we are able to accepta string input and break it up to be analysed and encoded later. To do this we print out theletter being encoded and the corresponding morse code. Each time the void loop goes througha cycle.

We performed the same test on a string including every available variable in the morselibarray along with some illegal symbols to see if our encoder was working as intended. Resultscan be seen in section 8.2 in the appendix.

The last test we had to perform was to test if the length of the morse code pulses was accurate.To do this we wrote a separate code that reads the output of our program on a second arduinoand measures the time from a high to a low, i.e. times the pulses. Then we compare the shortand long signals to see if the ratio is correct. See section 8.1 in the appendix. Upon testing wediscovered that it was crucial to connect both arduino units used for this test to mutual groundto get uncorrupted read on the signal from the encoder.

4 Usage

The device is useful for people who for some reason need to communicate in morse code butare not trained in transmitting morse code. be able to transmit code so it can be understoodtakes a lot of training so not everyone who needs to transmit morse code has the ability to doit, at least straight away. In order to use the device some steps must be taken in assembly andsoftware installation.

4.1 Installation

To be able to use the device it is necessary to have the Arduino 1.0.1 software installed. Ifit is not installed on the computer the device is to be used with it can be downloaded andinstalled from Arduino's homepage [5].

1. Turn on the computer and start an operating system.

8

Page 10: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

2. Open a web browser and go to Arduino's homepage [5].

3. Download and install Arduino 1.0.1. by following the instructions on Arduino's homepage.

4. Install the serial driver for the Arduino Uno.

5. Connect the arduino to a working USB port.

6. Construct the circuit as described above. The circuit can be seen in �gure 1.

7. Open the Arduino 1.0.1 software and open the morse encoder software, either by cut-ting and pasting into a new sketch or by downloading the program here: https://dl.

dropbox.com/u/2086034/morse_encoder.ino. This is an open download link hosted bydropbox.com.

8. Upload the program to the Arduino Uno.

4.2 Instructions

1. Open the serial monitor.

2. Type in the message to encode and press Enter.

3. If an invalid letter or symbol is entered an error display will let you know.

5 Results and Discussion

By implementing test driven development we were successfully able to limit the amount oftime and energy lost to bugs and errors in the programming. This was done by cutting theprogram up into objects or seperate functions that each have a restricted and well de�nedpurpose and testing each step of the way. Naturally some problems occurred, but none of themstopped the programming for long. The circuit was built without any major hitches, experiencein electric circuits came to good use there. Testing proved the function of the device as can beseen in sections 5 and 6

6 Conclusion

The product of this lab report is a working morse decoder that can handle all the lettersand symbols in international morse code as de�ned by ITU-R [3]. It displays the translatedmessage using a blinking green LED along with an electric buzzer and printing the result out inthe serial monitor. The speed of the transmission can be altered while the program is running,a error displays when an invalid character is entered, both via a red LED and in the serialmonitor of the development software. A manual override was implemented that can be turnedon and used by a human in spite of what the encoder is trying to display. The objectives set inthe beginning of the project have all been met and some additional functions have been addedover the course of the project.

Possible costumers for the device include radio amateurs that are honing their skills at morsecode transmissions. The device is very useful as a learning tool for people who are learningmorse code and want a safe and sure way to transmit morse code.

9

Page 11: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

6.1 Future work

A way to take this project further would be to develop a decoder capable of translating thewhole library for any given frequency. This addition would be make morse code translationcapable for people with little or no knowledge of the code. The system might also be used asa learning tool for people interested in morse code to learn how to decipher it.

10

Page 12: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

7 References

[1] M. Bellis, �The History of the Telegraph,� September 2012. [Online]. Available:http://www.inventors.about.com/od/tstartinventions/a/telegraph.htm

[2] G. Stanley, The Cambridge Companion to Beethoven. Cambridge University Press, 2000.

[3] �Recommendation ITU-R M.1677-1 (10/2009), International Morse Code,� Septem-ber 2012. [Online]. Available: http://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1677-1-200910-I!!PDF-E.pdf

[4] �ASCII Table and Description,� September 2012. [Online]. Available: http://www.arduino.cc/

[5] �Arduino homepage,� September 2012. [Online]. Available: http://www.asciitable.com/

11

Page 13: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

8 Appendix

8.1 Code

1 Encoder :

3 // Var iab le d e f i n i t i o n s .

5 i n t g r e en l ed = 12 , r ed l ed = 13 ;i n t minpulse = 50 ; // Sets the minimum pul s e l ength as 50 ms . I f minpulse = 50

and s c a l e o f p u l s e = 6 the dot l ength can be changed from 50 ms to 220 ms byad ju s t i ng the potent iometer whi l e the program i s running .

7 i n t s c a l e o f p u l s e = 6 ; // Sets the d i v i d e r va lue f o r each pulse , This c on t r o l sthe s c a l e o f the potent iometer . I f minpulse = 50 ms , d i v i s i o n by 2 means thel ong e s t pu l s e w i l l be (1023 ms / 2) + 50 ms = 562 ms .

i n t pot = A0 ; // A potent iometer i s used to r e gu l a t e the speed o f the encoder .9 i n t s e r i a l i n p u t = 0 ; // I n t i t a l va lue f o r the s e r i a l input s e l e c t e d as 0 .i n t rounder = 0 ; // Makes S e r i a l . p r i n t p r i n t out l e t t e r s and symbols i n s t ead o f

the ASCII va lue .11 unsigned long e r ro r t ime = 50UL; // Sets the d i sp l ay time f o r the e r r o r func i t on

to 50 ms . UL i s to s p e c i f y that the va r i a b l e type i s unsigned long .unsigned long t imer ; // Dec lare the t imer va r i ab l e used to stop the e r r o r

d i s p l ay i ng .13

// A r e f e r e n c e array o f po i n t e r s r ep r e s en t i ng the ASCII va lue s from 34 f o r thesymbol " to 90 f o r the l e t t e r Z . The s t r i n g s in the array r ep r e s en t the Morsecode f o r each l e t t e r or symbol . I f the l e t t e r i s not in Morse code a " i " f o ri gno re s e t s o f f a e r r o r .

15 char ∗morse l ib [ ] = {" s l s s l s " , " i " , " i " , " i " , " i " , " s l l l l s " , " l s l l s " , " l s l l s l " , "i " , " s l s l s " , " l l s s l l " , " l s s s s l " , " s l s l s l " , " l s s l s " , " l l l l l " , " s l l l l " , " s s l l l ", " s s s l l " , " s s s s l " , " s s s s s " , " l s s s s " , " l l s s s " , " l l l s s " , " l l l l s " , " l l l s s s " , " i" , " i " , " l s s s l " , " i " , " s s l l s s " , " s l l s l s " , " s l " , " l s s s " , " l s l s " , " l s s " , " s " , "s s l s " , " l l s " , " s s s s " , " s s " , " s l l l " , " l s l " , " s l s s " , " l l " , " l s " , " l l l " , " s l l s " ," l l s l " , " s l s " , " s s s " , " l " , " s s l " , " s s s l " , " s l l " , " l s s l " , " l s l l " , " l l s s " } ;

// Legend : """ "#" "$" "%" "&" " '" "(" ") " "∗""+" " ," "−" " ." "/" "0" "1" "2"

"3" "4" "5" "6" "7" "8" "9" " :"" ;" "<" "=" ">" "?" "@" "A" "B" "C" "D" "E"

"F" "G" "H" " I " "J" "K" "L" "M" "N" "O" "P""Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

17

void setup ( )19 {

pinMode ( green led , OUTPUT) ; // Ass igns output 12 f o r green LED f o r morsed i sp l ay .

21 pinMode ( red led , OUTPUT) ; // Assign output 13 f o r red LED f o r e r r o r d i sp l ay .S e r i a l . begin ( 9600 ) ; // Sets up s e r i a l port to communicate with arduino ,

speed 9600 b i t / s .23 }

25 // S e r i a l l i s t e n e r , must be c a l l e d " loop " .

27 void loop ( ){

29 i f ( m i l l i s ( ) − t imer >= er ro r t ime ) // Stops the e r r o r d i sp l ay a f t e r the t imerhas f i n i s h e d .

{31 d i g i t a lWr i t e ( red led , LOW ) ;

}

12

Page 14: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

33 i f ( S e r i a l . a v a i l a b l e ( ) > 0 ) // I f the re i s something to t r an s l a t e , the symboli s t r an s l a t ed and d i sp layed on the s e r i a l monitor .

{35 s e r i a l i n p u t = toupper ( S e r i a l . read ( ) ) ;

S e r i a l . p r i n t ( "\n Trans la t ing : " ) ;37 S e r i a l . p r i n t l n ( s e r i a l i n pu t , 0) ;

S e r i a l . p r i n t ( "Morse code : " ) ;39 t r a n s l a t o r ( s e r i a l i n p u t ) ;

S e r i a l . p r i n t ( "\nDot l ength in ms : " ) ;41 S e r i a l . p r i n t l n ( ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse , 1) ; // Disp lays

the l ength o f each dot , the lowest pu l s e l ength .}

43 }

45 // Trans la tor from s e r i a l input to ASCII va lue s . ASCII t ab l e found here : http://www. a s c i i t a b l e . com .

void t r a n s l a t o r ( char symbol )47 {

i n t index = symbol − ' " ' ; // Locates the c o r r e c t array element by subt ra c t i ngthe ASCII va lue 34 f o r " from the symbol ' s ASCII va lue .

49 char ∗ encoder = morse l ib [ index ] ; // Copies a s t r i n g from the ∗morse l ibr e f e r e n c e array

51 i f ( symbol == ' ' ) // When a space between words i s entered in the s e r i a lmonitor the func t i on space ( ) c r e a t e s a de lay f o r 6 dots . The de lay a f t e reach shor t or long i s 1 dot long to make the de lay between words 7 dotslong .

{53 space ( ) ;

r e turn ;55 }

e l s e i f ( index < 0 | | index > 56 ) // Makes sure only the scope o f the ∗morse l ib r e f e r e n c e array i s used , from the ASCII va lue s 34 to 90 . Otherwisea e r r o r i s d i sp l ayed .

57 {e r r ( ) ;

59 re turn ;}

61

f o r ( i n t i = 0 ; i < s t r l e n ( encoder ) ; i++ ) // A f o r loop that walks througheach element o f the s t r i n g taken from the ∗morse l ib r e f e r e n c e array .

63 {i f ( encoder [ i ]== ' s ' ) // I f the s t r i n g element i s s the void func t i on sh ( )

i s s t a r t ed to d i sp l ay a dot ( a shor t s i g n a l ) .65 {

sh ( ) ;67 }

e l s e i f ( encoder [ i ] == ' l ' ) // I f the s t r i n g element i s l the void func t i onl o ( ) i s s t a r t ed to d i sp l ay a dash ( a 3 dot long s i g n a l ) .

69 {l o ( ) ;

71 }e l s e

73 {e r r ( ) ; // I f the l e t t e r i s marked with a i f o r i gno re a e r r o r i s d i sp l ayed

.75 }

}77 l e t t e r s p a c e ( ) ; // Makes the space between each l e t t e r 3 dots long by adding 2

dots o f space a f t e r the one dot in sh ( ) and l o ( ) .

13

Page 15: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

}79

// De f i n i t i o n o f f unc t i on s81

void sh ( ) // Disp lays a shor t pu l s e ( dot ) .83 {

i n t pu l s e = ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ; // This v a r i ab l ec on t r o l s the space l ength .

85 S e r i a l . p r i n t ( "Short " ) ;d i g i t a lWr i t e ( green led , HIGH ) ;

87 delay ( pu l s e ) ;d i g i t a lWr i t e ( green led , LOW ) ;

89 delay ( pu l s e ) ;}

91

void l o ( ) // Disp lays a long pu l s e ( dash ) .93 {

i n t pulseon = (3∗ ( ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ) ) ;95 i n t p u l s e o f f = ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ;

S e r i a l . p r i n t ( "Long " ) ;97 d i g i t a lWr i t e ( green led , HIGH ) ;

de lay ( pulseon ) ;99 d i g i t a lWr i t e ( green led , LOW ) ;

de lay ( p u l s e o f f ) ;101 }

103 void e r r ( ) // Disp lays a e r r o r message and l i g h t s a red LED.{

105 i n t pu l s e = (1∗ ( ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ) ) ;S e r i a l . p r i n t ( " I n c o r r e c t input " ) ;

107 d i g i t a lWr i t e ( red led , HIGH ) ;t imer = m i l l i s ( ) ; // S ta r t s a t imer that counts un t i l the e r ro r t ime p r e s e t i s

reached .109 }

111 void l e t t e r s p a c e ( ){

113 i n t pu l s e = (2∗ ( ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ) ) ;de lay ( pu l s e ) ;

115 }

117 void space ( ){

119 i n t pu l s e = (6∗ ( ( analogRead ( pot ) / s c a l e o f p u l s e ) + minpulse ) ) ;de lay ( pu l s e ) ;

121 }

123 // End o f program

14

Page 16: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

1 Decoder :i n t readpin = 12 ;

3 unsigned long durat ion ;

5 void setup ( ){

7 pinMode ( readpin , INPUT) ;S e r i a l . begin (9600) ;

9 }

11 void loop ( ){

13 durat ion = pu l s e In ( readpin , HIGH, 350000) ;i f ( durat ion > 20000)

15 {S e r i a l . p r i n t ( "\n Pulse [ us ] : " ) ;

17 S e r i a l . p r i n t ( durat ion ) ;}

19 }

8.2 Tests

1 Test1 :

3 This i s wr i t t en in to the s e r i a l monitor :"#$%&'()∗+,−../0123456789:;<=>?@ABCDEFGHJKLMNOPQRSTUVWXYZ

See results in the �gures below:

Test2 :2

We typed SOS in to the s e r i a l port o f the encoder and t h i s i s what we read fromthe s e r i a l port on the decoder :

4

Pulse [ us ] : 497436 Pulse [ us ] : 49744

Pulse [ us ] : 497468 Pulse [ us ] : 149224

Pulse [ us ] : 14922810 Pulse [ us ] : 149230

Pulse [ us ] : 4974312 Pulse [ us ] : 49746

Pulse [ us ] : 49746

15

Page 17: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Figure 5:

16

Page 18: Mechatronics I Lab Report 2 Arduino Morse Encoder · Mechatronics I Lab Report 2 Arduino Morse Encoder ... In this lab exercise we are forced to deal ... eventually be encoded by

Figure 6:

17