function called in serial monitor ignores bounds set for function

Alright, I know the subject line might sound confusing but hear me out. Here’s some background. With this project, I am running a dc motor with an encoder. The dc motor should only work between 120 degrees, 60 degrees left and right when a coupling is set as vertical. To limit this motion, I attempted to set bounds. These were set before setup as variable as such:

//dc wiper bounds in degrees
float home1 = 180.00;
float stop1 = 240.00;
float stop2 = 120.00;

I would then call this function this function to run the motor by just calling it within the void loop:

///////function for rod_forward_flick (works when manually calling)
void rod_forward_flick(float stop1,float deg) 
{
  //Serial.println("end forward flick");
  digitalWrite (8,LOW); //dir cw low ccw
if (deg < stop1){
  analogWrite (11,30);
  }
  else {
  analogWrite (11,0);}
  return;
}

Then I wanted to be able to call the function through the serial monitor by using say a three letter input. This way the user can control the motions through the serial monitor. This code looks like this:

////Serial input section within loop 11-5 11:55am

if (Serial.available())
{
  command = Serial.readStringUntil('\n');
}
if (command == "cff")
{
  carriage_forward_flick();
}
else if(command == "cbf")
{
  carriage_backward_flick();
}
else if(command =="hfr")
{
  home_flick_rod(home1, deg, stop1,  stop2);
}
else if(command =="rff")
{
  rod_forward_flick(stop1, deg);
}
else if(command =="rbf")
{
  rod_back_flick(stop2, deg);
}
else if(command == "cf")
{
  casting_forward(stop2, deg);
}
else if(command == "cb")
{
  casting_backward(stop1, deg);
}
else if(command =="lff")
{
  line_flick_forward(stop2, deg);
}
else if(command =="lfb")
{
  line_flick_backward(stop1, deg);
}
command = "";

This code is within the void loop.

The issue is such. When I just call the function within the void loop it will work. As soon as I comment that call out and use this serial monitor code to call the function the bounds are just thrown out the window and the motor just continues to spin endlessly. It is as though the bounds are just being completely ignored. I am sure this is an issue with the serial monitor section of code but I have no idea where to start. I will attach my full code as a reference.

I really appreciate all the help that this forum has been with this. Thank you so very much for your time.

11-5_4pm.ino (9.18 KB)

What does the Tic.h library do?

I suggest you take everything out of the program except the function carriage_forward_flick() and the minimum other code needed to call it. Then you will have a program you can include in your Post and we can all focus on the problem.

Some of your functions seem to expect STOP parameters, why doesn't carriage_forward_flick() ?

And if your limits are set in Global variables why pass them as parameters? Just get the functions to refer to the Global variables.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What sort of encoder are you using?

Thanks.. Tom.. :slight_smile: