Reading int over serial monitor

I'm working on a project using the HC12 transmitter/receiver. Right now I'm just using a pushbutton on one arduino, and an led on the other, just as a proof of concept. I eventually want to pass multiple variables, but for now, I'm keeping it simple. My end goal is to make a remote controlled car, or something of that nature. My problem has to do with ASCII characters, I'm fairly sure.

Here is my transmitter code:

//This is the transmit side. hc12 TXD connected to pin 2, RXD to pin 4.
#include <SoftwareSerial.h>
SoftwareSerial hc12(2,4);
const int buttonPin = 7;
int buttonState;
void setup() {
Serial.begin(9600);
hc12.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);

}

void loop() {
buttonState = digitalRead(buttonPin);
hc12.print(buttonState);
Serial.print(buttonState);
delay(50);

}

This code works (I think). I can get it to send to the other hc12. The problem is on the receiving end:

#include <SoftwareSerial.h>
SoftwareSerial hc12(10,11);
const int ledPin = 6;
int ledState = 0;
char incomingData = 0;

void setup() {
Serial.begin(9600);
hc12.begin(9600);
pinMode(ledPin, OUTPUT);
}

void loop() {
  while (hc12.available()){
  incomingData = char(hc12.read());     // Read data from the hc12. Store it in incomingData.
  }

 if (incomingData == 0){               //if hc12 sent a 1, turn ledState to HIGH.
    ledState = HIGH;
    Serial.println("high");
 }
 else if (incomingData == 1){          //if hc12 sent a 0, turn ledState to LOW.
  ledState= LOW;
  Serial.println("low");
 }
Serial.print("incomingByte is ");
Serial.write(incomingData);
Serial.println("");                     // some Serial for debugging


 digitalWrite(ledPin, ledState);        // set the led on or off, depending on incomingData

 }

This code doesn't work as-is. If I change the 1 and the 0 in the "if" and "else if" statements to 48 and 49, respectively, it works like it's supposed to. I can make it work, but I eventually want to send more than just an "on-off" command. I'll be sending variables to control motor speed, and possibly other things as well. How can I make it read incomingData as an int, and not its corresponding ASCII number?

How can I make it read incomingData as an int, and not its corresponding ASCII number?

You can't. You have to read it in as an ascii string and then convert that ascii string to a number.

Google "C++ atoi".

Serial input basics.

@OP

1. You can execute the following Transmit and Receive sketches and observe that when you are transmitting this figure/number:1234 through the declaration of int x = 0x1234;, the receiver receives them as 31, 32, 33, and 34 which are ASCII Codes (in hex base) for the characters 1, 2, 3, and 4 of the original figure/number 1234.
Transmit Codes:

//This is the transmit side. 
#include <SoftwareSerial.h>
SoftwareSerial hc12(2, 3);
int x = 0x1234;

void setup() 
{
  Serial.begin(9600);
  hc12.begin(9600);
}

void loop() 
{
  hc12.print(x, HEX);  //0x31, 0x32, 0x33, 0x34
  Serial.print(x, HEX); //
  Serial.println();
  delay(1000);
}

Receive Codes:

#include <SoftwareSerial.h>
SoftwareSerial hc12(2, 3);


void setup()
{
  Serial.begin(9600);
  hc12.begin(9600);
}

void loop()
{
  if(hc12.available()>0)
  {
    byte x = hc12.read();
    Serial.print(x, HEX);
    Serial.println();
  }
}

2. As has been stated in Post#1, you need to use th atoi() function to retrieve the original figure/number 1234 from the received ASCII codes (31, 32, 33, and 34). Note that the function atoi()(ASCII to Integer) will fail if the transmitted figure/number contains any digit other than 0 - 9.
Modified Modifier Receive Codes:

#include <SoftwareSerial.h>
SoftwareSerial hc12(2, 3);
bool flag1 = LOW;
char asciiArray[20] = "";
int i = 0;

void setup()
{
  Serial.begin(9600);
  hc12.begin(9600);
}

void loop()
{
  if (flag1 == LOW)
  {
    if (hc12.available()>0)
    {
      asciiArray[i] = hc12.read();
      i++;
      if (i == 4)
      {
        flag1 = HIGH;
      }
    }
  }
  else
  {
    int x = atoi(asciiArray);
    Serial.print(x, DEC);
    Serial.println();
    flag1 = LOW;
    i = 0;
  }
}

GolamMostafa:
Modifier Receive Codes:

The code in that example is not robust. It does not check if there are two chars available and it does not check to make sure that it is getting them in the correct order.

See the link in Reply #2

...R

Robin2:
The code in that example is not robust.

But, the sketch works; therefore, it could be adjusted to have robustness. Learning begins from a simple approach and then it moves slowly to the complex/beautiful level.

The primary intention of my Post#3 was to exemplify the use of atoi() function which @Delta_G had mentioned in his Post#1.