pushbutton

im trying to make a Morse code translator. I need to use a button to start it. It works good with out the button but when i try to hook the there is now many problems with the code. The code bellow is the code with button

//Define the LED Pin
#define PIN_OUT        9
//Define unit length in ms
#define UNIT_LENGTH    250

//Build a struct with the morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
  { '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', "--.." },
  { ' ', "     " }, //Gap between word, seven units 
    
  { '1', ".----" },
  { '2', "..---" },
  { '3', "...--" },
  { '4', "....-" },
  { '5', "....." },
  { '6', "-...." },
  { '7', "--..." },
  { '8', "---.." },
  { '9', "----." },
  { '0', "-----" },
    
  { '.', "·–·–·–" },
  { ',', "--..--" },
  { '?', "..--.." },
  { '!', "-.-.--" },
  { ':', "---..." },
  { ';', "-.-.-." },
  { '(', "-.--." },
  { ')', "-.--.-" },
  { '"', ".-..-." },
  { '@', ".--.-." },
  { '&', ".-..." },
};

const int buttonPin = 2;
int buttonState = 0;
const int ledPin =  A0;

void setup()
{
  pinMode( PIN_OUT, OUTPUT );
  digitalWrite( PIN_OUT, LOW );
  pinMode(buttonPin, INPUT);
  pinMode(A0, INPUT);
  const int buttonPin = 2;
int buttonState = 0;
const int ledPin =  A0;

}

void loop()
{
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

  String morseWord = encode( "1234 " );
    
  for(int i=0; i<=morseWord.length(); i++)
  {
    switch( morseWord[i] )
    {
      case '.': //dit
        digitalWrite( PIN_OUT, HIGH );
        delay( UNIT_LENGTH );
        digitalWrite( PIN_OUT, LOW );
        delay( UNIT_LENGTH );
          
        break;

      case '-': //dah
        digitalWrite( PIN_OUT, HIGH );
        delay( UNIT_LENGTH*3 );
        digitalWrite( PIN_OUT, LOW );
        delay( UNIT_LENGTH );
          
        break;

      case ' ': //gap
      
        delay( UNIT_LENGTH );
    }
  }
}

String encode(const char *string)
{
  size_t i, j;
  String morseWord = "";
  
  for( i = 0; string[i]; ++i )
  {
    for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
    {
      if( toupper(string[i]) == MorseMap[j].letter )
      {
        morseWord += MorseMap[j].code;
        break;
      }
    }
    morseWord += " "; //Add tailing space to seperate the chars
  }

  return morseWord;  
}

String decode(String morse)
{
  String msg = "";
  
  int lastPos = 0;
  int pos = morse.indexOf(' ');
  while( lastPos <= morse.lastIndexOf(' ') )
  {    
    for( int i = 0; i < sizeof MorseMap / sizeof *MorseMap; ++i )
    {
      if( morse.substring(lastPos, pos) == MorseMap[i].code )
      {
        msg += MorseMap[i].letter;
      }
    }

    lastPos = pos+1;
    pos = morse.indexOf(' ', lastPos);
    
    // Handle white-spaces between words (7 spaces)
    while( morse[lastPos] == ' ' && morse[pos+1] == ' ' )
    {
      pos ++;
    }
  }

  return msg;
}
 else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

You need to be more specific than "many problems"...

Also, what is the random mess of indents, statics and never ending else?

tyty1107:
but when i try to hook the

Hook the what?

tyty1107:
there is now many problems with the code.

Explain what the problems are. If you’re getting error messages then post them using code tags.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

After the auto format take a close look at the indentation of your code to see if it matches the intended structure. Do you see a problem?

when the main problem is 'encode' was not declared in this scope. but when i don't use the button it works.

Where does the loop() function end ?

How is the button wired ?

You have your encode funtion in loop().

Count your { and }

Use Ctrl-T to format the code in the IDE.

The loop ends at at else. and the button is wired to A0

tyty1107:
The loop ends at at else.

No, it doesn't.

sorry it ends at return msg;
}

Nope. Guess again.

Where does your 'else' end? Where is its 'if'?

digitalWrite(ledPin, LOW);

const int buttonPin = 2;
int buttonState = 0;
const int ledPin =  A0;

void setup()
{
  pinMode( PIN_OUT, OUTPUT );
  digitalWrite( PIN_OUT, LOW );
  pinMode(buttonPin, INPUT);
  pinMode(A0, INPUT);
//   Why are these here?  They're already written before setup()
//   Why is the pin the button connected to called 'ledPin'?  Why is it on an analog pin?
  const int buttonPin = 2;
  int buttonState = 0;
  const int ledPin =  A0;

}

tyty1107:
digitalWrite(ledPin, LOW);

Think you might need a closing '}'?

Even then, it's in the wrong place.

so hit enter and add a } the end

tyty1107:
so hit enter and add a } the end

Are you telling me I should hit enter and add the }? It's your code, you hit enter and add the }.
It won't fix your problem, but at least the 'else' will have a closing }, even if the else has no business being there.

See reply #5. You CANNOT have a function definition inside of another function (loop()). Count your { and }!

so how to add a button to thing code.

/Define the LED Pin
#define PIN_OUT        9
//Define unit length in ms
#define UNIT_LENGTH    250

//Build a struct with the morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
	{ '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', "--.." },
	{ ' ', "     " }, //Gap between word, seven units 
		
	{ '1', ".----" },
	{ '2', "..---" },
	{ '3', "...--" },
	{ '4', "....-" },
	{ '5', "....." },
	{ '6', "-...." },
	{ '7', "--..." },
	{ '8', "---.." },
	{ '9', "----." },
	{ '0', "-----" },
		
	{ '.', "·–·–·–" },
	{ ',', "--..--" },
	{ '?', "..--.." },
	{ '!', "-.-.--" },
	{ ':', "---..." },
	{ ';', "-.-.-." },
	{ '(', "-.--." },
	{ ')', "-.--.-" },
	{ '"', ".-..-." },
	{ '@', ".--.-." },
	{ '&', ".-..." },
};

void setup()
{
  pinMode( PIN_OUT, OUTPUT );
  digitalWrite( PIN_OUT, LOW );
}

void loop()
{
  String morseWord = encode( "SOS " );
    
  for(int i=0; i<=morseWord.length(); i++)
  {
    switch( morseWord[i] )
    {
      case '.': //dit
        digitalWrite( PIN_OUT, HIGH );
        delay( UNIT_LENGTH );
        digitalWrite( PIN_OUT, LOW );
        delay( UNIT_LENGTH );
          
        break;

      case '-': //dah
        digitalWrite( PIN_OUT, HIGH );
        delay( UNIT_LENGTH*3 );
        digitalWrite( PIN_OUT, LOW );
        delay( UNIT_LENGTH );
          
        break;

      case ' ': //gap
        delay( UNIT_LENGTH );
    }
  }
}

String encode(const char *string)
{
  size_t i, j;
  String morseWord = "";
  
  for( i = 0; string[i]; ++i )
  {
    for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
    {
      if( toupper(string[i]) == MorseMap[j].letter )
      {
        morseWord += MorseMap[j].code;
        break;
      }
    }
    morseWord += " "; //Add tailing space to seperate the chars
  }

  return morseWord;  
}

String decode(String morse)
{
  String msg = "";
  
  int lastPos = 0;
  int pos = morse.indexOf(' ');
  while( lastPos <= morse.lastIndexOf(' ') )
  {    
    for( int i = 0; i < sizeof MorseMap / sizeof *MorseMap; ++i )
    {
      if( morse.substring(lastPos, pos) == MorseMap[i].code )
      {
        msg += MorseMap[i].letter;
      }
    }

    lastPos = pos+1;
    pos = morse.indexOf(' ', lastPos);
    
    // Handle white-spaces between words (7 spaces)
    while( morse[lastPos] == ' ' && morse[pos+1] == ' ' )
    {
      pos ++;
    }
  }

  return msg;
}

In your original code, if you put the else where it was supposed to be and gave loop() a closing }, it would have at least compiled with no errors.

Thats my problem i dont know were the else goes i don't know how to add button i did th button tutorial on the arduino website

const byte buttonPin = 9;


setup()
{
pinMode(buttonPin, INPUT_PULLUP);
}

Your loop() needs a closing } right before you have your encode function definition.
The else (with its { and }) should probably go right before that.

Once you get the button defined, you're going to have to keep track of when it gets pressed, how long it's been pressed, and how long it has been released in order to tell whether its a dot or dash and when the word is finished.