help needed: functions!


i need control motorized slide  that goes , forth along track, inverting direction switching high or low (operated slide itself) switches @ each end of track.  since between each travel (left right , right left) other things happen on others outputs, thougt manage sketch function. did 2 opposite functions this:

void leftoright()
{
swsxstat=digitalread(swsx);  // checks switch state  at right  end
 digitalwrite(trackdir,low); // set left-to-right direction of motor  
analogwrite(motorspeed,200); // gives power motor
 while (fcsxstat==0)
 {
   analogwrite(trackspeed,0); // off motor
 }}

... , rightoleft function, whit opposite parameters.

now, it's quite difficult me put these simple functions in void loop: thought enough, doesn't work...

void loop()
{
leftoright();

//.... other tasks, delay, ecc.///

rightoleft();
}

where i'm wrong?

hello there.

this simple option, assuming system can start carriage going left right.

code: [select]

void setup(void)
{
 // initialise motor speed , direction, motor going right
}

void loop(void)
{
 static bool goingright = true;

 if (goingright)
 {
   // check rightmost endstop , if hit, reverse motor.
   {
     //  ....
   }
   goingright = false;
 }
 else
 {
   // check leftmost endstop , if hit, reverse motor.
   {
     //  ....
   }
   goingright = true;
 }
}


this relies on 'static' modifier direction variable. means variable keeps value in between calls. make global. put end-point handling code in appropriate places.

----

you could step level , following. state machine. break problem down blocks (or 'states') describe system @ 1 time , write code describe  these runs each time main loop called.

your problem break down these states: moving right, moving left. easy. fancier , add transitional states (hit right end stop, e-stop pressed) let's start simple.

here's code. sorry if it's not understandable, keep plugging @ , should become clear. i'll happy explain further if want. apologies if there's compiler errors, haven't got access ide right now. i'll fix find once chance test.

code: [select]


// have been done classes function pointers neat , .. i'm in kind of mood :)
//
// tells compiler want define new type (like int, char, etc) type 'statefunc' , it's a
// pointer function returns int , takes no parameters.
//
typedef int (*statefunc)(void);

// here have array of pointers functions.
//
statefunc states[2];

// shorthand way of assigning increasing values labels.
//
enum
{
 left_to_right,   // equal 0
 right_to_left    // equal 1
};

// doesn't need global, doing differently 1st example.
//
int currentstate;

void setup(void)
{
 // put addresses of functions service each state array.
 //
 states[left_to_right] = lefttoright;
 states[right_to_left] = righttoleft;

 // set initial conditions
 //
 currentstate = left_to_right;
 digitalwrite(trackdir,low); // set left-to-right direction of motor
 analogwrite(motorspeed,200); // gives power motor
}

void loop(void)
{
 // (one way) call function indirectly via pointer
 //
 statefunc thefunction = states[currentstate];
 currentstate = (*thefunction)();
}

int lefttoright(void)
{
 // in state hang out until hit right-hand limitswitch
 //
 if(digitalread(right_limit))
 {
   // reverse motor , start executing other state
   //
   digitalwrite(trackdir,low); // set left-to-right direction of motor
   return right_to_left;
 }

 return left_to_right;
}

int righttoleft(void)
{
 // in state hang out until hit left-hand limitswitch
 //
 if(digitalread(right_limit))
 {
   // reverse motor , ... picture
   //
   digitalwrite(trackdir,high); // set right-to-left direction of motor
   return left_to_right;
 }

 return right_to_left;
}



i appreciate second example lot more difficult understand, it's way add complexity program without making messy inner loop. handling of states passed off functions can go ahead , expsnd as like.


have fun it, , don't afraid ask if want know more.

charlie


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > help needed: functions!


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums