0

I am trying to manipulate a project and the simplest way to do what I am trying to do would be to use the existing code. This code is designed to take an input from the serial monitor using serial.read() and manipulate it. For example if I type hello into the serial monitor's command bar, the code would read it and manipulate it. I was wondering if there is any way to create a serial command to run in the code which would act in a way as to bypass the serial monitor since this project is not going to be connected to a computer when it is in use. The command will be the same every time and will be in response to a single switch input.

#include <avr/pgmspace.h>
#include <MorseEnDecoder.h>
#include <stdio.h>

// Pin mapping
const byte morseInPin = 7;      
const byte morseOutPin = 13;
String check = "";
String origin = "S";
const int maxItems = 3;
char myStr[] = "Help is Coming";
int x;
char myArray[maxItems], curChar;
char nextChar;
int i;
int y;

// Instantiate Morse objects
morseDecoder morseInput(morseInPin, MORSE_KEYER, MORSE_ACTIVE_LOW);
morseEncoder morseOutput(morseOutPin);

// Variables dealing with formatting the output somewhat
// by inserting CR's (carriage returns)
long lastTransmissionTime;
long currentTime;
boolean transmissionEnded = true; // Flag to mark old trans finished

// Minimum transmission pause time to insert carriage returns (CR)
// Adjust depending on Morse spd. IE 13 wpm = 646 ms btw words (no CR).
const long transmissionPaused   = 1000; // Suitable for 13 wpm?

void setup()
{
Serial.begin(9600);
Serial.println("Morse EnDecoder Geocache");

// Setting Morse speed in wpm - words per minute
// If not set, 13 wpm is default anyway
morseInput.setspeed(5);
morseOutput.setspeed(5);

lastTransmissionTime = (long)millis();

}


void loop()
{
currentTime = (long)millis();

// Need to call these once per loop
morseInput.decode();
morseOutput.encode();

// SEND MORSE (OUTPUT)
// Encode and send text received from the serial port (serial monitor)
if (Serial.available() && morseOutput.available())
{
// Get character from serial and send as Morse code
char sendMorse = Serial.read();
morseOutput.write(sendMorse);

// Not strictly needed, but used to get morseSignalString before it is      destroyed
// (E.g. for morse training purposes)
morseOutput.encode();

// Also write sent character + Morse code to serial port/monitor
Serial.write(' ');
Serial.write(sendMorse);
// Morse code in morseSignalString is now backwards
for (int y=morseOutput.morseSignals; y>0; y--)
{
  Serial.write(morseOutput.morseSignalString[y-1]);
}
}

// RECEIVE MORSE (INPUT)
// If a character is decoded from the input, write it to serial port
if (morseInput.available())
{

char receivedMorse = morseInput.read();
//Serial.print(receivedMorse);
// A little error checking    
if (receivedMorse == '#') Serial.println("< ERROR:too many morse   signals! >");
morseOutput.write('#');
morseOutput.encode();

//Create Array out of receivedMorse Chars

    curChar = 0;
    nextChar = receivedMorse;
    curChar +=nextChar;
    myArray[i] = curChar;
    check.concat(myArray[i]);
    Serial.println(check);

  //Check input and respond
  if (check == origin) 
  {
Serial.println("check = origin");

// Encode and send text set in myStr[x]

for (x = 0; x < sizeof(myStr) - 1; x++)    
{
char sendMorse = myStr[x];
morseOutput.write(sendMorse);

// Not strictly needed, but used to get morseSignalString before it is destroyed
// (E.g. for morse training purposes)
morseOutput.encode();

// Also write sent character + Morse code to serial port/monitor
Serial.write(' ');
Serial.write(sendMorse);
// Morse code in morseSignalString is now backwards
for (int y=morseOutput.morseSignals; y>0; y--)
{     
Serial.write(morseOutput.morseSignalString[y-1]);
}
}

  }


 if (receivedMorse == '#') Serial.println("< ERROR:too many morse signals! >");
 }
 // two strings not equal:
 if (check != origin) {

 }
 }

This is a morse code encoder and decoder. I have adapted it to do a certain thing when a certain string of morse code is entered. I am trying to get it to then encode and respond with a morse string, but the only way it is programmed to do such a thing is through the serial.read() command There is a library that I am not sure how to attach, it can be found here https://www.dropbox.com/s/eg21qmg0151gxz6/Morse_EnDecoder_2012.11.25.tar-2.gz?dl=0 this also includes all of the original codes. My additions can be found directly under the comment //Create Array out of receivedMorse Chars and continuing to the bottom (The last 45 lines or so not including char, string, and int declarations). I apologize if some of the edits I have made are hideous or "not in the spirit of the language". I am a novice and a hobbyist.

The Code I have adapted will print my string exactly as it appears (myStr[]), however it will not encode it to Morse nor will it output it through the LED or the buzzer. The 'serial.read()' command appears to work outside of my if statement, but really I have no idea how I would fix this.

  • Is the “switch input” (mentioned in last sentence) on your host computer or on an arduino? – James Waldby - jwpat7 Sep 6 '16 at 6:13
  • The switch is an independent input into the arduino. It is not on the computer, it will be built into the project assembly which includes the housing for the arduino board. – user26383 Sep 6 '16 at 16:02
  • Perhaps you can edit your question. (1) Include the “existing code” in the question, using code-formatting to format it; (2) Say what the new program is supposed to do, rather than talking about “bypass the serial monitor”, which seems like a wrong approach; (3) indicate how many positions the switch will have or how many switches there are – James Waldby - jwpat7 Sep 6 '16 at 16:40
  • I have added my code. This code encodes and decodes Morse signals. It will decode a morse input from a switch and output the text to the serial monitor. It will also accept text from the serial monitor and output Morse code with and LED and Buzzer. I have adapted the code to compare the input from the switch with a predetermined string and when they match to do XYZ. In this case I want to output morse code through the LED and Buzzer, but I cannot figure out an alternative to the serial.read() command. There is one switch, it is SPST switch in other words, it has on and off only. – user26383 Sep 6 '16 at 20:20
1

The serial input is read into a variable.

You could comment out the serial read and assign whatever value you need directly to the variable. By commenting out the serial read, you will have a marker of where you made the changes.

This should work since you are saying the inputs are known and do not change.

  • I'm afraid I forgot to mention that the response is several characters long, for example test could be a response or correct. I am able to get the code to output a single character, but not the entire string. I attempted to bypass this with a for loop which would increment a variable assigning it all values in the string, however it seems this did not work. Could this be because the for loop runs entirely on a single loop of the main code? Or does it only loop once per code cycle as well? – user26383 Sep 6 '16 at 16:06
  • More importantly, this section is inside an if statement which is only triggered based on the switch input. I believe this is why the code doesn't work. – user26383 Sep 6 '16 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.