HELP SENDING STRINGS TO SUBFUNTCIONS

HI EVERYONE,
i need some help in a project (i'm working on arduino uno r3 and macosx).

In the code that follows, which is used to send morse messages (see sendmsg function), i would like to take analogue readings of some pins and make the program transmit the value of these integer variables.
The function sendmsg() should be used to send a string which corresponds to an analog reading of a sensor.
however this sendmsg() function accepts text in the form of "text", but i can't make it accept strings which correspond to analogue reading values.
thanks and 73

THE CODE FOLLOWS::::

struct t_mtab { char c, pat; } ;
struct t_mtab morsetab[] = {
  	{'.', 106},
	{',', 115},
	{'?', 76},
	{'/', 41},
	{'A', 6},
	{'B', 17},
	{'C', 21},
	{'D', 9},
	{'E', 2},
	{'F', 20},
	{'G', 11},
	{'H', 16},
	{'I', 4},
	{'J', 30},
	{'K', 13},
	{'L', 18},
	{'M', 7},
	{'N', 5},
	{'O', 15},
	{'P', 22},
	{'Q', 27},
	{'R', 10},
	{'S', 8},
	{'T', 3},
	{'U', 12},
	{'V', 24},
	{'W', 14},
	{'X', 25},
	{'Y', 29},
	{'Z', 19},
	{'1', 62},
	{'2', 60},
	{'3', 56},
	{'4', 48},
	{'5', 32},
	{'6', 33},
	{'7', 35},
	{'8', 39},
	{'9', 47},
	{'0', 63}
} ;

#define N_MORSE  (sizeof(morsetab)/sizeof(morsetab[0]))
#define SPEED  (14)
#define DOTLEN  (1200/SPEED)
#define DASHLEN  (3*(1200/SPEED))

void
dash()
{
  digitalWrite(0, HIGH) ;
  delay(DASHLEN);
  digitalWrite(0, LOW) ;
  delay(DOTLEN) ;
}

void
dit()
{
  digitalWrite(0, HIGH) ;
  delay(DOTLEN);
  digitalWrite(0, LOW) ;
  delay(DOTLEN);
}

void
send(char c)
{
  int i ;
  if (c == ' ') {
    
    delay(7*DOTLEN) ;
    return ;
  }
  for (i=0; i<N_MORSE; i++) {
    if (morsetab[i].c == c) {
      unsigned char p = morsetab[i].pat ;
     

      while (p != 1) {
          if (p & 1)
            dash() ;
          else
            dit() ;
          p = p / 2 ;
      }
      delay(2*DOTLEN) ;
      return ;
    }
  }
  
}

void
sendmsg(char *str)
{
  while (*str)
    send(*str++) ;
 
}

void setup() {
   pinMode(0, OUTPUT);

}

void loop() {
  sendmsg("MESSAGE") ;
  delay(1500) ;
}

but i can't make it accept strings which correspond to analogue reading values.

I can't see such a string

Well this is the only bit that's going to run.
So the rest of your code is redundant.

void setup() {
   pinMode(0, OUTPUT);

}

void loop() {
  sendmsg("MESSAGE") ;
  delay(1500) ;
}

Well ok you call a small function to send "message"

the sendmsg() recalls other functions (send(), dot and dash etc.) to move cursor in strings and isolate single characters to be sent.
the problem is that i would like to insert into sendmsg() some variables corresponding to anlogue readings, as it seems to accept only text into quotation marks.
thanks

OK
But this bit ensures that the last bit never gets sent.

      while (p != 1) {

Edit scrap this. I see you're using a leading 1 bit to mark the end of the string.

So to achieve your objective, you just need something like this in your code somewhere.

char analogString[5];
sprintf ( analogString, "%d",analogRead(pin) );
sendmsg( analogString );

yes, it should work . i'll let you know :wink: there was even an issue with cahr * conversion thanks!!

You can make your N_MORSE macro a little more flexible:

#define ARRAY_SIZE(x)  (sizeof(x)/sizeof(x[0]))

and then just call it with:

  for (i = 0; i < ARRAY_SIZE(morsetab); i++) {

You can also simplify your dash length macro to:

#define DASHLEN  (3*DOTLEN)

here is the final product

const int dPin = 2; 
const int aiPin = 3; 
const int aiiPin = 4; 
int d = 0;

struct t_mtab { char c, pat; } ;
struct t_mtab morsetab[] = {
              	{'.', 106},
              	{',', 115},
              	{'?', 76},
              	{'/', 41},
                {'*', 53},
                {'+', 42},
                {'=', 49},
              	{'A', 6},
              	{'B', 17},
              	{'C', 21},
              	{'D', 9},
              	{'E', 2},
              	{'F', 20},
              	{'G', 11},
              	{'H', 16},
              	{'I', 4},
              	{'J', 30},
              	{'K', 13},
              	{'L', 18},
              	{'M', 7},
              	{'N', 5},
              	{'O', 15},
              	{'P', 22},
              	{'Q', 27},
              	{'R', 10},
              	{'S', 8},
              	{'T', 3},
              	{'U', 12},
              	{'V', 24},
              	{'W', 14},
              	{'X', 25},
              	{'Y', 29},
              	{'Z', 19},
              	{'1', 62},
              	{'2', 60},
              	{'3', 56},
              	{'4', 48},
              	{'5', 32},
              	{'6', 33},
              	{'7', 35},
              	{'8', 39},
              	{'9', 47},
              	{'0', 63}
} ;

            #define N_MORSE  (sizeof(morsetab)/sizeof(morsetab[0]))
            #define SPEED  (14)
            #define DOTLEN  (1200/SPEED)
            #define DASHLEN  (3*(1200/SPEED))

void
dash()
{
digitalWrite(0, HIGH) ;
delay(DASHLEN);
digitalWrite(0, LOW) ;
delay(DOTLEN) ;
}

void
dit()
{
digitalWrite(0, HIGH) ;
delay(DOTLEN);
digitalWrite(0, LOW) ;
delay(DOTLEN);
}

void
send(char c)
{
int i ;
if (c == ' ') {
delay(7*DOTLEN) ;
return ;
}
for (i=0; i<N_MORSE; i++) {
if (morsetab[i].c == c) {
unsigned char p = morsetab[i].pat ;
while (p != 1) {
if (p & 1)
dash() ;
else
dit() ;
p = p / 2 ;
}
delay(2*DOTLEN) ;
return ;
}
}
}
void
sendmsg(char *str)
{
while (*str)
send(*str++) ;
}
void setup() {
 pinMode(0, OUTPUT);
 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);
}
void loop() {
  
  sendmsg ("=");
    
char analogiString[5];
sprintf ( analogiString, "%d",analogRead(aiPin) );

char analogiiString[5];
sprintf ( analogiiString, "%d",analogRead(aiiPin) );
 
                      sendmsg("* * * RYRYRYRYRYRY " ) ;
                      sendmsg("1." ) ;
                      sendmsg( analogiString ); 
                      sendmsg(" , 2." ) ;
                      sendmsg( analogiiString );
            
                    d = digitalRead(dPin);
                    if (d == HIGH) {     
                    sendmsg (" , 3.TRUE ");
                     } 
                    else {
                     sendmsg (", 3.FALSE ");
                    }  
                       sendmsg ("+");
                                     
                     delay(5000) ;  
}





[code/]

So did it work? :slight_smile:

yes it worked perfectly thanks!