Pages: [1]   Go Down
Author Topic: sprintf kinda works but I am having problems  (Read 404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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
  LCDContrast(45);
  LCDClear(WHITE);
 
  Serial.begin(9600);
  Serial.println("Enter Password");
}

void loop()
{
 if(Serial.available())
 {
    //int val1 = Serial.read();
    char buffer[10];
    sprintf (buffer, "%c", Serial.read());
    LCDPutStr(buffer,1,5,BLACK,WHITE);
    delay(150);
 }
 
 
}

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:)
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7251
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'
Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Koman
Logged

Pages: [1]   Go Up
Jump to: