#include <AccelStepper.h>
//alphas A-Z
byte MorseChars[] = {34, 65, 69, 49, 16, 68, 51, 64, 32, 78, 53, 66, 35, 33, 55, 70, 75, 50, 48, 17, 52, 72, 54, 73, 77, 67,
31, 30, 28, 24, 16, 0, 1, 3, 7, 15
};
//digits 0-9
char morseBuff[80];
int mc_state = 0;
int mc_count = 0;
int mc_idx = 0;
int dd_state = 0;
int dd_idx = 0;
int dd_time = 0;
int dot_dur = 200;
int dd_dur = 200;
bool mc_done;
bool dd_done;
bool morse_done = true;
bool loop_morse = false;
unsigned long dd_start;
#define LED_PIN 8
// Define the stepper motor and the pins that is connected to
AccelStepper stepper1(1, 2, 5); // (Typeof driver: with 2 pins, STEP, DIR)
AccelStepper stepper2(1, 3, 6);
byte Speed_flip1 = 0;
byte Speed_flip2 = 0;
void setup() {
Serial.begin(115200);
pinMode(8, OUTPUT);
stepper1.setMaxSpeed(1200); // Set maximum speed value for the stepper
stepper1.setAcceleration(396); // Set acceleration value for the stepper
stepper1.setCurrentPosition(0); // Set the current position to 0 steps
stepper2.setMaxSpeed(1200);
stepper2.setAcceleration(400);
stepper2.setCurrentPosition(0);
stepper1.moveTo(50);
stepper2.moveTo(50);
MorseString("ARDS ARE AWESOME", true);
}
void loop() {
MorseLoop();
stepper1.run();
stepper2.run();
if (stepper1.distanceToGo() == 0) {
if (Speed_flip1==0) stepper1.setAcceleration(400);else stepper1.setAcceleration(396);
Speed_flip1++;
if (Speed_flip1>1)Speed_flip1=0;
stepper1.moveTo(-stepper1.currentPosition());
}
if (stepper2.distanceToGo() == 0) {
if (Speed_flip2==0) stepper2.setAcceleration(396);else stepper2.setAcceleration(400);
Speed_flip2++;
if (Speed_flip2>1)Speed_flip2=0;
stepper2.moveTo(-stepper2.currentPosition());
}
}
void leds_on()
{
digitalWrite(LED_PIN, HIGH);
}
void leds_off()
{
digitalWrite(LED_PIN, LOW);
}
//clear out the old
void ZeroMorseBuff()
{
for (int i = 0; i < sizeof(morseBuff); i++ ) {
morseBuff[i] = 0;
}
}
bool MorseString(char *text, bool looped) {
loop_morse = looped;
bool loaded = false;
if (strlen(text) > 0) {
mc_idx = 0;
mc_done = false;
mc_state = 0;
dd_done = true;
dd_idx = 0;
dd_state = 0;
ZeroMorseBuff();
char ch = *text;
int count = 0;
while (ch != NULL)
{
morseBuff[count] = ch;
count++;
*text++;
ch = *text;
}
loaded = true;
mc_count = count;
morse_done = false;
}
return loaded;
}
void MorseLoop() {
if (!morse_done) {
if (mc_done) {
if (mc_idx < mc_count) {
mc_idx ++;
if (mc_idx < mc_count) {
mc_done = false;
mc_state = 0;
dd_done = true;
dd_idx = 0;
dd_state = 0;
if (isalpha(morseBuff[mc_idx]))
{ if (isupper(morseBuff[mc_idx]))
{
MorseChar(MorseChars[morseBuff[mc_idx] - 'A'], false);
//morse_char(letters[morseBuff[mc_idx] - 'A']);
} else
{
MorseChar(MorseChars[morseBuff[mc_idx] - 'a'], false);
}
} else if (isdigit(morseBuff[mc_idx])) {
MorseChar(MorseChars[morseBuff[mc_idx] - '1' + 27], true);
} else if (morseBuff[mc_idx] == ' ') {
mc_state = 1;
dd_start = millis();
dd_dur = dot_dur * 3;
} else mc_done = true;//skip it
} else {
dd_start = millis();
dd_dur = dot_dur * 6;
morse_done = true;
}
} else {
dd_start = millis();
dd_dur = dot_dur * 6;
morse_done = true;
}
} else
{
if (isalpha(morseBuff[mc_idx]))
{ if (isupper(morseBuff[mc_idx]))
{
MorseChar(MorseChars[morseBuff[mc_idx] - 'A'], false);
//morse_char(letters[morseBuff[mc_idx] - 'A']);
} else
{
MorseChar(MorseChars[morseBuff[mc_idx] - 'a'], false);
}
} else if (isdigit(morseBuff[mc_idx])) {
MorseChar(MorseChars[morseBuff[mc_idx] - '1' + 27], true);
} else if (morseBuff[mc_idx] == ' ') {
MorseChar(MorseChars['B' - 'A'], false);
//morse_char(letters['B' - 'A']);
}
}
} else
{
if (loop_morse) {
if (millis() - dd_start >= dd_dur) {
mc_done = false;
dd_done = true;
dd_idx = 0;
mc_idx = 0;
mc_state = 0;
dd_state = 0;
morse_done = false;
}
}
}
}
//starts a char morsing..
void MorseChar(byte morse_code, bool digit) {
if (mc_state == 0) {
byte dit = GetDit(morse_code, digit);
if (dd_done) {
if (dit != 99) {
if (dd_idx == 0) dd_state = 0;//beginning
dd_done = false;
dd_state = 0;
DitDat(dit);
dd_idx ++;
} else {
// mc_done = true;
dd_idx = 0;
mc_state = 1;//inter char delay
dd_start = millis();
dd_dur = dot_dur * 3;
}
} else {
DitDat(dit);
}
} else { //mc_state = 1 = inter char delay
if (millis() - dd_start >= dd_dur) {
mc_done = true;
}
}
}
byte GetDit(byte morse_code, bool digit) {
byte dit = 99;// all done
//digits are fixed count of 5
int bits = 0;
if (digit) bits = 5 ; else bits = morse_code >> 4;
if (dd_idx == 0) dd_state = 0;//beginning
if (bits > 0 && bits < 6 && dd_idx < bits) {
dit = (morse_code >> dd_idx) & 0x1;
}
return dit;
}
void DitDat(byte dit) {
if (dd_state == 0) {
leds_on();
dd_state = 1;
dd_start = millis();
if (dit == 0) {
dd_dur = dot_dur;
} else {
dd_dur = dot_dur * 3;
}
} else if (dd_state == 1) {
if (millis() - dd_start >= dd_dur) {
dd_state = 2;
leds_off();
dd_start = millis();
dd_dur = dot_dur;
}
} else if (dd_state == 2) {
if (millis() - dd_start >= dd_dur) {
dd_state = 3;
dd_done = true;
}
}
}