sprintf unexpected return value

So I am tryin to feed my fona800 a string that contains the phone number I want to dial. I am obtaining the numbers through a count of a rotary dial. Inputs come in and are stored in an array. I convert the array values into a single long number and then use sprintf to convert them to a string so I can feed my fona. However, the value of the string it returns is only 5 digits long and I don’t know why.

The attached screenprint of the serial shows 1 dialed ten times and the resulting sprintf output. Any help is appreciated.

#include "Adafruit_FONA.h"
#include "stdint.h"
#include "stdio.h"

#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

// this constant won't change:
const int  buttonPin = 2;    // the pin that the rotary number switch is attached to
const int diallingPin = 4; // pin that the rotor start/finish is attached to


// Variables that will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 1;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int phoneNumber[9];          // sets array for phone number
int dialing = 0;            // sets state of dialing on/off
int dialState;             // place to store dial state
int numberString;           // keep track of the numbers dialer
int number = 0;
int array [10];
uint32_t k = 0;



void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initializes dialling pin as an input;
  pinMode(diallingPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
    Serial.println("Setup is initialized");
    Serial.print("Current state of Dial Connector is  ");
    if(digitalRead(2) == 0){
      Serial.println("on");
      }else{
      Serial.println("off");
      }
    Serial.print("Current state of Dialer is  ");
    if(digitalRead(4) == 0){
      Serial.println("dialing");
      }else{
      Serial.println("base");
      }
    
}



void loop() {

  if (buttonPushCounter > 0 && digitalRead(4) == 1) {
    if(buttonPushCounter == 10) {
      buttonPushCounter = 0;
    }
 array[number] = buttonPushCounter;
 number = number + 1;
 buttonPushCounter = 0;
  if(number == 10){
  for(int place = 0; place < 10; place++){
  Serial.println(array[place]);
  k = (10 * k) + array[place];
  
 
 //convert int phone number to string so it will work with fona.callPhone

  if (k > 1000000000){
  char str[10];
  sprintf(str, "%d", k);
  // fona.callPhone(str);
  Serial.println(str);
  }
  }

 
 

    
  
 }
}else{
  // read the pushbutton input pin:
  buttonState = digitalRead(2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    
    if (buttonState == LOW) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      //if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  
  // save the current state as the last state,
  //for next time through the loop

 
  lastButtonState = buttonState;


  }


  
  }

  //if(buttonPushCounter > 0 && digitalRead(4) != 1){
    // for(number = 0; number <= 9; number++){
      //   array[number] = buttonPushCounter;
        // Serial.println(array[number]);
     }

 // if(buttonPushCounter > 0 && digitalRead(4) != 1){ 
   // Serial.print(buttonPushCounter);
   // buttonPushCounter = 0;

Hello and welcome,

%d is for printing a signed integers, you want to print a unsigned long, so you must use %lu (see this ). Also your variable str is too small, it should be 11 at least.

Guix,

Thanks for the help. I have read the link your provided(very helpful) and made the suggested changes. The result, however, is just a single extra digit reported.

  if (k > 1000000000){
  char str[15];
  sprintf(str, "%ul", k);
  // fona.callPhone(str);
  Serial.println(str);
  }
  }

jrmelanson:
The result, however, is just a single extra digit reported.

Look closer, it is not a digit. :wink:

  sprintf(str, "%ul", k);  // << should be %lu

Touche'!

This works great. Now I am left with another question however...

This codesprintf(str,"%d","%d","%d","%d","%d","%d","%d","%d","%d","%d",array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7],array[8],array[9]); does not return the same string as sprintf(str, "%lu", k);.

Any thoughts on this?

does not return the same string as

No surprise there!
There is not even a remote similarity between those two calls to sprintf.

Read about sprintf() here.

Use just one format string:

sprintf(str, "%d%d%d%d%d%d%d%d%d%d", array[0], ...);

sprintf(str, "%d", "%d", ...) gives the address of the second "%d". The rest of the arguments are ignored.

jrmelanson:
Touche'!

This works great. Now I am left with another question however...

This code

sprintf(str,"%d","%d","%d","%d","%d","%d","%d","%d","%d","%d",array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7],array[8],array[9]);

does not return the same string as

sprintf(str, "%lu", k);

.

Any thoughts on this?

That isn't the way sprintf() works.
You only pass in a single format string.
See the man page:
http://www.cplusplus.com/reference/cstdio/sprintf/