IR removed. Global variables use 762 bytes (37%) of dynamic memory, leaving 1,286 bytes for local variables. Maximum is 2,048 bytes.
Lines are all 70 characters now. Characters are still repeated. I will look to find a different random routine in 'C' that the compiler likes.
v i m q y c t , c i ? 4 s s f 9 l k w 3 k 1 e 3 8 e 9 d 8 e q d k k j 2 q p q 2 7 q d p k 2 j k 8 d k 2 j k : : w v 8 w v 8 w v : w v 8 w v
70 characters
" " e k r w r w 3 q 9 8 3 : 9 : e 8 3 : 9 8 e : 9 : k 8 k 8 e 8 9 8 k 8 k 2 e 8 3 2 " 2 k 2 k 8 e : 9 d 9 d 9 d 9 d 9 d 9 d 9 d 9 d " : 9 d
70 characters
In case pin 13 and random are using the same interrupt, I should build the answer string first and then send the characters!
Edit: I did and it did not help to make the run more random.
The code below is before I wrote this paragraph.
Found an article on improving random seed.
Lines are all 70 characters now. Characters are still repeated. I will look to find a different random routine in 'C' that the compiler likes.
v i m q y c t , c i ? 4 s s f 9 l k w 3 k 1 e 3 8 e 9 d 8 e q d k k j 2 q p q 2 7 q d p k 2 j k 8 d k 2 j k : : w v 8 w v 8 w v : w v 8 w v
70 characters
" " e k r w r w 3 q 9 8 3 : 9 : e 8 3 : 9 8 e : 9 : k 8 k 8 e 8 9 8 k 8 k 2 e 8 3 2 " 2 k 2 k 8 e : 9 d 9 d 9 d 9 d 9 d 9 d 9 d 9 d " : 9 d
70 characters
In case pin 13 and random are using the same interrupt, I should build the answer string first and then send the characters!
Edit: I did and it did not help to make the run more random.
The code below is before I wrote this paragraph.
Found an article on improving random seed.
Code:
// send random Morse code
#include <Arduino.h>
byte signalPin = 13; // will control oscillator speaker
unsigned int wpm = 30;
unsigned int elementWait = 1200 / wpm;
unsigned int space = 2;
boolean signal_state = LOW;
void setup() {
pinMode(signalPin, OUTPUT);
Serial.begin(9600);
menu();
}
void loop() {
parser();
}
void menu() {
Serial.println();
Serial.println("Send a random group of characters for Morse practice");
Serial.println();
Serial.println("Press Spacebar [Enter] to start");
Serial.println();
Serial.println("Enter 's' to adjust letter space (1-whatever)");
Serial.println();
Serial.println("Enter w(value) to adjust wpm (Example: w20)");
Serial.println();
}
void parser()
{
unsigned int digits;
unsigned int value = 0;
do
{
digits = Serial.available();
}
while (digits < 1);
char keypress = Serial.read();
do
{
digits = Serial.available();
}
while
(digits < 0);
value = Serial.parseInt();
Serial.flush();
switch (keypress)
{
case ' ': // generate code
{
random_code();
break;
}
case 's': // letter space
{
if (value != 0) space = value;
break;
}
case 'w': // wpm
{
if (value != 0) wpm = value;
elementWait = 1200 / wpm;
break;
}
}
}
void dit() {
digitalWrite(signalPin, HIGH);
delay(elementWait); // milliseconds - one dit
digitalWrite(signalPin, LOW);
delay(elementWait);
}
void dah() {
digitalWrite(signalPin, HIGH);
delay(elementWait * 3);
digitalWrite(signalPin, LOW);
delay(elementWait);
}
void letter_space() {
delay(elementWait * space);
}
void random_code() {
// send a random character's Morse equivlent
char* code_char[] =
{ "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",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
",", ".", "?", "!", ":", "\"", "'", "="
};
char* codes[] =
{ ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", // 26 letters
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", // 10 numbers
"--..--", ".-.-.-", "..--..", "..--.", "---...", ".-..-.", ".----.", "-...-" // 8 punctuation
};
String send_char; String answers;
for (unsigned int t = 0; t < 70 ; t++) // each run will be 70 characters and 69 spaces, one line on my screen
{
randomSeed(analogRead(0));
unsigned int randNumber = random(43); // 0 - 43, all characters
send_char = codes[randNumber];
String x;
for (unsigned int i = 0; i < send_char.length(); i++)
{
x = send_char.substring(i, i + 1);
if (x == ".") dit(); else dah();
}
letter_space();
answers.concat(code_char[randNumber]); answers.concat(" ");
//delay(15);
}
Serial.println(answers);
Serial.print(answers.length() / 2); // remove space between characters from count
Serial.println(" characters");
}
Last edited: