Go Down

Topic: sprintf kinda works but I am having problems (Read 754 times) previous topic - next topic


I have tried many combination using sprintf to read from serial to my color LCD it print 1 2 3 4 5 6 7 in a sequence but not in a string like i want here is my code:

Code: [Select]
// Included files
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"
// External Component Libs
#include "LCD_driver.h"

void setup()
  ioinit(); //Initialize I/O
  LCDInit(); //Initialize the LCD
  Serial.println("Enter Password");

void loop()
    //int val1 = Serial.read();
    char buffer[10];
    sprintf (buffer, "%c", Serial.read());

to better explain: if i type into the serial monitor 12345 it will iterate through the values 12345 and only display 5 how do I make it display 12345 on the color LCD screen.\

Thanks for the help:)


What is the 1,5 representing? coordinate to print? If it is, then any character is printed at the same spot. Any other lcd printing functions you can use?
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter


it isn't displaying at the same spot. if it was it would not look like a 5 it would look like 12345 all mixed in with each other I think it has something to do with passing a char but for whatever reason it passes in only 1 value instead of the string "12345" it passes in '1' '2' '3' '4' '5'


The mystery is easily explained. Your loop is looping so quickly, that it reads the characters one by one. So if you send 12345 on the serial line, sprintf will be called for each character individually and as you print each character on the position 1,5 on your display. So you'll just see the last character sent, the 5 in my example.

Also calling sprintf with %c isn't very useful, you don't really have to format a character to itself. You just can write it directly into buffer and add '\0' to the position right after it. But I guess, this here is just some testing code anyway.


Go Up