Arduino receiving different values through serial

Hi, to start off, this might not be a problem with arduino code or arduino, but I figured I would post here because I really just can not figure out what is wrong.

I am working on this project just for fun to send key strokes from the keyboard, through the computer, and out through the USB to my arduino mega. No additional hardware is here, just the computer, the arduino, and the USB cable.

I am using Microsoft Visual Studio Express 2012 to write code to receive key strokes and send them to the USB. This is the code I am using:

#include "stdafx.h"
#include "conio.h"

using namespace System;
using namespace System::IO::Ports;

int main(array<System::String ^> ^args)
{
	String^ portName;
	String^ key;
	int baudRate=9600;
	Console::WriteLine("type in a port name and hit ENTER");
	portName=Console::ReadLine();
	//arduino settings
	SerialPort^ arduino;
	arduino = gcnew SerialPort(portName, baudRate);
	//open port
	try
	{
		arduino->Open();

		while(1)
		{
			int k = getch();
			key = k.ToString();
			Console::WriteLine(key);
			arduino->Write(key);
			if (k == 32)
				return 0;
		}
		
	}
	catch (IO::IOException^ e )
	{
		Console::WriteLine(e->GetType()->Name+": Port is not ready");
	}
}

This code works fine, and sends commands to the arduino. I might as well ask this as well, but after 35 key strokes it just stops sending key strokes, I am unsure as to why, but that is not an arduino problem (I don’t think).

So when the certain value for key gets sent to the arduino, it changes. For example, the values that are assigned to the variable key for pressing the number 1 and 2 are 49 and 50, respectively. However, when they get sent to the arduino, the values are different some how. 1 is now 57, and 2 is now 48. I am unsure as to why this is happening. I tried 4 and 5 and they both have their values shift down 2 like the key 2. This is the code I have on the arduino:

int ledPin = 13;
int key=0;
int c;
void setup() 
{
    pinMode(ledPin, OUTPUT); // pin will be used to for output
    Serial.begin(9600); // same as in your c++ script
}

void loop() 
{
  if (Serial.available() > 0)
  {
    key = Serial.read(); // used to read incoming data
    if (key == 57)
    {
      digitalWrite(ledPin, HIGH);
    }
    else if (key == 48)
    {
      digitalWrite(ledPin, LOW);
    }
  }
  c = key;
  Serial.println(c);
}

As of right now it is just to switch a light on and off. I am hoping to involve many more keys and having the values be consistent would be very convenient. Anyways, if anyone could help me with why the values are different that would be awesome. I am not completely new to programming but I am certainly no expert and have not gotten too far into advanced stuff.

Thank you for any help or advice.

I might as well ask this as well, but after 35 key strokes it just stops sending key strokes, I am unsure as to why, but that is not an arduino problem (I don't think).

The PC program quits sending or the Arduino quits receiving? Details matter.

So when the certain value for key gets sent to the arduino, it changes. For example, the values that are assigned to the variable key for pressing the number 1 and 2 are 49 and 50, respectively. However, when they get sent to the arduino, the values are different some how. 1 is now 57, and 2 is now 48.

How do you know this? You have a Serial.print() statement in the Arduino code, but nothing to read serial data in the PC application.

The PC program I think stops sending. When I try to type more stuff in nothing happens, sorry, I forgot to clarify this.

For the second item, I know that it changes because when I tried the LED on off it doesn't work. What I do is type in '2' a couple of times on the PC program, quickly close it, then switch to the Arduino IDE serial monitor. The first few values on the Arduino are what it is receiving and I can see that they are different.

Rather than closing your PC program and opening the serial monitor you need to extend your PC program to read the data coming back from the Arduino - that may be choking the system while it is not being read and the incoming buffer on the PC keeps filling up.

...R

Try just running your Arduino program with serial monitor. If you get garbage then check the baud rate at each end. Once you have that working right then use that to check whatever your PC program is sending.

These two lines compile exactly the same. Which is easier to read/understand?

    if (key == 57)
    if (key == '9')

Raieth: What I do is type in '2' a couple of times on the PC program, quickly close it, then switch to the Arduino IDE serial monitor. The first few values on the Arduino are what it is receiving and I can see that they are different.

This does not sound to me like a valid thing to do, and I wouldn't put any faith in it as a diagnostic technique.