Button push print string

Hello, me again, having some issues with this code and quite seem to figure it out, I'm still new to coding so bare with me please, and thank you for any help!

//Software details, only edit lines with editme tags
//OTMK Version 1.1.0
//Version date: 3/28/22
//Contributors: Jeremy E
//Software not to be edited by anyone other than OTMK moderators.


//editme
int callSign = ("Test Call Sign");
//editme


const int led = 13;
const int buz = 8;
const int micPin = 9;
const int buttonPin = 3;
const int numButtons = 6;
int buttonState;
int lastButtonState = LOW;
const int button [] = {1, 2, 3, 4, 5, 6};
const int buttonLowRange[] = {127, 175, 315, 510, 930, 1018};
const int buttonHighRange[] = {135, 179, 326, 520, 935, 1023};
int reading = analogRead(buttonPin);
String code = "";
int len = 0;
char ch;
char new_char;
int unit_delay = 250;
long randNumber;

//initialization code


void dot()
{
  Serial.print(".");
  digitalWrite(led, HIGH);
  digitalWrite(buz, HIGH);
  digitalWrite(micPin, HIGH);
  delay(unit_delay);
  digitalWrite(led, LOW);
  digitalWrite(buz, LOW);
  digitalWrite(micPin, LOW);
  delay(unit_delay);
}
void dash()
{
  Serial.print("-");
  digitalWrite(led, HIGH);
  digitalWrite(buz, HIGH);
  digitalWrite(micPin, HIGH);
  delay(unit_delay * 3);
  digitalWrite(led, LOW);
  digitalWrite(buz, LOW);
  digitalWrite(micPin, LOW);
  delay(unit_delay);
}
void A()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void B()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void C()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void D()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void E()
{
  dot();
  delay(unit_delay);
}
void f()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void G()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void H()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void I()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void J()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void K()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void L()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void M()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void N()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void O()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void P()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
}
void Q()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void R()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void S()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void T()
{
  dash();
  delay(unit_delay);
}
void U()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void V()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void W()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void X()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void Y()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void Z()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void one()
{
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void two()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void three()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void four()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void five()
{
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void six()
{
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void seven()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void eight()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void nine()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dot();
  delay(unit_delay);
}
void zero()
{
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
  dash();
  delay(unit_delay);
}
void morse()
{
  if (ch == 'A' || ch == 'a')
  {
    A();
    Serial.print(" ");
  }
  else if (ch == 'B' || ch == 'b')
  {
    B();
    Serial.print(" ");
  }
  else if (ch == 'C' || ch == 'c')
  {
    C();
    Serial.print(" ");
  }
  else if (ch == 'D' || ch == 'd')
  {
    D();
    Serial.print(" ");
  }
  else if (ch == 'E' || ch == 'e')
  {
    E();
    Serial.print(" ");
  }
  else if (ch == 'f' || ch == 'f')
  {
    f();
    Serial.print(" ");
  }
  else if (ch == 'G' || ch == 'g')
  {
    G();
    Serial.print(" ");
  }
  else if (ch == 'H' || ch == 'h')
  {
    H();
    Serial.print(" ");
  }
  else if (ch == 'I' || ch == 'i')
  {
    I();
    Serial.print(" ");
  }
  else if (ch == 'J' || ch == 'j')
  {
    J();
    Serial.print(" ");
  }
  else if (ch == 'K' || ch == 'k')
  {
    K();
    Serial.print(" ");
  }
  else if (ch == 'L' || ch == 'l')
  {
    L();
    Serial.print(" ");
  }
  else if (ch == 'M' || ch == 'm')
  {
    M();
    Serial.print(" ");
  }
  else if (ch == 'N' || ch == 'n')
  {
    N();
    Serial.print(" ");
  }
  else if (ch == 'O' || ch == 'o')
  {
    O();
    Serial.print(" ");
  }
  else if (ch == 'P' || ch == 'p')
  {
    P();
    Serial.print(" ");
  }
  else if (ch == 'Q' || ch == 'q')
  {
    Q();
    Serial.print(" ");
  }
  else if (ch == 'R' || ch == 'r')
  {
    R();
    Serial.print(" ");
  }
  else if (ch == 'S' || ch == 's')
  {
    S();
    Serial.print(" ");
  }
  else if (ch == 'T' || ch == 't')
  {
    T();
    Serial.print(" ");
  }
  else if (ch == 'U' || ch == 'u')
  {
    U();
    Serial.print(" ");
  }
  else if (ch == 'V' || ch == 'v')
  {
    V();
    Serial.print(" ");
  }
  else if (ch == 'W' || ch == 'w')
  {
    W();
    Serial.print(" ");
  }
  else if (ch == 'X' || ch == 'x')
  {
    X();
    Serial.print(" ");
  }
  else if (ch == 'Y' || ch == 'y')
  {
    Y();
    Serial.print(" ");
  }
  else if (ch == 'Z' || ch == 'z')
  {
    Z();
    Serial.print(" ");
  }
  else if (ch == '0')
  {
    zero();
    Serial.print(" ");
  }
  else if (ch == '1')
  {
    one();
    Serial.print(" ");
  }
  else if (ch == '2')
  {
    two();
    Serial.print(" ");
  }
  else if (ch == '3')
  {
    three();
    Serial.print(" ");
  }
  else if (ch == '4')
  {
    four();
    Serial.print(" ");
  }
  else if (ch == '5')
  {
    five();
    Serial.print(" ");
  }
  else if (ch == '6')
  {
    six();
    Serial.print(" ");
  }
  else if (ch == '7')
  {
    seven();
    Serial.print(" ");
  }
  else if (ch == '8')
  {
    eight();
    Serial.print(" ");
  }
  else if (ch == '9')
  {
    nine();
    Serial.print(" ");
  }
  else if (ch == ' ')
  {
    delay(unit_delay * 7);
    Serial.print("/ ");
  }
  else
    Serial.println("Unknown symbol!");
}
void String2Morse()
{
  len = code.length();
  for (int i = 0; i < len; i++)
  {
    ch = code.charAt(i);
    morse();
  }
}
//Hello?
//Dad? Is that you?
//Initialize SWAN Protocall
//Warning
//***entered***4 8 15 16 23 42
//WALT?!
//WALT!


void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(buz, OUTPUT);
  pinMode(micPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.println("Input/Output Ready");
  Serial.println(">:");
}



void loop() {
  { while (Serial.available())

      ///multiplexer position codes below , do not edit



      ///Position 6 = = = manual key / tone
      if ((reading >= 1000) && (reading <= 1023)) {
        Serial.print("manual key tone initiated");
        digitalWrite(led, HIGH);
        digitalWrite(buz, HIGH);
        digitalWrite(led, LOW);
        digitalWrite(buz, LOW);
        buttonState = LOW;

      }
    ///Position 5 = = = SK end of transmission / tone
    {
      if ((reading >= 127) && (reading <= 135)) {
        code = Serial.readString();
        Serial.print(code);
        Serial.print(" = ");
        String2Morse();
        Serial.println("");
        Serial.println("SK");
        buttonState = LOW;
        delay(50);
      }
      ///Position 4 = = =  6 bit OTMK / tone
      {
        if ((reading >= 510) && (reading <= 520)) {
          code = Serial.readString();
          Serial.print(code);
          Serial.print(" = ");
          String2Morse();
          Serial.println("");
          randNumber = random(100001, 999999);
          Serial.println(randNumber);
          buttonState = LOW;
          delay(50);
        }
        ///Position 3 = = =  Call sign / tone
        {
          if ((reading >= 315) && (reading <= 326)) {
            code = Serial.readString();
            Serial.print(code);
            Serial.print(" = ");
            String2Morse();
            Serial.println("");
            Serial.println(callSign);
            buttonState = LOW;
            delay(50);
          }
          ///Position 2 = = = QSL Confirm / tone
          {
            if ((reading >= 315) && (reading <= 326)) {
              code = Serial.readString();
              Serial.print(code);
              Serial.print(" = ");
              String2Morse();
              Serial.println("");
              Serial.println("QSL QSL");
              buttonState = LOW;
              delay(50);
            }

            ///Position = = = 1 CQ CQ CQ Call Sign / tone
            {
              if ((reading >= 315) && (reading <= 326)) {
                code = Serial.readString();
                Serial.print(code);
                Serial.print(" = ");
                String2Morse();
                Serial.println("");
                Serial.println("CQ CQ CQ");
                Serial.println(callSign);
                buttonState = LOW;
                delay(50);
              }

            }
          }
        }
      }
    }
  }
}

Such as?

One issue of course is that there's a lot of repetitiveness in the code, making it laborious to troubleshoot. But technically speaking that can still make a functional program.

You might want to look into the concept of a for-loop. Might be helpful.

and arrays


what's that supposed to do??

1 Like

the term callSign is called upon and is supposed to print the string that is in quote marks "Test Call Sign", this is for Ham radio morse code operation.

Ok, but it's not really an int, is it?

Perhaps I'm incorrect in my understanding of some of these coding terms, Sorry

Oh, it's evident you are, and it's not something to apologize for! You have to start somewhere, and even for those of us where that moment was in the distant past, there's still lots to learn.

'int' stands for 'integer'. If you think of this, then you might recognize why it's odd to have a series of characters masquerading as an integer. Something like this would be more sensible:
char callSign[] = "Test Call Sign";

You realize that this analogRead() will only happen ONE time at initialization?

Also I would recommend changing while (Serial.available()) to if (Serial.available()) and let loop() do the looping.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.