Serial.available under void setup doesn't work

Hello,
I am trying to write a code that should control a steppermotor.
I want feed the information about the speed, ease in, ease out, and direction
in a string of information into the arduino once.
Then in the void loop I want Arduino to work with that info.
So I am trying to put a If(Serial.available()) in the void setup().
For some reason that doesn't work and I can't figure out why.
Here is the code that I wrote. Any suggestions are very welcome.
Regards
Ruud

String inString;
float easeIn;
float eaN;
int dash1;
int dash2;
int dash3;
int dash4;

String input1;
String input2;
String input3;
String input4;
String input5;

int tmpSpeed; // number of steps at full speed
int tmpEaseIn;// number of steps for ease in
int tmpEaseOut;// number of steps for ease out

float Speed;
float EaseIn;
float EaseOut;

float StepIn;
float StepOut;

void setup(){
Serial.begin(9600);
if(Serial.available()){
a = Serial.read();
if (isAscii(a)) {
inString += (char)a;
}
if (a == '\n') {
dash1 = inString.indexOf('/');
dash2 = inString.indexOf('/', dash1 + 1);
dash3 = inString.indexOf('/', dash2 + 1);
input1 = inString.substring(0,dash1);
input2 = inString.substring(dash1 +1,dash2);
input3 = inString.substring(dash2+1,dash3);
input4 = inString.substring(dash3+1);

tmpSpeed = (input1.toInt());
tmpEaseIn = (input2.toInt());
tmpEaseOut = (input3.toInt());

Speed = tmpSpeed / 1;
EaseIn = tmpEaseIn / 1;
EaseOut = tmpEaseOut / 1;
StepIn = Speed/EaseIn;
StepOut = Speed/EaseOut;

Serial.print("Speed = ");
Serial.println(Speed);
Serial.print("EaseIn = ");
Serial.println(EaseIn);
Serial.print("EaseOut = ");
Serial.println(EaseOut);
Serial.print("Direction = ");
Serial.println(input4);
}
}
}

Is this your full code? Because one, your missing your loop() function and two, everything below Serial.begin(9600); should be in the loop function.

Yes it does work. There is nothing to read the one and only time you look at the serial port, so you read nothing, exactly like you told the code to do.

You’re calling available right directly after you call begin. I wouldn’t expect a whole bunch of anything to show up in the few microseconds between the two. Throw a millisecond of delay in there between the two and see if that doesn’t help.

Are you sure that the sender is sending the information right at that millisecond? If you need the data before you do anything else, maybe you should wait around on it with a while loop.

I put that in a loop
x= 90/EaseIn;
for(y=0;y<90; y = y +x){
and then I tried
float z = Speed/sin(y);

Some real code, please. Not what you think you remember the code looking like.

You might also be interested in reading the documentation for the sin() function. Of particular interest would be the part that says that the input is in radians, not degrees.

This is the entire code sofar.

Do yourself, and us, a favor. Learn how to use Tools + Auto Format and how to post code correctly. There is a sticky at the top of the forum. Read it.

           tmpSpeed = (input1.toInt());
            tmpEaseIn = (input2.toInt());
            tmpEaseOut = (input3.toInt());

(Why) (is) (the) (call) (to) (the) (toInt()) (method) (in) (parentheses) (?)

PaulS:
You might also be interested in reading the documentation for the sin() function. Of particular interest would be the part that says that the input is in radians, not degrees.

That little tidbit has bitten more than a few Arduino newbies.

That little tidbit has bitten more than a few Arduino newbies.

And a few oldies, too. Beats me why sin(), cos(), etc. take an unusual measurement like radians as input, when no one knows what the hell a radian is. Everyone knows what a degree is. 90 degrees is a measurement everyone can visualize. 1.57 radians? What the hell is that?

Thing is that the algorithms for working out sin & cos are in radians, it is a circulat measurement with two Pi of them being in a circle. If you can stick to radians it is a lot more effecent.

ruudwouden:
This is useless

About as useless as that comment.

It's better to stay silent and be thought a fool, than to speak and prove yourself a fool.

If you can't be constructive, shut up.

Thing is that the algorithms for working out sin & cos are in radians, it is a circulat measurement with two Pi of them being in a circle. If you can stick to radians it is a lot more effecent.

I have no problem with sin(), cos(), etc. converting a value, like 90 degrees, into a value like PI/2 radians, if it makes sense. I object to having to convert from a unit of measure that makes sense to one that doesn't. I can understand how those functions could be more efficient dealing with radians. But, the mathematicians should be designing software - they should have been restricted to implementing the algorithm. Engineers should have been in charge of the interface. Then, the functions would have taken values in degrees.

Other versions, like sinForNerds(), could have taken an input in radians.

PaulS:

Thing is that the algorithms for working out sin & cos are in radians, it is a circulat measurement with two Pi of them being in a circle. If you can stick to radians it is a lot more effecent.

I have no problem with sin(), cos(), etc. converting a value, like 90 degrees, into a value like PI/2 radians, if it makes sense. I object to having to convert from a unit of measure that makes sense to one that doesn’t. I can understand how those functions could be more efficient dealing with radians. But, the mathematicians should be designing software - they should have been restricted to implementing the algorithm. Engineers should have been in charge of the interface. Then, the functions would have taken values in degrees.

Other versions, like sinForNerds(), could have taken an input in radians.

Radians are what trig functions have always been in (by default) since the dawn of time. Change it and people who are used to the “normal” way of doing it get confused. No one who uses trig seriously works in degrees. You might as well argue for arc-seconds (1/1296000 of a circle), or gradians (1/400 of a circle). The ratio between a circle’s circumference and its diameter is Pi - it makes sense to work in angles based on that same ratio.

Some programming languages provide the facility to change “mode”, but by default they will always be in radians.

Radians are what trig functions have always been in (by default) since the dawn of time.

And Arduinos have had odd pin spacing on one side since the beginning. Doesn't mean it's right, though. A dumb decision is a dumb decision, no matter how long ago it was made.

No one who uses trig seriously works in degrees.

Engineers do. I know. I are one.

ruudwouden:
This is the entire code sofar.
#include<math.h>
const float pi= 3.14;
float pRad;
float x;

Code tags, thanks.

Read this before posting a programming question

How to use this forum

PaulS:
And a few oldies, too. Beats me why sin(), cos(), etc. take an unusual measurement like radians as input, when no one knows what the hell a radian is. Everyone knows what a degree is. 90 degrees is a measurement everyone can visualize. 1.57 radians? What the hell is that?

Probably because the iterative function that does the calculation works in radians. To convert from degrees to radians is simple, but if the sine function did that for you (and you happened to want to use radians) then you have two extra calculations (converting to degrees, and then have the function convert back to radians) when you don’t need any. Especially when those calculations involve a division.

. A dumb decision is a dumb decision, no matter how long ago it was made.

You mean like the one that put 90 degrees in a right-angle, and not 100?
That was the Babylonians*, wasn’t it?

*The ancient people from modern-day Iraq, not the Metropolitan Police.

PaulS:

No one who uses trig seriously works in degrees.

Engineers do. I know. I are one.

I said “seriously” :wink:

Personally though I think C++ should have gone further with strongly typed variables, and have the variable definition define the content, not just the storage format.

So you could have

Degrees myAngle = 45;
Radians myOtherAngle = 180/PI;

float result;
result = sin(myAngle);
result = sin(myOtherAngle);

and it’d use the right calculations for you.

Yes, I know you can manually do it with classes and overloading the sin() etc functions, but it’d be nice if that kind of thing was standard. Trouble is, the math functions are pretty much all the original C functions, not C++ specific ones, so that kind of thing never happened.

Maybe an “advanced math library” would be good, with classes for the different things, so you could do such things as:

Degrees myDeg = 45;
Radians myRad = myDeg.toRadians();

Maybe I’ll write one…

Degrees are mostly used for navigation. Its better to say "heading 280deg" as "heading 4.37rad".. At least you know where is the North then. Otherwise we use "angular mil" when we home at EU commissioners..

pito:
Degrees are mostly used for navigation. Its better to say "heading 280deg" as "heading 4.37rad".. At least you know where is the North then. Otherwise we use "angular mil" when we home at EU commissioners..

Is that "mil" as in 1/1000 inch?