4-Digit Display Error

Hi,

I have some problems with the following code. I don’t get why something like tm1637.display(0,0); ends up displaying a 0, but something like tm1637.display(2,inputString[2]); displays only rubbish.

Please help!!
Thank you for your input!
zws

[code]/*
I started from the example from the link

http://www.arduino.cc/en/Tutorial/SerialEvent

Set to newline.

In this example you have to open the serial window and type 4 digits.
Ex: 1234 and press Enter.
See what happens on the serial monitor and on the 4-digit display

*/

String inputString = “”; // a string to hold incoming data
char x1 = 0; // char to attribute to 2nd digit of the display

#include “TM1637.h”
#define CLK 2//pins definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637 tm1637(CLK,DIO);

void setup() {
// initialize serial:
Serial.begin(9600);

tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}

void loop() {

if (Serial.available() > 0) {
// get the new byte:
char inChar = (char)Serial.read();

// add it to the inputString:
inputString += inChar;


// if the incoming character is a newline, 
 if (inChar == '\n') {
  
   Serial.print("string ");
   Serial.println(inputString); //print string

  tm1637.display(0,0); // display 0 on the first digit 
  
  x1 = inputString[1]; // attach x1 to the second char in the string created
  Serial.print("2nd digit ");
  Serial.println(x1);  // print that second char to check if everything is ok
  
  tm1637.display(1,x1);// display the char attached to x1 on the second digit
                       // which does not work. Why??
  Serial.print("3rd digit ");
  Serial.println(inputString[2]);
  tm1637.display(2,inputString[2]);// display the char attached to 3rd char on 
                          // the 3rd digit
                          // which does not work. Why??
   inputString = "";  // delete string
  
}

}
}
[/code]

Hi,Dear,I have changed your demo

/*
I started from the example from the link

arduino.cc/en/Tutorial/SerialEvent

Set to newline.

In this example you have to open the serial window and type 4 digits.
Ex: 1234 and press Enter.
See what happens on the serial monitor and on the 4-digit display

*/

String inputString = “”; // a string to hold incoming data
char x1 = 0; // char to attribute to 2nd digit of the display

#include “TM1637.h”
#define CLK 2//pins definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637 tm1637(CLK,DIO);

void setup() {
// initialize serial:
Serial.begin(9600);

tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}

void loop() {

if (Serial.available() > 0) {
// get the new byte:
char inChar = (char)Serial.read();

// add it to the inputString:
inputString += inChar;


// if the incoming character is a newline, 
 if (inChar == '\n') {
  
   Serial.print("string ");
   Serial.println(inputString); //print string

  tm1637.display(0,0); // display 0 on the first digit 
  
  x1 = inputString[1]; // attach x1 to the second char in the string created
  Serial.print("2nd digit ");
  Serial.println(x1);  // print that second char to check if everything is ok
  
  tm1637.display(1,x1-'0');// display the char attached to x1 on the second digit
                       // which does not work. Why??  you should use decimal  system, and '0'--ASCII means 48--decimal  system!     
  Serial.print("3rd digit ");
  Serial.println(inputString[2]);
  tm1637.display(2,inputString[2]-'0');// display the char attached to 3rd char on 
                          // the 3rd digit
                          // which does not work. Why??   you should use decimal  system too!                      
   inputString = "";  // delete string
  
}

}
}

Now,you can compile it without errors.
Best Regards
Jacket