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.
1 Answer 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– user263832016年09月06日 16:06:42 +00:00Commented Sep 6, 2016 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– user263832016年09月06日 16:10:12 +00:00Commented Sep 6, 2016 at 16:10
Explore related questions
See similar questions with these tags.
serial.read()
command. There is one switch, it is SPST switch in other words, it has on and off only.