Serial.read(); Question, Not printing the same digit I am sending

So I am completely green at the programming. A lot of experience with hardware and electronics theory. I bought a UNO and a shield as a kit with a 4 digit 7 segment display. Took me not much more than an hour to get everything assembled, upload the counter program, and have a working 4 digit counter.

Next I wanted to change the program so i could put a digit on a line of the program and just have it show that one digit, after three days of reading and tweaking, playing etc. I got it. Next step I want to enter the base10 digit into the serial monitor and have it displayed on the 7 segment LED display. It did not take me too much to get this working but I find one part strange.

During the Serial Troubleshooting phase I had the UNO print back to the monitor what it received. When I send a 0 it sends back a 48. I send 2 it sends 50, and so on. I just added a line to subtract 48 and both the serialprint and my LED display reads the same number. Its working, but why is this.

I have set the baud rate in the serial monitor same as in the code, 4800. I chose 4800 because I hope to eventually connect to NMEA0183.

//1 Digit Arduino Counter
#define A A4
#define B 13
#define C 10
#define D 9
#define E 8
#define F A1
#define G 11
// Pins driving common anodes
#define CA1 12
// Pins for A B C D E F G, in sequence
const int segs[7] = {A4, 13, 10, 9, 8, A1, 11};
// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010, 0b0000010, 0b1111000, 0b0000000, 0b0010000};

int dataIN = 0;

void setup() {
  pinMode(A4, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);  
  pinMode(A1, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  Serial.begin(4800);
}
void loop()

{
if (Serial.available() > 0){
  dataIN = Serial.read();
  dataIN = dataIN - 48;
  Serial.print("I received: ");
  Serial.println(dataIN);
}
//calls the data out of the array
lightDigit1(numbers[dataIN]);
}

//activate current digita anode
void lightDigit1(byte number) {
  digitalWrite(CA1, HIGH);
  lightSegments(number);
}
//activate LED segments
void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

My next step is to get three digits working and maybe a negative sign in the same fashion

Thanks everyone for the support, Brian

When I send a 0 it sends back a 48. I send 2 it sends 50, and so on

Google ASCII codes You are receiving a character representing each digit.

Got the ASCII Codes. It seams the Serial.print and the PC Serial Monitor to be fairly straight forward. Whats the best way to deal with this on the Serial.read side. eventually I plan on reading data from an NMEA0183 sentence and perform arithmetic on the data. I doubt my current method of subtracting 48 is a good solution. especially when I get into more than one digit and negative.

Brian

You can split NMEA sentences using the strtok() function and convert the resulting string representing a number into a real number using the atoi() function.

The TinyGPS library (and others) can also do a lot of the work for you.

Have a look at Serial Input Basics. Simple reliable ways to receive data.

…R

During the Serial Troubleshooting phase I had the UNO print back to the monitor what it received. When I send a 0 it sends back a 48.

Very simple serial echo code.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}