Go Down

Topic: Execute subroutine/function once? (Read 397 times) previous topic - next topic

jeffmorris

Code: [Select]

#include <Esplora.h>
int standwalk = 0;
void setup()
{
  Serial1.begin(9600);
  robotstand01();
}
void loop()
{
  if(Esplora.readButton(JOYSTICK_UP)==LOW)
  {
    //robotstandtowalk01();
    //delay(1000);
    robotwalk01();
    delay(1000);
    robotwalk02();
    delay(1000);
    robotwalk03();
    delay(1000);
    robotwalk04();
    delay(1000);
    robotwalk05();
    delay(1000);
    robotwalk06();
    delay(1000);
  } 
  if(Esplora.readButton(JOYSTICK_DOWN)==LOW)
  {
    robotwalk01();
    delay(1000);
    robotwalk02();
    delay(1000);
    robotwalk03();
    delay(1000);
    robotwalk04();
    delay(1000);
    robotwalk05();
    delay(1000);
    robotwalk06();
    delay(1000);
  } 
}
void move(int servo, int position, int time)
{
  Serial1.print("#");
  Serial1.print(servo);
  Serial1.print(" P");
  Serial1.print(position);
  Serial1.print(" T");
  Serial1.println(time);
}
void robotstand01()
{
  move(0,1500,1000);
  move(1,1500,1000);
  move(2,1500,1000);
  move(3,1500,1000);
  move(4,1500,1000);
  move(5,1500,1000);
  move(16,1500,1000);
  move(17,1500,1000);
  move(18,1500,1000);
  move(19,1500,1000);
  move(20,1500,1000);
  move(21,1500,1000);
}
void robotstandtowalk01()
{
  move(0,1500,1000);
  move(1,1667,1000);
  move(2,1500,1000);
  move(3,1500,1000);
  move(4,1500,1000);
  move(5,1667,1000);
  move(16,1500,1000);
  move(17,1500,1000);
  move(18,1500,1000);
  move(19,1667,1000);
  move(20,1500,1000);
  move(21,1500,1000);
}

void robotwalk01()
{
  move(0,1167,1000);
  move(1,1667,1000);
  move(2,1833,1000);
  move(3,1500,1000);
  move(4,1167,1000);
  move(5,1667,1000);
  move(16,1167,1000);
  move(17,1500,1000);
  move(18,1833,1000);
  move(19,1667,1000);
  move(20,1167,1000);
  move(21,1500,1000);
}
void robotwalk02()
{
  move(0,1167,1000);
  move(1,1500,1000);
  move(2,1833,1000);
  move(3,1500,1000);
  move(4,1167,1000);
  move(5,1500,1000);
  move(16,1167,1000);
  move(17,1500,1000);
  move(18,1833,1000);
  move(19,1500,1000);
  move(20,1167,1000);
  move(21,1500,1000);
}
void robotwalk03()
{
  move(0,1167,1000);
  move(1,1500,1000);
  move(2,1833,1000);
  move(3,1667,1000);
  move(4,1167,1000);
  move(5,1500,1000);
  move(16,1167,1000);
  move(17,1667,1000);
  move(18,1833,1000);
  move(19,1500,1000);
  move(20,1167,1000);
  move(21,1667,1000);
}
void robotwalk04()
{
  move(0,1833,1000);
  move(1,1500,1000);
  move(2,1167,1000);
  move(3,1667,1000);
  move(4,1833,1000);
  move(5,1500,1000);
  move(16,1833,1000);
  move(17,1667,1000);
  move(18,1167,1000);
  move(19,1500,1000);
  move(20,1833,1000);
  move(21,1667,1000);
}
void robotwalk05()
{
  move(0,1833,1000);
  move(1,1500,1000);
  move(2,1167,1000);
  move(3,1500,1000);
  move(4,1833,1000);
  move(5,1500,1000);
  move(16,1833,1000);
  move(17,1500,1000);
  move(18,1167,1000);
  move(19,1500,1000);
  move(20,1833,1000);
  move(21,1500,1000);
}
void robotwalk06()
{
  move(0,1833,1000);
  move(1,1667,1000);
  move(2,1167,1000);
  move(3,1500,1000);
  move(4,1833,1000);
  move(5,1667,1000);
  move(16,1833,1000);
  move(17,1500,1000);
  move(18,1167,1000);
  move(19,1667,1000);
  move(20,1833,1000);
  move(21,1500,1000);
}


How do I have robotstandtowalk01() subroutine/function execute once? Is it possible to use the code in BlinkWithoutDelay sketch instead of Delay(1000)?

Krupski


Execute subroutine/function once?


Code placed in "setup()" runs once.

Code placed in "loop()" runs forever.

If you want to run something only once, place it in setup.

Or, write a separate function and then call it from setup.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

mistergreen

#2
Feb 20, 2013, 02:57 am Last Edit: Feb 20, 2013, 07:23 am by mistergreen Reason: 1
you need an extra condition perhaps like:

Code: [Select]

void setup() {
   onceFlag = false;
}

...
..
.


if(Esplora.readButton(JOYSTICK_UP)==LOW)
{
    if(onceFlag == false) {
        onceFlag = true;
        robotstandtowalk01();
    }

PaulS

Quote
you need an extra condition perhaps like:

Yes, but that code is less than professional.

Code: [Select]
if(Esplora.readButton(JOYSTICK_UP) == LOW)
{
     if(!onceFlag)
     {
         onceFlag = true;
         robotstandtowalk01();
     }


onceFlag is either true or false. The comparison to true or false is unnecessary.

majenko

This is an ideal candidate for the Finite State Machine.

That is essentially what BlinkWithoutDelay implements (albeit a simplistic one).

To learn more, and (hopefully) understand how a Finite State Machine works, read my Finite State Machine Tutorial.

Go Up