Calling a function from a char array

Below is a script that will be part of a thermostat sketch. It should sound morse number characters through a small speaker to indicate the temperature.

My question is, is it possible to call a function that is part of a char array? The array char numeral[10] holds the text of functions. If I want to call a function, can I just write numeral[2], for example?

At this point the script is not working. The problem may very well be elsewhere. However if it is possible to call the functions this way, it should work, eventually.

int x = 0;
int ditlength = 25;
int readout = 125;
int hundreds = 0;
int tens = 0;
int ones = 0;
char numeral[10] = {'zero()', 'one()', 'two()', 'three()', 'four()', 'five()', 'six()', 'seven()', 'eight()', 'nine()'};

void dit();
for (x=0; x<ditlength; x++){
  digitalWrite(9, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  delayMicroseconds(100);
}
  digitalWrite(9,LOW);
  delay(100);
}

void dash() {
for (x=0; x<ditlength*3; x++){
  digitalWrite(9, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  delayMicroseconds(100);
}
  digitalWrite(9,LOW);
  delay(100);
}

void pause(){
  digitalWrite(9,LOW);
  delay(300);
}
  
  void one() {
  dit();
  dash();
  dash();
  dash();
  dash();
  pause();
  }
  
  void two() {
  dit();
  dit();
  dash();
  dash();
  dash();
  pause();
  }  

//lots of stuff removed

hundreds = int(readout/100);
tens = int((readout - (hundreds*100))/10);
ones = int(((readout - (hundreds*100)) - (tens*10)));

for (x=0; x<10; x++) {
  if (hundreds==x){
  numeral[x];
  }
}

for (x=0; x<10; x++) {
if (tens==x){
  numeral[x];
  }
}

for (x=0; x<10; x++) {
  if (ones==x){
  numeral[x];
  }
}

delay(1000);
 }

As an example, the output of the last for loop is supposed to work like this:

if(ones==2){
two();
}

I have the script working without for loops but its pretty bulky.

I think the following code fragment is an answer to your question, it uses an array of function pointers.

typedef void (* DigitFuncPtr) (); // this is a typedef to your digit functions

//the following declares an arry of 10 function pointers of type DigitFuncPtr 
DigitFuncPtr numeral[10] = {zero,one,two,three,four,five,six,seven,eight,nine};

void setup()                    // run once, when the sketch starts
{
}

void loop()                     // run over and over again
{
  for(int i = 0; i < 10; i ++)
     numeral[i];   // this calls each of the 10 digit functions
}

// define your digit functions 
void zero(void) {};
void one(void) {};
void two(void) {};
void three(void) {};
void four(void) {};
void five(void) {};
void six(void) {};
void seven(void) {};
void eight(void) {};
void nine(void) {};

But my inclination would be to impliment your application using byte arrays. For each digit I would have one byte with bit values of 0 and 1 indicating the sequence of dots or dashes, and the other byte indicating the number of bits to use in that byte.

You could then have a function where a given digit (0-9) was used as an index into the above array and looped through the bits sounding dots or dashes depending on the bit value.

I have not been able to find much on using typedef to create a function pointer. This script does not work:

int x = 0;
int ditlength = 25;
int readout = 125;
int hundreds = 0;
int tens = 0;
int ones = 0;

typedef void (* DigitFuncPtr) ();
DigitFuncPtr numeral[10] = {zero,one,two,three,four,five,six,seven,eight,nine};

void zero(void) {dash(); dash(); dash(); dash(); dash(); pause();};
void one(void) { dit(); dash(); dash(); dash(); dash(); pause();};
void two(void) {dit(); dit(); dash(); dash(); dash(); pause();};
void three(void) {dit(); dit(); dit(); dash(); dash(); pause();};
void four(void) {dit(); dit(); dit(); dit(); dash(); pause();};
void five(void) {dit(); dit(); dit(); dit(); dit(); pause();};
void six(void) {dash(); dit();dit();dit();dit(); pause();};
void seven(void) {dash(); dash(); dit(); dit(); dit(); pause();};
void eight(void) {dash(); dash(); dash(); dit(); dit(); pause();};
void nine(void) {dash(); dash(); dash(); dash(); dit(); pause();}; 

void dit() {
for (x=0; x<ditlength; x++){
  digitalWrite(9, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  delayMicroseconds(100);
}
  digitalWrite(9,LOW);
  delay(100);
}

void dash() {
for (x=0; x<ditlength*3; x++){
  digitalWrite(9, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  delayMicroseconds(100);
}
  digitalWrite(9,LOW);
  delay(100);
}

void pause(){
  digitalWrite(9,LOW);
  delay(300);
}
    
void setup() {
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  zero();  

for(int i = 0; i < 10; i ++){
  numeral[i];   // this calls each of the 10 digit functions
}

hundreds = int(readout/100);
tens = int((readout - (hundreds*100))/10);
ones = int(((readout - (hundreds*100)) - (tens*10)));

for (x=0; x<10; x++) {
  if (hundreds==x){
    numeral[x];
  }
}

for (x=0; x<10; x++) {
if (tens==x){
  numeral[x];
  }
}

for (x=0; x<10; x++) {
  if (ones==x){
  numeral[x];
  }
}

delay(1000);
 }

The zero() function works, the for loops seem to be working. Is there another step in declaring the function pointer?

I am interested in the bit/byte method as well but I need to research that a bit, I don’t know much about handling bits.

The loop could be simplified, I'm not sure why I was using for loops previously.

void loop() {

hundreds = int(readout/100);
tens = int((readout - (hundreds*100))/10);
ones = int(((readout - (hundreds*100)) - (tens*10)));

numeral[hundreds];
numeral[tens];
numeral[ones];

delay(1000);
 }

Sorry, I forget the parens after the function in my earlier post. Below is a little test sketch (that prints the morse to the serial port) the only significant change from the sketches above is the parens following the array index: i.e. numeral*()[/b]; *
```
*int x = 0;
int ditlength = 25;
int readout = 125;
int hundreds = 0;
int tens = 0;
int ones = 0;

typedef void (* DigitFuncPtr) (); // this is a typedef to your digit functions

//the following declares an arry of 10 function pointers of type DigitFuncPtr
DigitFuncPtr numeral[10] = {zero,one,two,three,four,five,six,seven,eight,nine};

void zero(void) {dash(); dash(); dash(); dash(); dash(); pause();};
void one(void) { dit(); dash(); dash(); dash(); dash(); pause();};
void two(void) {dit(); dit(); dash(); dash(); dash(); pause();};
void three(void) {dit(); dit(); dit(); dash(); dash(); pause();};
void four(void) {dit(); dit(); dit(); dit(); dash(); pause();};
void five(void) {dit(); dit(); dit(); dit(); dit(); pause();};
void six(void) {dash(); dit();dit();dit();dit(); pause();};
void seven(void) {dash(); dash(); dit(); dit(); dit(); pause();};
void eight(void) {dash(); dash(); dash(); dit(); dit(); pause();};
void nine(void) {dash(); dash(); dash(); dash(); dit(); pause();};

void dit() {
   Serial.print(".");
}

void dash() {
 Serial.print("-");
}

void pause(){
 Serial.println("" );
}
     
void setup() {
 pinMode(9, OUTPUT);
 Serial.begin(9600);
 Serial.println(“setup completed”);
}

void loop() {
for(int i = 0; i < 10; i ++){
 Serial.print(i);
 Serial.print("= ");
 numerali;   // this calls each of the 10 digit functions
}

Serial.println(readout);
 hundreds = int(readout/100);
 tens = int((readout - (hundreds100))/10);
 ones = int(((readout - (hundreds
100)) - (tens*10)));

numeralhundreds;
 numeraltens;
 numeralones;
 
 Serial.println("");

delay(1000);

}
__
```*__

You have already done the heavy lifting to get the digit functions declared so it may not be worth persuing, but if you are interested here is a sketch that does the morse translation using 0 and 1 to encode the dits and dashes.

int x = 0;
int ditlength = 25;
int readout = 125;
int hundreds = 0;
int tens = 0;
int ones = 0;

byte digits[] = {0b11111,0b01111,0b00111,0b00011,0b00001,   // 0 through 4
                 0b00000,0b10000,0b11000,0b11100,0b11110};  // 5 through 9 
                 
void dit() {
    Serial.print(".");
}

void dash() {
  Serial.print("-");
}

void pause(){
  Serial.println("" );
}
      
void sendMorseDigit(int numeral){
   int i;
   byte bit = 0b10000;   // init with the most significant bit set to 1
   
   for(i = 0; i < 5; i++, bit >>=1) {   // loop through all five bits testing for 0 or 1 
      if(bit & digits[numeral])         // if bit is set then its a dash 
         dash();
      else
         dit();
   }
   pause();           
}

void setup() {
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  Serial.println("setup completed");
}

void loop() {
for(int i = 0; i < 10; i ++){
  Serial.print(i);
  Serial.print("= ");
  sendMorseDigit(i);   
}

  Serial.println(readout);
  hundreds = int(readout/100);
  tens = int((readout - (hundreds*100))/10);
  ones = int(((readout - (hundreds*100)) - (tens*10)));

  sendMorseDigit(hundreds);
  sendMorseDigit(tens);
  sendMorseDigit(ones);
  
  Serial.println("");

  delay(1000);

}

Thanks! That works!

// Kegerator Controller
// Kegerator control to digital pin 5
// Thermistor to analog pin 2
// this example is with a 500 Ohm thermistor at 20C and a 1k Ohm resistor to ground
// 5VDC to thermistor to resistor to ground, pin 2 to thermistor/resistor junction
// Vmax is 4C, Vmin is 2C on my thermistor circuit.
// Vdyn is the current voltage read from the thermistor
// Pin 13 flashes morse value for Vdyn on LED, pin 9 sounds a morse tone on speaker for Vdyn
// 434-> 0C, 447-> 1C, 459-> 2C, 472-> 3C, 484-> 4C, 497-> 5C, 509-> 6C, 522-> 7C, 534-> 8C, 545-> 9C, 559-> 10C

int Vmax = 484;  
int Vmin = 459;
int ThermistorPin = 2;
int ditlength = 25;
int hundreds = 0;
int tens = 0;
int ones = 0;
int Vdyn = 0;
int status = 0;
int x = 0;
int c = 0;

typedef void (*FunctionPointer) ();
FunctionPointer numeral[10] = {zero,one,two,three,four,five,six,seven,eight,nine};

int conversion[9] = {434,447,459,472,484,497,509,522,534};

void zero(void) {dash(); dash(); dash(); dash(); dash(); pause();};
void one(void) { dit(); dash(); dash(); dash(); dash(); pause();};
void two(void) {dit(); dit(); dash(); dash(); dash(); pause();};
void three(void) {dit(); dit(); dit(); dash(); dash(); pause();};
void four(void) {dit(); dit(); dit(); dit(); dash(); pause();};
void five(void) {dit(); dit(); dit(); dit(); dit(); pause();};
void six(void) {dash(); dit();dit();dit();dit(); pause();};
void seven(void) {dash(); dash(); dit(); dit(); dit(); pause();};
void eight(void) {dash(); dash(); dash(); dit(); dit(); pause();};
void nine(void) {dash(); dash(); dash(); dash(); dit(); pause();}; 

void dit() {
for (x=0; x<ditlength; x++){
  digitalWrite(9, HIGH);
  digitalWrite(13, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  digitalWrite(13, LOW);
  delayMicroseconds(100);
  }
  digitalWrite(9,LOW);
  digitalWrite(13,LOW);
  delay(100);
}

void dash() {
for (x=0; x<ditlength*3; x++){
  digitalWrite(9, HIGH);
  digitalWrite(13, HIGH);
  delayMicroseconds(2800);
  digitalWrite(9, LOW);
  digitalWrite(13, LOW);
  delayMicroseconds(100);
  }
  digitalWrite(9,LOW);
  digitalWrite(13,LOW);
  delay(100);
}

void pause(){
  digitalWrite(9,LOW);
  digitalWrite(13,LOW);
  delay(300);
}
    
void setup() {
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  
Vdyn = analogRead(ThermistorPin);
Serial.println(Vdyn);

if(Vdyn > Vmax){
  status = 1;
  }
 
if (Vdyn < Vmin){
  status = 0;
  } 

if(status == 1){
  digitalWrite(5, HIGH);
  Serial.println("Compressor On");
  } 

if(status == 0){  
  digitalWrite(5, LOW);
  Serial.println("Compressor Off");
  }

hundreds = int(Vdyn/100);
tens = int((Vdyn - (hundreds*100))/10);
ones = int(((Vdyn - (hundreds*100)) - (tens*10)));

numeral[hundreds]();
numeral[tens]();
numeral[ones]();

delay(1000);

for (c=0; c<9; c++){   //if in range 0 to 8, morse flash degrees Celsius 
  int  d = c + 1;
  if (Vdyn > conversion[c] && Vdyn < conversion[d]){
    numeral[c]();
    Serial.println(c);
    }
  }
delay(1000);
}

I am currently creating a morse library. My purpose is that you could do like this Morse.morse("text to morse"); and it would blink it with proper dashes and dots. Everybody would chat with their Arduinos and maybe place some beacons or something :D

I am currently creating a morse library.
My purpose is that you could do like this Morse.morse(“text to morse”);
and it would blink it with proper dashes and dots.
Everybody would chat with their Arduinos and maybe place some beacons or something :smiley:

… .-- — …- .-… -… -… . … -. - . .-. . … - . -… - — … . . … — .-- -.-- — …- … .- …- . … – .–. .-… … – . -. - . -… … -

:smiley:

.. .-- --- ..- .-.. -.. -... . .. -. - . .-. . ... - . -.. - --- ... . . .... --- .-- -.-- --- ..- .... .- ...- . .. -- .--. .-.. .. -- . -. - . -.. .. -

:D

-.-- . .- ... ..- .-. . .. .-- .. .-.. .-.. .--. ..- -... .-.. .. ... .... .. - ..-. --- .-. .- .-.. .-.. -... - .-- -.. --- -.-- --- ..- .-. . .- .-.. .-.. -.-- ..- -. -.. . .-. ... - .- -. -.. -- --- .-. ... . - .... .- - --. --- --- -.. --- .-. .- .-. . -.-- --- ..- ..- ... .. -. --. ... --- -- . - .-. .- -. ... .-.. .- - --- .-. ;)

Sorry, I missed your last post. How is it going?

p.s. I have long since forgotten the little morse I knew. Isn't the web a wonderful thing ;)