Serial Communication Code....Help!

The code is about obtaining a value of an angle (in degrees) through serial communication and feeding it to a stepper motor.

I have few problems in the code(given below):

  1. I am able to obtain the value for “String angle” successfully. I want to convert this string to an Integer (angle to finangle) and finally read that integer in the output window.

  2. I need to write this integer value (finangle) to the stepper motor in form of steps. I want to read the steps values as well in the output window. I am badly stuck as I am not a pro programmer. Kindly help asap.

#include <SoftwareSerial.h>

String angle;
int finangle;
int steps; //400 steps

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {

pinMode(5, OUTPUT);

Serial.begin(9600);
mySerial.begin(9600);

}

void loop() {

serial();
motor();

}

void serial(){

if (mySerial.available()) {
Serial.println(mySerial.readString());
}
if (Serial.available()) {
angle = mySerial.println(Serial.readString());
}

//Serial.println(“angle is”);
//Serial.println(angle,DEC);

finangle = atoi(angle.c_str());
Serial.println(finangle,“fin”);
}

void motor(){

steps = 1.111 * finangle;
{
for(int x = 0; x < steps; x++)
{ //3200 are number of steps
digitalWrite(5,HIGH);
delayMicroseconds(500);
digitalWrite(5,LOW);
delayMicroseconds(500);
}
}
Serial.println(steps,“steps”);

delay (1500);
}

I would advise to read the Serial Input Basics thread

It will first of all give you ideas and secondly help you to prevent the use of String (capital S); heavy use of String manipulation can result in unexpected behaviour at run time.

Further please read https://forum.arduino.cc/index.php?topic=148850.0, specifically point #7 how to post code.

             Serial.println(finangle,"fin");

I'm surprised that the compiler accepts this. Look up the reference for Serial.println() to see how it works.

Serial.readString() may be appropriate here. But as your program gets bigger, that will be a significant problem for you.

What stepper? What driver? What do you expect SoftwareSerial to do?

MorganS:
I'm surprised that the compiler accepts this.

One word: >:( >:( >:( -fpermissive >:( >:( >:(

The Serial class has a parseInt() method that you could use to read an int rather than a String.

Or, the String class has a toInt() method.

Both methods ARE documented on the Reference page.

@sterretje Pardon me for posting the code wrongly as I am quite new to the forum.

I had gone through the link Serial Input Basics thread before writing the code. Can u please help me specifically with the issues i am facing. I have gone through many tutorials but I have no idea about what is the exact problem.

About the software serial -

I am using a serial communication medium to send string data to arduino board. Arduino board receives data at 10,11 ports as RX,TX. So i used software serial here. That string data needs to be converted to an integer to feed to a stepper motor. i am confused in that part.

I am using a serial communication medium to send string data to arduino board. Arduino board receives data at 10,11 ports as RX,TX. So i used software serial here. That string data needs to be converted to an integer to feed to a stepper motor.

What is sending that data, and do you have any control over how it is formatted?

If this data is coming to the Arduino over software serial, what is the role of the usb/hardware serial port?

I had gone through the link Serial Input Basics thread before writing the code.

Yet your code shows no influence at all from the methods presented in the tutorial.
Why not give it a try?

np_iitm:
I had gone through the link Serial Input Basics thread before writing the code.

You did not tell us and there is no sign of code from Serial Input Basics in your program.

If there is something about my code that you don't understand then if you tell me what it is I will try to help.

...R

Serial is

S
.
.
.
L
.
.
.
O
.
.
.
W

            if (mySerial.available()) {

Serial.println(mySerial.readString());
            }
            if (Serial.available()) {
            angle = mySerial.println(Serial.readString());
            }

After the first String is read you try to read another only one or two microseconds later. That is not enough time for the next character to begin transmitting, let alone finish one character.

So there is nothing available and it won't wait.