+ All Categories
Home > Documents > T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf ·...

T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf ·...

Date post: 16-Mar-2018
Category:
Upload: tranthuy
View: 224 times
Download: 1 times
Share this document with a friend
23
T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur Pétursson December 2, 2012
Transcript
Page 1: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

T-411-MECH Mechatronics 1Lab 2: Morse code

Árni Þorvaldsson and Sæþór Ólafur Pétursson

December 2, 2012

Page 2: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Contents

1 Introduction 21.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Design 22.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2.1.1 Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.2 Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.3 Strategy to meet requirements . . . . . . . . . . . . . . . . . . . . . . . . 32.1.4 Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.5 Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.2 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2.1 Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.2.2 Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

2.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 Usage 73.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Testing 94.1 Encoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.2 Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94.3 Results/Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

5 Conclusion 12

6 Future Work 12

A Appendix: Encoder 14

B Appendix: Decoder 17

1

Page 3: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Operating Voltage 5VInput Voltage(recommended) 7-12VInput Voltage(limits) 6-20VDigital Pins 14 PinsAnalog Input I/O Pins 6 PinsDC Current per I/O Pin 40 mADC Current per 3.3V Pin 50 mAFlash Memory 32 KBSRAM 2 KBEEPROM 1 KBClock Speed 16 MHz

Table 1: Arduino Uno specifications [1]

1 Introduction

The objectives of this exercise is to learn how to use Arduino microcontroller, coding envi-ronment and the final objective is to design and make our own Morse encoder and decoderwhich solves the problem of long distance communication known in the past when starting outwith communication devices. Making the Morse encoder/decoder with Arduino makes it novel.Finally the Morse encoder will be tested.

1.1 Background

Arduino is an open-source electronics platform intended to be easy to use hardware and software[1]. The type we are using is Arduino Uno (see figure 3) which is a microcontroller based onATmega328. Summary can be seen in table 1

In 1830 Joseph Henry was first to send electric current for over a mile on wire whichactivated an electromagnet and made a bell ring. 7 years later or 1837 an American inventorand a succesfull painter Samuel Finley Breese Morse patented a working telegraph machineor the Morse code. He started out by using dot and spaces code for numbers and lettersbut later the code was improved by adding dashes. First telegraph line was build betweenWashingdon D.C. and Baltimore and on May 24, 1844 the first telegraphic message was sentthrough that line. The message was "what hath God wrought". Long distance communicationswere revolutionized by Morse invention[2]. Currently it is mostly used by radio amateurs[3]

2 Design

The system we developed was made from scratch, by getting knowledge of the morsecode envi-ronment that is listed the book Introduction to Mechatronic Design [4], mapping the problemswe need to solve and then using pseudo code before we tried to do anything. Concept was touse a simple buzzer, a led and an Arduino for the design to make the encoder as simple aspossible. The decoder was done in a similar manner just but adding a button for the manual

2

Page 4: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 1: Arduino Uno microprocessor

input to the Arduino. The decoder is more complex in it’s nature because it needs to be ableto handle individual input from the individual users. This technology is way out of date, andis just a school project so this morse code computer will not be used for anything of notice.

2.1 Requirements

2.1.1 Encoder

1. Demonstrate the ability to correctly transform letters typed into a keyboard into theappropriate human readable morse code on an LED

2. Have test code that can cycle through the entire alphabet

3. Be able to change the morse code timings while the program is still running.

4. The timing for each part needs to be correct.

5. A button that can flash the led regardless what the program is doing.

2.1.2 Decoder

1. Should be able to decode morse code back to letters and display them on a screen.

2. Make it recognize individual morse code, bad or good.

3. Handle large variations in timing.

4. Try to have a test code to implement various timings automatically.

2.1.3 Strategy to meet requirements

2.1.4 Encoder

1. Using two kinds arrays with the same length one for written characters and the otherwith the symbols for the morse code. Morse code symbols for each letter is in the same

3

Page 5: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

place as the letters in the other character vector, so using a for loop we can find whateach letter represents in morse code. The from that using functions blink the led andmake an buzzer sound at a same time.

2. No code is needed to use. We can just copy paste the character array to the commandwindow and from that the system should run through all the alphabet.

3. By using an potentiometer and an if statement in the program.

4. Getting the timing right for each part will be checked via oscilloscope.

5. Using the 3.3V output from the Arduino we can bypass the program and flash the led.

2.1.5 Decoder

1. By using a button and taking into consideration that the morse speed varies from personto person. Then implementing into the code a dot time for each letter the individual ismorse coding, by taking the first space between dot/dash. Because that space should beequal or similar to the dot then we can find the dash which should be similar to 3 timesthe dot found for that given speed.

2. To recognize individual morse code, the code is implemented in a way to recognize a dotor dash which varies from the first dot value of the letter. After the first space the dothas been found and the interval for the next dot to be recognized as from zero to twotimes the dot found first.

3. To handle a significant variations of timings. The interval of the recognized dot or dashis shortened or lengthened after how long the first space is in the letter.

4. By connecting the encoder output to the decoder input we can test variety speeds byusing the potentiometer on the encoder to make it go faster or slower. Then we just copypaste or write in the encoder what we want to test.

2.2 Software

2.2.1 Encoder

The Encoders software main functionality is to change characters we write in the commandwindow into beeps and buzzes. A simple encoder Software architecture can be seen Fig. 2

In the pseudo code for the main loop of the program2.1 it can be seen that few functionsare used, FINDPLACE finds the place of the letter in an array, MORSE2SOUND sends signalaccording to the symbol and BETWEENLETTERS puts in a delay in between letters. Toupper

4

Page 6: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 2: Encoder software Architecture

is an inbuilt function and puts all letters to upper case. The source code can be seen in AppendixB with relevant comments.

Algorithm 2.1: Morsecode(0)

comment:Using few function together to implement the morsesounds

if Serial.available() > 0

then

letter ← serial.read()

letter = toupper(letter)

place = findplace(letter)

comment: Find the index of the Char in the MorseCode array

Morse2sound()

comment: Changes dots and dashes into pin outputs

if place! = 0

then

betweenletters()

comment: If there is not a space there we put delay by 3

5

Page 7: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 3: Decoder software Architecture

Algorithm 2.2: Morsecode1(0)

comment: Pseudo code of the Morse encoder loop

while (wait < 5dot)

then

if (buttonPushed())

then

{if (count == 1)

then dot← wait

CheckDotDash()

FindLetter()

let wait = 0

let count = 0

let dotdash = ””

2.2.2 Decoder

The Decoder software main goal is to be able to decode morse code back to letters and displaythem on the screen. It should also take into consideration the speed of the input varies fromindividual. A simple decoder Software architecture can be seen Fig. 3

In the pseudo code for the main loop for the decoder program2.2 it can be seen that fewfunctions are used, BUTTONPUSHED which is a boolean function that tells us if the buttonis down and gives the length of dot/dash. BUTTONUP a boolean function that tells the us ifthe button is down and gives us time for the space between dot/dash. CHECK_DOTDASHtakes the values the previous functions give us and decide if the input was a dot or a dash. Thesource code can be seen in Appendix B with relevant comments.

2.3 Limitations

Our system is not able to function in most environments so having it indoors with no moisture,no static and no liquid would be an ideal place for the Morse Code computer we built. There

6

Page 8: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

is no need for a Morse Code machine like this in any other place then indoors so this does notcause a problem to us as developers.

3 Usage

In order to use this program the user has to have some experience with motorcontroller andbasic electric circuits, circuits and wiring can be seen in Figs. 4 and 5. For further developmentthe field of expertise needed is in programming part since the code for the decoder is quitechallenging.

3.1 Installation

Software required to view, edit and use our Morse code is Arduino software. Here is a step bystep instruction how to install the software.

1. Start up your computer

2. Open your web browser and go to website http://arduino.cc/en/Main/Software

3. Download from there Arduino software compatible with your computer

4. After downloading the software, follow install instructions

3.2 Instructions

Step by step Instructions.

1. Start by activating the Arduino software and uploading the program from SVN folderhttps://projects.cs.ru.is/projects/mechatronics1-2012/repository/show/Submit/

LAB2-MorseEncode/saethor10_arnitho10 to the Arduino platfrom.

2. Connect the buzzer, potentiometer and led to right pins. See Fig 4 also pins can be seenin code in Appendix B

3. Open the Serial monitor in the Arduino software see Fig. 6

4. Then just write what you want in the serial monitor and press Enter.

5. Potentiometer can be used to adjust the morse speed.

6. Manual Override button blinks the LED at any wanted time.

7. Do not expose to water or fire and make sure the wiring is correct to prevent damages tothe devices.

7

Page 9: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 4: Figure of Encoders circuits and components

Figure 5: Figure of Decoders circuits and components

8

Page 10: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 6: Serial monitor

signal lengthdot 100 msdash 300 msspace characters 300 msspace words 700 ms

Table 2: Results from test on Morse code [1]

4 Testing

4.1 Encoder

Testing is an important part of developing a device, testing all inputs and outputs can improvethe device and prevent any dangerous behaviour on the equipment. To test the morse code weused the oscilloscope function on NI Elvis [5], a wire is connected from the input of the led tothe port A of the oscilloscope and a ground is connected from exit of buzzer to port A- (seefigure 7. We did tests on the length of the dots, dashes, space between characters and spacebetween words. At first we got the dots and dashes right but spaces were wrong. After fixinga minor flaws on the code we got all the lengths right and results can be seen in table 2 andfigures 8 9 10. Tests were also done on characters and symbol that were not in the code andturned out that the program did not respond to those unknown characters or symbols. Allpossible inputs we could think of were tested and those inputs that we did not define in ourprogram resulted in no output.

4.2 Decoder

The testing strategy of the morse decoder was done by putting different and incorrect inputsmanually and check if the morse decoder can handle a variety of speeds.Input testPutting input similar to this one -> . . . . . . . . . . . . which does not mean anythingand checking what the decoder would do with that information. The result was always thatthe decoder did not recognize the string of dots and dashes, so it did not give out any data on

9

Page 11: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 7: Morse encoder wired to oscilloscope

Figure 8: Measurement of dot and space, character A displayed

what letter it was. So it practically did nothing with the incorrect input and then waited forthe next input.Speed testMaking different individuals try out our morse decoder and check if it works from person toperson. And asking them to change their speed for different inputs on occasion. Results of thistest proved to be successful and different people with different speeds can operate the decoder.Then by wiring the output of the encoder to the decoder input as can be seen in Fig. 11, aseries of letters and words were tested by making them talk with each other. And by using thepotentiometer to change the speed of the input. The results of this test made it clear to usthat letters with just single dash or dot do not work. And we can fix that in the future but fornow this will do.

These tests are important for being sure that all the requirements have been met. Allpossible inputs and varieties of inputs was tested and if something was missing the developersjust did not think about it.

10

Page 12: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Figure 9: Measurement of space between characters

Figure 10: Measurement of space between words

Figure 11: Using the encoder to test the decoder

11

Page 13: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

4.3 Results/Discussion

After testing a variety of outputs from a single letter, two letters and letters with spacesbetween we recorded all the data we needed to implement the needed time frame for each ofthese outputs. So now we have a Morse Encoder with the right time frame for the dot, dash andspaces. And by turning the potentiometer we can make this time frame be longer or shorterwith the right ratio.

Implementing the decoder turned out to be more challenging than the encoder, the sourcecode requirements are much harder to meet. By compensating on few things we managed toget it working but the user has to try it a few times to get it working 100% correctly. Ourdecoder program is adaptive and can take different length of pulses for dot and dashes from theuser. That was done by measuring the length of space between letters and use that to definedot. The hardest part was to get the program to stop running and print out the letter but wefound a raw solution which can be a good starting point for further improvement. We used thespace between characters to see when the user is done with each letter but the flaws on thatstrategy is that an additional input is needed from the user to get this space measured. Thenafter that additional input, next letter can be produced.

5 Conclusion

Our system is capable of taking in input from the user and display the Morse signal both inform of led light as well as a tone from a buzzer. The input from the user can contain severalletters numbers and spaces all combined and mixed after user needs. The user can also generatehis own morse code by using a button on the breadbord 5, the decoder program is adaptive andlearns how long signals the user generates. Letters are then displayed on the terminal windowwhen they have been produced on the button. The Morse encoder and decoder can be usefulin many ways, for example Scouts that are learning about the Morse Code can now have aneasy way of training their skills.

Our objective with this project was to build a working Morse encoder/decoder which we didso our goals was reached. Performing the tests turned out to be very useful since we spottedsome errors in the code which we were able to fix. Building our own Morse encoder and decoderwas challenging but in the meantime a fun project.

6 Future Work

For further development on the morse computer we have set up, would be to add the decoderand the encoder to the same device by merging the codes together. A essential improvementwill be made when the time is given and that is adding a wireless communication betweentwo Arduinos via Xbees wireless modules. So people can morse together wireless. That wouldmake people more interested in trying or maybe buying this device. The estimated time ofthese improvements would probably take about 3-7 days.

12

Page 14: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

References

[1] “Arduino official webpage,” september 2012. [Online]. Available: www.arduino.cc

[2] “The history of the telegraph,” October 2012. [Online]. Available: http://inventors.about.com/od/tstartinventions/a/telegraph.htm

[3] october 2012. [Online]. Available: http://www.linguanaut.com/morse_code.htm

[4] J. E. Carryer, R. M. Ohline, and T. W. Kenny, Introduction to Mechatronic Design. PearsonEducation, 2011.

[5] “Ni elvis,” October 2012. [Online]. Available: http://www.ni.com/nielvis/

13

Page 15: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

A Appendix: Encoder

//Morsecode Encoder by Árni Þorvaldsson and Sæþór Ólafur Pétursson

//Intructors Joseph timothy foley and Bjorgvin Halldórsson.

//This is a basic encoder program and a person with just basics in c++ could read through this code and understand it.

int led = 7,buzzer = 4, place, time = 100 /*ms*/, Unit = 100 ;

char letter;

//Creating the two identically long vectors that for the same index in bothe represent the same letter.

const char LegalChars[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const char MorseCode[][8] = {" ",".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",

".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

void setup() {

Serial.begin(9600);

Serial.println("MorseCode Encoder");

Serial.println("Write the string or letter you want morse.");

pinMode(led, OUTPUT); // set a pin for led output

pinMode(buzzer, OUTPUT); //set a pin for buzzer output

}

char findplace(char letter){ // This function finds the index of the character you typed in.

for(int i=0; i < strlen(LegalChars); i++){

if(letter == LegalChars[i]){

place = i;

}

}

return place;

}

void beep(){ // Gives the wanted sound for the morse.

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

tone(buzzer,2500,Unit); // turn the buzzer on

delay(Unit); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

14

Page 16: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

}

void beep_space(){ // The same as above but makes no sound for the space between words.

digitalWrite(led, LOW); // turn the LED on (HIGH is the voltage level)

tone(buzzer,0,Unit); // turn the buzzer on

delay(Unit); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

}

void dot(){ // dot function changes the the Unit to 1 time unit.

Unit = time;

}

void dash(){ // dash function changes the the Unit to 3 time units.

Unit = time*3;

}

void space(){ // space function changes the the Unit to 3 time units.

Unit = time*3; // for space beetwen characters

}

void beetweenletters(){ // space function changes the the Unit to 2 time units.

Unit = time*2; // after debugging via Oscilloscope we just needed 2.

}

void Word(){

Unit = time*4; // Need to have 4 for space beetween word. The other functions

} // take care of the next 3 which should be total of 7 between words.

void Morse2sound(){ // Reads through the morse symbols and gives a beep for each symbol for a given period of time.

for(int i = 0; i < strlen(MorseCode[place]) ; i++){

if(MorseCode[place][i] == ’.’){

dot();

beep();

delay(time); }

else if(MorseCode[place][i] == ’-’){

dash();

beep();

15

Page 17: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

delay(time); }

else if(MorseCode[place][i] == ’ ’){

Word();

beep_space();

//Serial.print("Here is space");

//delay(time);

}//beep();

else if(MorseCode[place][i] == ’\0’){

Word();

Serial.print("Word"); }

//delay(time);

}}

void Speed(int val){ // Connected to the potentiometer to control the speed of the morse output

if((0 <= val) && (val <= 255)){

time=50; }//ms

else if((0 < val) && (val <= 512)){

time=100;}

else if((512 < val) && (val <= 767)){

time=200; }

else{

time=400;}

}

void loop() {

int potValue = analogRead(A0);

Speed(potValue);

if (Serial.available() > 0) {

letter = Serial.read();

letter = toupper(letter);

place = findplace(letter);

Morse2sound();

if(place != 0){ // There is space beetweenletters we run this if statement

beetweenletters(); // to get the timings right.

delay(Unit);

}

16

Page 18: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

Serial.println(LegalChars[place]);

Serial.println(MorseCode[place]);

}

}

B Appendix: Decoder

const int buttonPin = 2;

const int buttonPin2 = 4;// the number of the pushbutton pin

const int ledPin = 9; // the number of the LED pin

float lengd, wait;

float dot=1;

float Rise=0;

float Fall=0;

float Fall1;

float Rise1;

int times[40], index = 0;

//char* dotdash[40];

String dotdash;

String dot1 = ".";

String dash = "-";

int count=0;

//String a;

//char* letter;

const char LegalChars[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const char MorseCode[][8] = {" ",".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",

".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

boolean buttonPushed(){

if(digitalRead(buttonPin)==1){

//Serial.print("buttonPushed");

//Serial.print("\n");

count=count+1;

Rise=millis();

17

Page 19: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

while(digitalRead(buttonPin)==1)

{

}

Fall=millis();

buttonUp();

lengd=Fall-Rise;

return true;

}

else

{

return false;

}

}

boolean buttonUp(){

if(digitalRead(buttonPin)==0){

//Serial.print("buttonUp");

//Serial.print("\n");

Fall1=millis();

while(digitalRead(buttonPin)==0)

{

}

Rise1=millis();

wait=Rise1-Fall1;

return true;

}

else

{

return false;

}

}

void letter(){

for(int i ; i<36 ; i++){

if (dotdash==MorseCode[i]){

Serial.print(LegalChars[i]);

}

}

}

void check_dotdash(){

if (lengd>0 && lengd<2*dot)

18

Page 20: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

dotdash=dotdash + dot1;

else if(lengd>2*dot && lengd<5*dot)

dotdash=dotdash + dash;

Serial.print(dotdash);

Serial.print("\n");

}

void setup() {

Serial.begin(9600);

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

pinMode(buttonPin2, INPUT);

}

void loop(){

if(digitalRead(buttonPin2)){

while(wait< 4*dot){

if(buttonPushed()){

if (count==1){

dot = wait;

}

Serial.print(wait);

Serial.print("\n");

Serial.print(4*dot);

Serial.print("\n");

check_dotdash();

}

}

letter();

wait=0;

count = 0;

dotdash="";

}

}

\section{Appendix: Decoder}

const int buttonPin = 2;

const int buttonPin2 = 4;// the number of the pushbutton pin

const int ledPin = 9; // the number of the LED pin

19

Page 21: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

float lengd,lengd1, wait;

float dot;

float Rise;

float Fall;

String dotdash;

String dot1 = ".";

String dash = "-";

int count=0;

//String a;

//char* letter;

const char LegalChars[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";

const char MorseCode[][8] = {" ",".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",

".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",

".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

boolean buttonPushed(){

if(digitalRead(buttonPin)==1){

//Serial.print("P");

//Serial.print("\n");

count=count+1;

Rise=millis();

dot=Rise-Fall;

while(digitalRead(buttonPin)==1)

{

}

Fall=millis();

lengd=Fall-Rise;

if(count==1){

lengd1=Fall-Rise;

}

return true;

}

else

{

return false;

}

}

void letter(){

20

Page 22: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

for(int i ; i<36 ; i++){

if (dotdash==MorseCode[i]){

Serial.print(LegalChars[i]);

}

}

}

void check_dotdash(){

if (lengd>0 && lengd<2*dot){

dotdash=dotdash + dot1;

}

else if(lengd>2*dot && lengd<5*dot){

dotdash=dotdash + dash;

}

//Serial.print("C");

}

void check_dotdash1(){

if (lengd1>0 && lengd1<2*dot){

dotdash=dotdash + dot1;

}

else if(lengd1>2*dot && lengd1<5*dot){

dotdash=dotdash + dash;

}

}

void setup() {

Serial.begin(9600);

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

// pinMode(buttonPin2, INPUT);

}

void loop(){

if(digitalRead(buttonPin)){

buttonPushed();

if(count==2){

check_dotdash1();

}

if(count>1){

21

Page 23: T-411-MECH Mechatronics 1 Lab 2: Morse codesaethor.weebly.com/uploads/2/0/5/6/20569960/lab4.pdf · T-411-MECH Mechatronics 1 Lab 2: Morse code Árni Þorvaldsson and Sæþór Ólafur

check_dotdash();

}

}

if ((millis()-Fall>1000)&&(count>1)){

letter();

dotdash="";

count=0;

}

}

22


Recommended