Help with serial communication

I’m trying to tie a PWM output with a serial input. It’s acting really weird for me. I’ve set up some feedback to see what’s going on.

if (Serial.available()>0){
    incomingByte = Serial.read();
    Serial.println("incomingByte spot 1");
    Serial.println(incomingByte,DEC);

Using this, and entering 50 for its Serial.read() input, it initially tells me that what I entered is 53. This is my first problem. Then the loop continues to

 if (targetmmB > ((OCR3B+1)/20)){	
    if ((millis()-timeoldB) > 100){
      if (OCR3B <= 1994)
        OCR3B = OCR3B + x;
      timeoldB = millis();	
      Serial.println("targetmmB spot 2");
      Serial.println(targetmmB,DEC);
}

This time, the targetmmB value returned to me is 53, matching incomingByte.

Then the loop executes again and without entering any serial input, the serial.available() > 0 statement is true again and I’m told that the incomingByte is 48. The loop executes again and the same thing happens, but incomingByte is 10. Then the targetmmB comes to 10 and the loop then Serial.available > 0 becomes false and targetmmB stays at 10. Can anyone help me figure out what’s going on?

My complete code:

volatile byte spincount;

unsigned int spintime = 0;
unsigned long timeold = 0;
unsigned long timeoldB = 0;
unsigned long timeoldC = 0;

unsigned int x = 5; //this determines the actuator step

int feedPinB = 2;
int feedPinC = 3;
//set PWM pins for timer 4

int incomingByte = 0;

int targetmmB = 0;
int targetmmC = 0;

void setup()
{
  //attachInterrupt(2,spinfun,RISING); //pin 21
  pinMode(feedPinB, OUTPUT);
  pinMode(feedPinC, OUTPUT);	
  TCCR3A = _BV(COM3A1) | _BV(COM3B1) | _BV(COM3C1) | _BV(WGM31) | _BV(WGM30);
  TCCR3B = _BV(WGM33) | _BV(WGM32)| _BV(CS30);
  //set PWM setting bits, see datasheet
  OCR3A = 2000; //upper count limit
  OCR3B = 0; //count limit for timer B
  OCR3C = 0; //count limit for timer C
  /*COM3A1 - 1 COM3A0 - 0 COM3B1 - 1 COM3B0 - 0 COM3C1 - 1 COM3C0 - 0 WGM31 - 1 WGM30 - 1
   	ICNC3 - 0 (unused) ICES3 - 0 (unused) WGM33 - 1 WGM32 - 1 CS32 - 0 CS31 - 0 CS 30 - 1
   	Fast PWM mode, prescaler of 1*/
  Serial.begin(9600);
  Serial.println("Enter the value you want.  0->100 gives you B's target in mm, 101->200 gives you C's target (subtract 100 from the entered value).");
}

void loop()
{
 

  if (Serial.available()>0){
    incomingByte = Serial.read();
    Serial.println("targetmmB spot 1");
    Serial.println(incomingByte,DEC);

    if (0 < incomingByte <= 100)
      targetmmB = incomingByte;
    if (incomingByte > 100)
      targetmmC = incomingByte - 100;

  }


  if (targetmmB > ((OCR3B+1)/20)){	
    if ((millis()-timeoldB) > 100){
      if (OCR3B <= 1994)
        OCR3B = OCR3B + x;
      timeoldB = millis();	
      Serial.println("targetmmB spot 2");
      Serial.println(targetmmB,DEC);
      //every 0.1s, if the target is bigger than the current value, increase PWM duty cycle
    }  
  }		


  if (targetmmC > ((OCR3C+1)/20)){
    if (millis()-timeoldC > 100)
      if (OCR3C <= 1994)
        OCR3C = OCR3C + x;
    timeoldC = millis();
    //every 0.1s, if the target is bigger than the current value, increase PWM duty cycle
  }
}

The 50 is being sent as ASCII, first the '5' (ASCII 53) and then the '0' (ASCII 48). What you need to do is either send the number you want directly or set up some code to take those ASCII characters in and decode them and put them back together.

Very simple serial capture code that captures incoming character bytes into a character String for various uses.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

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

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c = Serial.read();
    readString += c;
  }

  if (readString.length() >0) {
    Serial.println(readString);

    readString="";
  } 
}

Ah, okay. That makes sense, that it’s reading as characters.

Is there any reason I shouldn’t just use Serial.parseFloat() ? It seems to be working (I found out about it after googling for a decoder)