Limiting Motor Action

Dear all.

I need Some coding Help Here . I just simple.

I have two angles coming out Desired and actual. The Difference of angle Might positive or negative. If it is positive move motor in forward Direction If it is Negetive move motor in reverse Direction If between outer and inner band limit motor stop How to put this in code.??? I wanted to keep band limit within 2 degree. if difference of degree >+/- 2 degee within stop motor else fw/reverse direction

static enum dir{
  FWD,
  REV,
  STOP
}DIR;
float Desire;
float Actual;
static int IN1=4;
static int IN2=5;
static int PWM=2;
void Setup()
{
Serial.begin(9600);
 pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(PWM,OUTPUT);
}
void loop()
{
void actuate(){

  if(abs(Desire-Actual) > TRACK_BAND){

    if(Desire>actual)
      DIR = REV;
    else
      DIR = FWD;
  }
  else{
    DIR = STOP;
  }

  moveActuator();
}
}

void moveActuator(){
  Serial.print("Actuator DIR:");
  switch(DIR){

  case FWD:
    moveFWD();

    break;
  case REV:
    moveREV();

    break;
  case STOP:
    moveSTOP();
    break;
  }
}

void moveFWD(){
  Serial.println(FWD);
  digitalWrite(IN1,1);
  digitalWrite(IN2,0);

}


void moveREV()
{
  Serial.println(REV);
  digitalWrite(IN1,0);//spdt
  digitalWrite(IN1,1);//dpdt


}



void moveSTOP(){

  Serial.println(STOP);
  //Serial.print("Actuator DIR:");
  digitalWrite(IN1,0);
  digitalWrite(IN2,0);

}

The basic idea of your program as posted seems OK, apart from the fact that it does not compile, that is.

I need just code.I have added this code for Reference i am doing.

How to do it ??

Why aren't you passing "dir" (note the lack of capitals) into "moveActuator()" ?

Edit: OK, maybe not "dir" - how about some sensibly-named variable?

AWOL: Why aren't you passing "dir" (note the lack of capitals) into "moveActuator()" ?

It just function call. When this function being called it check the Previous status like DIR= fw or Reverse.

I stopped looking too hard when I saw this

void loop()
{
void actuate(){

as the code would obviously not compile. Did you write this code ?

When this function being called it check the Previous status like DIR

Look carefully at DIR. What is it?

I Need code In c Here. Leave the code attached

algorithm is :

I need code for this . The function for fw /rev/ stop are Defined under program.
i have 3 function 1:
move forward 2) Move reverse 3) MOve stop

Delta_t = abs(desire- actual)
INNER BAND=-2.0 and OUTER BAND=2.0

If( INNER BAND<Delta_t >OUTER BAND) : Stop Motor
IF (Delta_t> OUTER BAND) ; reverse dir
if (Delta_t<INNER BAND) ; fw direction

Look carefully at DIR. What is it?

I have algorithms Which calculate the Desired angle and sensor to Determine actual angle.What i need is i need to take difference of two angle . then i need to choose direction

Note: "Setup" != "setup". IN1 is not defined. IN2 is not defined. PWM is not defined. tracker_des_angle, tracker_actual_pos and TRACK_BAND are all not defined.

You cannot nest function definitions

Shall I move this Gigs and collaborations?

Dear Awol.

I cant put my full code That Measure Desired and actual angle. I have Edited my code. Main intention here To move motor Fw /rev and Stop . In between BAnds

Please don’t edit previously posted code - it makes the thread hard to follow.

You cannot nest function definitions

error: a function-definition is not allowed

I pointed it out, and the compiler pointed it out.
Edit: And UKHeliBob pointed it out.

This code only for reference . Algorithm is also attached above thread. I will call The actuate() the function.IN actuate() function the algorithm will take place as mentioned above . after moveActuator(); will call function to move forward and reverse direction.

void actuate();
static enum dir{
  FWD,
  REV,
  STOP
}DIR;
float Desire;
float Actual;
static int IN1=4;
static int IN2=5;
static int PWM=2;
static int TRACK_BAND=2.0;
void setup()
{
Serial.begin(9600);
 pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(PWM,OUTPUT);
}
void loop()
{
actuate();
}

void actuate(){

  if(abs(Desire-Actual) > TRACK_BAND){

    if(Desire>Actual)
      DIR = REV;
    else
      DIR = FWD;
  }
  else{
    DIR = STOP;
  }

  moveActuator();
}



void moveActuator(){
  Serial.print("Actuator DIR:");
  switch(DIR){

  case FWD:
    moveFWD();

    break;
  case REV:
    moveREV();

    break;
  case STOP:
    moveSTOP();
    break;
  }
}

void moveFWD(){
  Serial.println(FWD);
  digitalWrite(IN1,1);
  digitalWrite(IN2,0);

}


void moveREV()
{
  Serial.println(REV);
  digitalWrite(IN1,0);//spdt
  digitalWrite(IN1,1);//dpdt


}



void moveSTOP(){

  Serial.println(STOP);
  //Serial.print("Actuator DIR:");
  digitalWrite(IN1,0);
  digitalWrite(IN2,0);

}

So, now it compiles. Good.

This code only for reference

As opposed to...what exactly?

static int TRACK_BAND=2.0;

Whatever.

I need code where below algorithm works

algorithm is :

I need code for this . The function for fw /rev/ stop are Defined under program.
i have 3 function 1:
move forward 2) Move reverse 3) MOve stop

Delta_t = abs(desire- actual)
INNER BAND=-2.0 and OUTER BAND=2.0

If( INNER BAND<Delta_t >OUTER BAND) : Stop Motor
IF (Delta_t> OUTER BAND) ; reverse dir
if (Delta_t<INNER BAND) ; fw direction

You need to update the value of Actual each time you move, otherwise whatever it is that you are moving will keep going in the same direction once it starts.

I think all here not understand problem.
I have not posted complete code here. Since it evolve too many calculation.
Every 3 minute i have being updated with desire and actual value.
What i need simple c code. which do below function. know that code i posted desired and actual position not variable. it keep remain constant always in stop mode
I**.**
Here i wanted to assume that there 2 inputs i.e actual and Desire , if difference is negative move forward
**if differece +ve **
if within band: STOP motor

Delta_t = abs(desire- actual)
INNER BAND=-2.0 and OUTER BAND=2.0

If( INNER BAND<Delta_t >OUTER BAND) : Stop Motor
IF (Delta_t> OUTER BAND) ; reverse dir
if  (Delta_t<INNER BAND) ; fw direction

I think all here not understand problem.

True. Emphasis on "all".

I have not posted complete code here.

Yup, that's the problem

Since it evolve too many calculation.

Evolutionary algorithms are tricky on a processor with limited RAM and a Harvard architecture.

I need c code for below process.

Like Elapsed time program posted in forum.

It looks to me like you've got most of that algorithm implemented. What is the actual problem?