Go Down

Topic: cant work out code (Read 2526 times) previous topic - next topic


Jun 26, 2012, 03:54 pm Last Edit: Jun 26, 2012, 03:58 pm by spruce_m00se Reason: 1
I am using this code
Code: [Select]
              MORSE ENDECODER

 Morse encoder / decoder classes for the Arduino.

 This example decodes Morse signals present on digital input 7.
 (active low, and then also using the internal pullup resistor)

 It also encodes Morse sent via the serial interface to the Arduino, on
 digital output pin 13.
 It can also decode audible signals, if using the constant MORSE_AUDIO
 instead of MORSE_KEYER, but then it is important to note that the
 input pin nr. will be for ANALOG inputs (0-5 on Atmega 168 - 328),
 and not the digital inputs.

 Copyright (C) 2010 raron


#include <MorseEnDecoder.h>

int morseInPin = 4;      
int morseOutPin = 6;
int x = 0;
int y = 0;

morseDecoder morseInput(morseInPin, MORSE_KEYER, MORSE_ACTIVE_LOW);
morseEncoder morseOutput(morseOutPin);

#include <LiquidCrystal.h>                  //include lcd library

LiquidCrystal lcd(11, 12, 7, 8, 9,10);

void setup()

 lcd.begin(16, 2);
 Serial.println("Morse EnDecoder demo");
 // Setting Morse speed in wpm - words per minute
 // If not set, 13 wpm is default

void loop()
 // Needs to call these once per loop

 // Morse output:
 // Encoding text received from the serial input
 if (Serial.available() && morseOutput.available())
   char sendMorse = Serial.read();
   //String code=Serial.read();
   if(x  <=15){
   // Also write it back to serial
   morseOutput.encode();    // This is just to get morseSignalString before it is destroyed
   Serial.write(' ');
   lcd.print (sendMorse);
            char sendMorse = Serial.read();
               // Also write it back to serial
               morseOutput.encode();    // This is just to get morseSignalString before it is destroyed
               Serial.write(' ');
               lcd.print (sendMorse);
                     if((x>=16) && (y >=16)){
 // Morse input:
 // If a character is decoded from the input, write it to serial output
 if (morseInput.available())
   char receivedMorse = morseInput.read();
   // A little error checking    
   if (receivedMorse == '#') Serial.println("< ERROR:too many morse signals! >");

 // Morse feedback from input if not sending Morse.
 if (morseOutput.available()) digitalWrite(morseOutPin, morseInput.morseSignalState);

the library it calls looks like this
Code: [Select]

#ifndef MorseEnDecoder_H
#define MorseEnDecoder_H

#if (ARDUINO <  100)
#include <WProgram.h>
#include <Arduino.h>

#define MORSE_AUDIO true
#define MORSE_KEYER false
#define MORSE_ACTIVE_LOW true
#define MORSE_ACTIVE_HIGH false

class morseDecoder
   morseDecoder(int decodePin, boolean listenAudio, boolean morsePullup);
   void decode();
   void setspeed(int value);
   char read();
   boolean available();
   int AudioThreshold;
   long debounceDelay;     // the debounce time. Keep well below dotTime!!
   boolean morseSignalState;  
   int morseInPin;         // The Morse input pin
   int audioSignal;
   int morseTableJumper;
   int morseTablePointer;
   int wpm;                // Word-per-minute speed
   long dotTime;           // morse dot time length in ms
   long dashTime;
   long wordSpace;
   boolean morseSpace;     // Flag to prevent multiple received spaces
   boolean gotLastSig;     // Flag that the last received morse signal is decoded as dot or dash
   boolean morseKeyer;
   boolean lastKeyerState;
   boolean morseAudio;
   boolean activeLow;
   long markTime;          // timers for mark and space in morse signal
   long spaceTime;         // E=MC^2 ;p
   long lastDebounceTime;  // the last time the input pin was toggled
   long currentTime;       // The current (signed) time
   char decodedMorseChar;  // The last decoded Morse character

class morseEncoder
   morseEncoder(int encodePin);
   void encode();
   void setspeed(int value);
   void write(char temp);
   boolean available();
   char morseSignalString[7];// Morse signal for one character as temporary ASCII string of dots and dashes
   char encodeMorseChar;   // ASCII character to encode
   int morseOutPin;
   boolean sendingMorse;
   int wpm;                // Word-per-minute speed
   long dotTime;           // morse dot time length in ms
   long dashTime;
   long wordSpace;
   int morseSignals;       // nr of morse signals to send in one morse character
   int morseSignalPos;
   int sendingMorseSignalNr;
   long sendMorseTimer;
   long lastDebounceTime;
   long currentTime;


in the guys demo of the code he has a buzzer and LED connected to the morse output , which buzz and light according to the morse code,
my buzzer doesnt have an internal tone generator,

I am hoping to use the tone() function
like this,
to create a tone on the buzzer at the same time that the led lights, but i cant for the life of me find the part of code that lights the led,

i then assume that i would need a noTone(6) to stop it in the relevant place aswell, but im stumped,

any ideas?


There should be a file MorseEnDecoder.cpp that contains the actual class code (you just posted the header file for the library).  I would look in either the encode or write methods of the Encoder class.  Seach for

morseEncoder::encode or morseEncoder::write


it wont let me post the otehr file you mention ,

but i looked in it and tried including the tone() after i saw the pin 6 put HIGH and notone() when its put low, but it didnt do the job ,

i will look a bit more later


There is a limit to how long the text of a post can be, which is why it likely didn't let you post the other file.  In those situations you can attach the files, including images, etc. using the Additional Options drop down when you enter messages.


okay, here it is


i opened it in wordpad, added the code where i think i need it and saved it, so lets see what happens,

i think the reason it may have not worked last time is because i edited the copy on the usb drive by mistake, not the copy in the libraries folder for arduino



there is some positive change and some negative change that has been made,

the positive is that the code now works and produces the tone for the first 16 characters.

the negative is that now, when the character length goes over 16 it starts to drop characters ,

my modifications to the code make it drop the cursor to the bottom line when x(the character count) reaches 16, then it increments y(the bottom line cahracter count) with each character printed,

im guessing that this isnt the cause as it works well without the beeping function, but when beeping is added it doesnt,

so i have two options,
1)limit messages to 16 characters,
2) sort it out,



sorted, apparently a small difference between the code under the conditions for the first line and second makes a big difference when the beeps are added,

so although it wasnt a problem without beeps it had to be changed,

now i have a good beeping board that prints the text out to serial and lcd,

it does get a bit confused with long strings though, especially when they are all very similar morse codes, and it seems to get out of sync with the serial every now and then as a result,

now to take the buzzer output and put it on a mic input and see if it comes out of speakers,


so far so good,

the morse code is transmitted to the other walkie talkie and comes out of the speaker,

now time to replicate the arduino and see if the recevier walkie talkie can decode what it receives,

its going well,

for anyone interested, i made a basic attenuation circuit between the tone() pin and ground and the mic input and ground, using some old resistors out of a power supply, (result!!)

Go Up