I can't find what is wrong in my code (newbie)

Hello.

I’m trying to send an SOS on a LED wired on N°4 PIn.
It has to send the different letter as serial binary data, LSB fisrt.
Before each letter is a start bit (0)
After each letter is 2 stop bit (0.0)
For S,(01010011) it has to send :
0 (startbit), 1, 1, 0 , 0, 1, 0, 1, 0, (then 2 stopbit), 0, 0.

I made this code that gives me error, but I can’t finf the reason.

enum Lettre {s, o};
char tabLettre [] = {'S', 'O'};
char Lettre;
long dureeBit = 333;
#define L1 4
#define L1On digitalWrite (L1, 1);
#define L1Off digitalWrite (L1, 0);

 void Zero () {
  L1On;
  //delayMicroseconds (dureeBit);
  delay(dureeBit);
    }
  
  void Un () {
  L1Off;
  //delayMicroseconds (dureeBit);
  delay(dureeBit);
    }
    
  void Start () {
    L1Off;
    //delayMicroseconds (dureeBit);
    delay(dureeBit);
  }
      
  void Stop () {
    L1Off;
      for (int i=0; i<2; i++) {
      //delayMicroseconds (dureeBit);
      delay(dureeBit);
      }
  }
  
  void Caractere (Lettre){
      Zero();
      for (int i=0; i<8; i++) {
          //if (tabLettre [Lettre] & 1 << i) {
            if (Lettre & 1 << i) {
            Zero ();
            }
            else {
            Un ();
            }
        }
        Stop();
  }
    
  void sequence () {
  
    Caractere (s);
    
    Caractere (o);
    
    Caractere (s);
  
    Zero();
  
  }
 

    
void setup() {
  // put your setup code here, to run once:
  pinMode (L1,1);
  Zero();
  
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  /*if (Serial.available () > 0) {
  Lettre = Serial.read ();
  Serial.print ("Lettre est = "); Serial.print (Lettre);
  Serial.print ("    BINaire est ") ; Serial.println (Lettre, BIN );
  }
  */
  
sequence();

delay(1000);
}

Any help to solve this “error: variable or field ‘Caractere’ declared void”?

It's a prototype error courtesy of the behind the scenes work the IDE does for you.

This change in your Caractere function definition will make it compile at least:

  void Caractere (char Lettre)

hary:

  void Caractere (Lettre){

Zero();
      for (int i=0; i<8; i++) {
          //if (tabLettre [Lettre] & 1 << i) {
            if (Lettre & 1 << i) {
            Zero ();
            }
            else {
            Un ();
            }
        }
        Stop();
  }

You are calling Zero() when the bit is 1 and Un() when the bit is 0.

It looks like you took out the code that uses tabLettre. That would be a mistake. If you want to pass the character directly, rather than as an index into a table, try:

    Caractere ('S');
    Caractere ('O');
    Caractere ('S');

Start bits are usually 1 so you can tell them from the stop bits.

There are several things wrong with your code to begin with. First, preprocessor directives do not have semicolons at the end of the statement line and they are usually placed before other definitions in the file:

#define L1 4
#define L1On digitalWrite (L1, 1)      // Note semicolons removed
#define L1Off digitalWrite (L1, 0)

Next, you define an enum named Lettre and two lines later define a char variable with the same name.

enum Lettre {s, o};
char tabLettre [] = {'S', 'O'};
char Lettre;

The compiler is not going to like that.

Then, you define a function named Caractere() and you pass in the data type specifier (i.e., the enum Lettre) or a char variable (char Lettre), but the compiler doesn't know whether it's the char or the enum because the function signature is incomplete.

  void Caractere (Lettre){

This should be one of the following:

  void Caractere (Lettre myEnum){      // You are using the enum
  void Caractere (char Lettre){      // You are using a char

If this is to be Morse Code, where the letter S is three dits (...), this seems like a hard way to do it. Regardless, you need to fix up several errors before you can proceed further. Finally, after you've removed all compiler errors, you can place the cursor in the IDE's source code window, press Ctrl-t, and the IDE will format your code using the standard C format conventions. This will make it easier for the rest of us to read it.

Many thanks for helping.

I'll try to work with these information and come back later if needed.

Thanks to you all.

Sorry but I don’t understand the “myEnum”

void Caractere (Lettre myEnum)

I have never mentioned this before.
Sorry for being so stupid but I’m very new at using array and enum !

wildbill:
It’s a prototype error courtesy of the behind the scenes work the IDE does for you.

When I’ve done this without changing anything else, it worked ! :
This change in your Caractere function definition will make it compile at least:

  void Caractere (char Lettre)

But I removed the “char Letter” because I think I don’t need it and now I got the same error as before.

The serial data protocole I’m using is very personal. It is sending on the pin N°4 the letter as ASCII code in binary S = 01010011, LSB and before the letter 1 Starbit, after the letter 2 Stopbit.

#define L1 4
#define L1On digitalWrite (L1, 1)
#define L1Off digitalWrite (L1, 0)

enum Lettre {
  s, o};
char tabLettre [] = {
  'S', 'O'};

long dureeBit = 3330;


void Zero () {
  L1On;
  delayMicroseconds (dureeBit);
  //delay(dureeBit);
}

void Un () {
  L1Off;
  delayMicroseconds (dureeBit);
  //delay(dureeBit);
}

void Start () {
  L1Off;
  delayMicroseconds (dureeBit);
  //delay(dureeBit);
}

void Stop () {
  L1Off;
  for (int i=0; i<2; i++) {
    delayMicroseconds (dureeBit);
    //delay(dureeBit);
  }
}

void Caractere ( Lettre){
  Zero();
  for (int i=0; i<8; i++) {
    //if (tabLettre [Lettre] & 1 << i) {
    if (Lettre & 1 << i) {
      Zero ();
    }
    else {
      Un ();
    }
  }
  Stop();
}

void sequence () {
  Caractere (s);
  Caractere (o);
  Caractere (s);
  Zero();
}



void setup() {
  // put your setup code here, to run once:
  pinMode (L1,1);
  Zero();

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  /*if (Serial.available () > 0) {
   Lettre = Serial.read ();
   Serial.print ("Lettre est = "); Serial.print (Lettre);
   Serial.print ("    BINaire est ") ; Serial.println (Lettre, BIN );
   }
   */

  sequence();

  delay(1000);
}

When you pass a variable to a function, you must use the following form:

return_data_type_specifier   function_name(data_type_being_passed_in   variable_name) {

You are not passing in the name of a variable, You are using the form:

void Caractere ( Lettre  ??????){

What is the name of the variable you are passing in? What is the name of "??????"?

Try this:

const int L1 = 4;

const unsigned long dureeBit = 3330;  // 3.33 milliseconds in microseconds

void L1On() {
  digitalWrite (L1, HIGH);
}

void L1Off() {
  digitalWrite (L1, LOW);
}

void Un () {
  L1On();
  delayMicroseconds (dureeBit);
}

void Zero () {
  L1Off();
  delayMicroseconds (dureeBit);
  //delay(dureeBit);
}

void Caractere (char lettre){
  Un();  // Start bit
  for (int i=0; i<8; i++) {
    if (lettre & (1 << i))
      Un();
    else 
      Zero();
  }
  Zero(); // Stop Bit 1
  Zero(); // Stop Bit 2
}

void sequence () {
  Caractere ('S');
  Caractere ('O');
  Caractere ('S');
}



void setup() {
  // put your setup code here, to run once:
  pinMode (L1,OUTPUT);
  Zero();

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  sequence();

  delay(1000);
}

I'm really sorry but I don't catch you yet.

I'm lost with all these terms (English is not my mother tongue)

Could you tell me please what I should write in the function declaration and after what to write at the parameter place in this function to use it.

OK, Thanks you.

I made my code working now.

Thanks for the help, indeed.

hary: I'm lost with all these terms (English is not my mother tongue)

If you need more help, try to post your problem on the French section. Regarding the names of your declarations in your code, I assume "tu est Francophone" :)

Yes you're right, I am.

But I have been used to that the english forum is way more active.

//Define the LED Pin
#define PIN_OUT 13
//Define unit length in ms
#define UNIT_LENGTH 200

//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 )

  • {*

  • 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 )_
_
{_
_ for( j = 0; j < sizeof MorseMap / sizeof MorseMap; ++j )_
_
{
_
_ if( toupper(string*) == 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.code )
{
msg += MorseMap.letter;
}
}
lastPos = pos+1;
pos = morse.indexOf(’ ', lastPos);*_

* // Handle white-spaces between words (7 spaces)*
* while( morse[lastPos] == ’ ’ && morse[pos+1] == ’ ’ )*
* {*
* pos ++;*
* }*
* }*
* return msg;*
}