Else without previous if error

Hi all,

In this code,when I add ‘tone(audioPin, ToneFreq) ;’ (line 76) I get an error message ‘else’ without previous 'if'.
Without this line, the soft is fully operating.
I just want have two output at the same time : a LED and a buzzer.

ERROR MESSAGE :
Arduino : 1.6.4 (Windows 7), Carte : "Arduino Uno"
Hellschreiber_2.ino: In function 'void encodechar(int)':
Hellschreiber_2.ino:76:11: error: 'else' without a previous 'if'

Thank you for your help,

Pierre

int radioPin = 13 ; // LED
int audioPin = 12 ; // HP
int ToneFreq = 800 ; // Beep frequency

typedef struct glyph {
  char ch ;
  word col[7] ;
} Glyph ;

const Glyph glyphtab[] PROGMEM = {
  {' ', {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'A', {0x07fc, 0x0e60, 0x0c60, 0x0e60, 0x07fc, 0x0000, 0x0000}},
  {'B', {0x0c0c, 0x0ffc, 0x0ccc, 0x0ccc, 0x0738, 0x0000, 0x0000}},
  {'C', {0x0ffc, 0x0c0c, 0x0c0c, 0x0c0c, 0x0c0c, 0x0000, 0x0000}},
  {'D', {0x0c0c, 0x0ffc, 0x0c0c, 0x0c0c, 0x07f8, 0x0000, 0x0000}},
  {'E', {0x0ffc, 0x0ccc, 0x0ccc, 0x0c0c, 0x0c0c, 0x0000, 0x0000}},
  {'F', {0x0ffc, 0x0cc0, 0x0cc0, 0x0c00, 0x0c00, 0x0000, 0x0000}},
  {'G', {0x0ffc, 0x0c0c, 0x0c0c, 0x0ccc, 0x0cfc, 0x0000, 0x0000}},
  {'H', {0x0ffc, 0x00c0, 0x00c0, 0x00c0, 0x0ffc, 0x0000, 0x0000}},
  {'I', {0x0ffc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'J', {0x003c, 0x000c, 0x000c, 0x000c, 0x0ffc, 0x0000, 0x0000}},
  {'K', {0x0ffc, 0x00c0, 0x00e0, 0x0330, 0x0e1c, 0x0000, 0x0000}},
  {'L', {0x0ffc, 0x000c, 0x000c, 0x000c, 0x000c, 0x0000, 0x0000}},
  {'M', {0x0ffc, 0x0600, 0x0300, 0x0600, 0x0ffc, 0x0000, 0x0000}},
  {'N', {0x0ffc, 0x0700, 0x01c0, 0x0070, 0x0ffc, 0x0000, 0x0000}},
  {'O', {0x0ffc, 0x0c0c, 0x0c0c, 0x0c0c, 0x0ffc, 0x0000, 0x0000}},
  {'P', {0x0c0c, 0x0ffc, 0x0ccc, 0x0cc0, 0x0780, 0x0000, 0x0000}},
  {'Q', {0x0ffc, 0x0c0c, 0x0c3c, 0x0ffc, 0x000f, 0x0000, 0x0000}},
  {'R', {0x0ffc, 0x0cc0, 0x0cc0, 0x0cf0, 0x079c, 0x0000, 0x0000}},
  {'S', {0x078c, 0x0ccc, 0x0ccc, 0x0ccc, 0x0c78, 0x0000, 0x0000}},
  {'T', {0x0c00, 0x0c00, 0x0ffc, 0x0c00, 0x0c00, 0x0000, 0x0000}},
  {'U', {0x0ff8, 0x000c, 0x000c, 0x000c, 0x0ff8, 0x0000, 0x0000}},
  {'V', {0x0ffc, 0x0038, 0x00e0, 0x0380, 0x0e00, 0x0000, 0x0000}},
  {'W', {0x0ff8, 0x000c, 0x00f8, 0x000c, 0x0ff8, 0x0000, 0x0000}},
  {'X', {0x0e1c, 0x0330, 0x01e0, 0x0330, 0x0e1c, 0x0000, 0x0000}},
  {'Y', {0x0e00, 0x0380, 0x00fc, 0x0380, 0x0e00, 0x0000, 0x0000}},
  {'Z', {0x0c1c, 0x0c7c, 0x0ccc, 0x0f8c, 0x0e0c, 0x0000, 0x0000}},
  {'0', {0x07f8, 0x0c0c, 0x0c0c, 0x0c0c, 0x07f8, 0x0000, 0x0000}},
  {'1', {0x0300, 0x0600, 0x0ffc, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'2', {0x061c, 0x0c3c, 0x0ccc, 0x078c, 0x000c, 0x0000, 0x0000}},
  {'3', {0x0006, 0x1806, 0x198c, 0x1f98, 0x00f0, 0x0000, 0x0000}},
  {'4', {0x1fe0, 0x0060, 0x0060, 0x0ffc, 0x0060, 0x0000, 0x0000}},
  {'5', {0x000c, 0x000c, 0x1f8c, 0x1998, 0x18f0, 0x0000, 0x0000}},
  {'6', {0x07fc, 0x0c66, 0x18c6, 0x00c6, 0x007c, 0x0000, 0x0000}},
  {'7', {0x181c, 0x1870, 0x19c0, 0x1f00, 0x1c00, 0x0000, 0x0000}},
  {'8', {0x0f3c, 0x19e6, 0x18c6, 0x19e6, 0x0f3c, 0x0000, 0x0000}},
  {'9', {0x0f80, 0x18c6, 0x18cc, 0x1818, 0x0ff0, 0x0000, 0x0000}},
  {'*', {0x018c, 0x0198, 0x0ff0, 0x0198, 0x018c, 0x0000, 0x0000}},
  {'.', {0x001c, 0x001c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'?', {0x1800, 0x1800, 0x19ce, 0x1f00, 0x0000, 0x0000, 0x0000}},
  {'!', {0x1f9c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'(', {0x01e0, 0x0738, 0x1c0e, 0x0000, 0x0000, 0x0000, 0x0000}},
  {')', {0x1c0e, 0x0738, 0x01e0, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'#', {0x0330, 0x0ffc, 0x0330, 0x0ffc, 0x0330, 0x0000, 0x0000}},
  {'

, {0x078c, 0x0ccc, 0x1ffe, 0x0ccc, 0x0c78, 0x0000, 0x0000}},
  {'/', {0x001c, 0x0070, 0x01c0, 0x0700, 0x1c00, 0x0000, 0x0000}},
} ;

#define NGLYPHS        (sizeof(glyphtab)/sizeof(glyphtab[0]))

void
encodechar(int ch)
{
  int i, x, y, fch ;
  word fbits ;

for (i = 0; i < NGLYPHS; i++) {
    fch = pgm_read_byte(&glyphtab[i].ch) ;
    if (fch == ch) {
      for (x = 0; x < 7; x++) {
        fbits = pgm_read_word(&(glyphtab[i].col[x])) ;
        for (y = 0; y < 14; y++) {
          if (fbits & (1 << y))
            digitalWrite(radioPin, HIGH) ; 
            tone(audioPin, ToneFreq) ;    // Line 76 (this line generates error)
          else
            digitalWrite(radioPin, LOW) ;
            noTone(audioPin) ;

delayMicroseconds(4045L) ;
        }
      }
    }
  }
}

void
encode(char *ch)
{
  while (*ch != '\0')
    encodechar(*ch++) ;
}

void
setup()
{
  pinMode(radioPin, OUTPUT) ;
  pinMode(audioPin, OUTPUT) ;
}

void
loop()
{
  encode("ABCDEFG........") ;
}

  for (i = 0; i < NGLYPHS; i++) { // <<<<<<<<<< You used braces

    fch = pgm_read_byte(&glyphtab[i].ch) ;

    if (fch == ch) { // <<<<<<<<<< You used braces

      for (x = 0; x < 7; x++) { // <<<<<<<<<< You used braces

        fbits = pgm_read_word(&(glyphtab[i].col[x])) ;

        for (y = 0; y < 14; y++) { // <<<<<<<<<< You used braces

          if (fbits & (1 << y)) // <<<<<<<<<< NO BRACES?

            digitalWrite(radioPin, HIGH) ; 
            tone(audioPin, ToneFreq) ;     // Line 76 (this line generates error)
          else
            digitalWrite(radioPin, LOW) ;
            noTone(audioPin) ;

...

Thank you very much for fast reply !

Yes I forgot two braces, an opening one after if (fbits & (1 << y)) and an ending one after tone(audioPin, ToneFreq) ;

I can now compile but I get a sizzling sound in the HP whereas if I delete the LED lighting instructions, the beep sound is pure.

Is another way to get a 800Hz beep during the LED lighting ?

Pierre

... I get a sizzling sound in the HP...

"HP"? As in "HP computer"?

If yes then you need to stop immediately, remove power from your board, then fix the hardware problem.

HP is Haut-Parleur in french but I'm using a piezzo buzzer !

Calling tone multiple times in quick succession interrupts sound generation which can cause things like a sizzling sound. It looks like tone / noTone can be called approximately every 4ms which is certainly too quickly for an 800 Hz tone. I suspect you will have to introduce a state machine / delay to solve the problem.

It's a facsimile-based radio communication mode called Hellschreiber used by the Germans during the Second World War.

The pulses last for 8 ms but what I don't understand is that when I remove the two instructions to illuminate the LED, the sound is clear and loud but with the LED working the sound is low and almost inaudible.

Erratum : You are right, pulse 4 ms.

Hi,
What LEDs do you have connected to the arduino and how are you powering them?
If you disconnect the LED but still run the LED instructions, does the speaker hiss?

Tom.... :slight_smile:

Hi Tom,

Red LED is in serial with an optocoupler and a 100 ohms resistor.
With or without LED I've a faint sizzling sound but if I remove (//) the two LED instructions in the sketch, the tone is clear and loud.

I think I'll use a XR2206 for generating the tone. I have tried this and it works very well. I wanted still understand this mystery.

This would be a good time to post your new code.

Hi,

This sketch was found on the Web and I try to add a buzzer without success.
So the pin 13 is now connected to a XR2206, a dedicated integrated circuit to produce a sine wave.

The full code with // to remove the audio option that don't work :

//Hellschreiber is normally defined as sending characters
//defined on a 7x7 matrix, at 122.5 dots (2.5 characters) per second.
//But the actual font is actually defined on a 7x14 matrix.
//To keep the bandwidth of the signal down, the font doesn't ever define
//a character that requires turning single dots on or off:
//the minimum signal changes are two dots long.
//These "half dot" are sent at 245 baud, or about 4.08ms per dot.
//Because I needed to account for the time spent looking up the character,
//I tuned that down to about 4.045ms. I was concerned that because
//I was keying the oscillator on and off, the startup time (which I
//estimated at about 2ms) could be a problem, but I suspect the shutdown
//time is about 2ms as well, so the overall system works better than
//you might imagine. The startup and shutdown keying waveforms are a bit
//erratic though, and the bandwidth of the signal is probably too wide.
//I think a better way to do this would be to build an oscillator that
//runs continuously, and then key a buffer amp with a filtered pulse
//to keep the bandwidth low. But for a 500 microwatt transmitter
//(estimated, and represents power going into the antenna, not radiated)
//it probably works just fine.

int radioPin = 13 ; // Optocoupleur + LED + 100 ohms to ground
//int audioPin = 12 ; // Buzzer
//int ToneFreq = 800 ;

typedef struct glyph {
  char ch ;
  word col[7] ;
} Glyph ;

const Glyph glyphtab[] PROGMEM = {
  {' ', {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'A', {0x07fc, 0x0e60, 0x0c60, 0x0e60, 0x07fc, 0x0000, 0x0000}},
  {'B', {0x0c0c, 0x0ffc, 0x0ccc, 0x0ccc, 0x0738, 0x0000, 0x0000}},
  {'C', {0x0ffc, 0x0c0c, 0x0c0c, 0x0c0c, 0x0c0c, 0x0000, 0x0000}},
  {'D', {0x0c0c, 0x0ffc, 0x0c0c, 0x0c0c, 0x07f8, 0x0000, 0x0000}},
  {'E', {0x0ffc, 0x0ccc, 0x0ccc, 0x0c0c, 0x0c0c, 0x0000, 0x0000}},
  {'F', {0x0ffc, 0x0cc0, 0x0cc0, 0x0c00, 0x0c00, 0x0000, 0x0000}},
  {'G', {0x0ffc, 0x0c0c, 0x0c0c, 0x0ccc, 0x0cfc, 0x0000, 0x0000}},
  {'H', {0x0ffc, 0x00c0, 0x00c0, 0x00c0, 0x0ffc, 0x0000, 0x0000}},
  {'I', {0x0ffc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'J', {0x003c, 0x000c, 0x000c, 0x000c, 0x0ffc, 0x0000, 0x0000}},
  {'K', {0x0ffc, 0x00c0, 0x00e0, 0x0330, 0x0e1c, 0x0000, 0x0000}},
  {'L', {0x0ffc, 0x000c, 0x000c, 0x000c, 0x000c, 0x0000, 0x0000}},
  {'M', {0x0ffc, 0x0600, 0x0300, 0x0600, 0x0ffc, 0x0000, 0x0000}},
  {'N', {0x0ffc, 0x0700, 0x01c0, 0x0070, 0x0ffc, 0x0000, 0x0000}},
  {'O', {0x0ffc, 0x0c0c, 0x0c0c, 0x0c0c, 0x0ffc, 0x0000, 0x0000}},
  {'P', {0x0c0c, 0x0ffc, 0x0ccc, 0x0cc0, 0x0780, 0x0000, 0x0000}},
  {'Q', {0x0ffc, 0x0c0c, 0x0c3c, 0x0ffc, 0x000f, 0x0000, 0x0000}},
  {'R', {0x0ffc, 0x0cc0, 0x0cc0, 0x0cf0, 0x079c, 0x0000, 0x0000}},
  {'S', {0x078c, 0x0ccc, 0x0ccc, 0x0ccc, 0x0c78, 0x0000, 0x0000}},
  {'T', {0x0c00, 0x0c00, 0x0ffc, 0x0c00, 0x0c00, 0x0000, 0x0000}},
  {'U', {0x0ff8, 0x000c, 0x000c, 0x000c, 0x0ff8, 0x0000, 0x0000}},
  {'V', {0x0ffc, 0x0038, 0x00e0, 0x0380, 0x0e00, 0x0000, 0x0000}},
  {'W', {0x0ff8, 0x000c, 0x00f8, 0x000c, 0x0ff8, 0x0000, 0x0000}},
  {'X', {0x0e1c, 0x0330, 0x01e0, 0x0330, 0x0e1c, 0x0000, 0x0000}},
  {'Y', {0x0e00, 0x0380, 0x00fc, 0x0380, 0x0e00, 0x0000, 0x0000}},
  {'Z', {0x0c1c, 0x0c7c, 0x0ccc, 0x0f8c, 0x0e0c, 0x0000, 0x0000}},
  {'0', {0x07f8, 0x0c0c, 0x0c0c, 0x0c0c, 0x07f8, 0x0000, 0x0000}},
  {'1', {0x0300, 0x0600, 0x0ffc, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'2', {0x061c, 0x0c3c, 0x0ccc, 0x078c, 0x000c, 0x0000, 0x0000}},
  {'3', {0x0006, 0x1806, 0x198c, 0x1f98, 0x00f0, 0x0000, 0x0000}},
  {'4', {0x1fe0, 0x0060, 0x0060, 0x0ffc, 0x0060, 0x0000, 0x0000}},
  {'5', {0x000c, 0x000c, 0x1f8c, 0x1998, 0x18f0, 0x0000, 0x0000}},
  {'6', {0x07fc, 0x0c66, 0x18c6, 0x00c6, 0x007c, 0x0000, 0x0000}},
  {'7', {0x181c, 0x1870, 0x19c0, 0x1f00, 0x1c00, 0x0000, 0x0000}},
  {'8', {0x0f3c, 0x19e6, 0x18c6, 0x19e6, 0x0f3c, 0x0000, 0x0000}},
  {'9', {0x0f80, 0x18c6, 0x18cc, 0x1818, 0x0ff0, 0x0000, 0x0000}},
  {'*', {0x018c, 0x0198, 0x0ff0, 0x0198, 0x018c, 0x0000, 0x0000}},
  {'.', {0x001c, 0x001c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'?', {0x1800, 0x1800, 0x19ce, 0x1f00, 0x0000, 0x0000, 0x0000}},
  {'!', {0x1f9c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'(', {0x01e0, 0x0738, 0x1c0e, 0x0000, 0x0000, 0x0000, 0x0000}},
  {')', {0x1c0e, 0x0738, 0x01e0, 0x0000, 0x0000, 0x0000, 0x0000}},
  {'#', {0x0330, 0x0ffc, 0x0330, 0x0ffc, 0x0330, 0x0000, 0x0000}},
  {'

Regards,
Pierre, {0x078c, 0x0ccc, 0x1ffe, 0x0ccc, 0x0c78, 0x0000, 0x0000}},
  {'/', {0x001c, 0x0070, 0x01c0, 0x0700, 0x1c00, 0x0000, 0x0000}},
} ;

#define NGLYPHS        (sizeof(glyphtab)/sizeof(glyphtab[0]))

void
encodechar(int ch)
{
  int i, x, y, fch ;
  word fbits ;

/* It looks sloppy to continue searching even after you've
  * found the letter you are looking for, but it makes the
  * timing more deterministic, which will make tuning the
  * exact timing a bit simpler.
  */
  for (i = 0; i < NGLYPHS; i++) {
    fch = pgm_read_byte(&glyphtab[i].ch) ;
    if (fch == ch) {
      for (x = 0; x < 7; x++) {
        fbits = pgm_read_word(&(glyphtab[i].col[x])) ;
        for (y = 0; y < 14; y++) {
          if (fbits & (1 << y)) {
            digitalWrite(radioPin, HIGH) ; }
            //tone(audioPin, ToneFreq) ; }
          else
            digitalWrite(radioPin, LOW) ;
            //noTone(audioPin) ;

delayMicroseconds(4045L) ;
        }
      }
    }
  }
}

void
encode(char *ch)
{
  while (*ch != '\0')
    encodechar(*ch++) ;
}

void
setup()
{
  pinMode(radioPin, OUTPUT) ;
  //pinMode(audioPin, OUTPUT) ;
}

void
loop()
{
  encode("7 MHZ FELD HELLSCHREIBER BEACON . . . . ") ;
}


Regards,
Pierre

You are still missing a brace pair. I'm going to also add space and remove the comments to make the change clear...

...
        for (y = 0; y < 14; y++) 
        {
          if (fbits & (1 << y)) 
          {
            digitalWrite(radioPin, HIGH) ;
            tone(audioPin, ToneFreq) ; 
          }
          else
          {
            digitalWrite(radioPin, LOW) ;
            noTone(audioPin) ;
          }

          delayMicroseconds(4045L) ;
        }
...

I assume that is how the code is supposed to be structured based on your indentation.

Thank you for your help, everything works successfully, LED and clear audio in the same time.
I've still some difficulties with the opening and closing braces.
Perhaps for readability, it's better to put on a new line despite more lines in the code.
Regards,
Pierre

Lines are cheap. They don't slow things down, and they improve readability.

HI,
Yes, it's a good message and a good lesson !