Pages: [1]   Go Down
Author Topic: Execute subroutine/function once?  (Read 331 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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)?
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you need an extra condition perhaps like:

Code:
void setup() {
    onceFlag = false;
}

...
..
.


 if(Esplora.readButton(JOYSTICK_UP)==LOW)
 {
     if(onceFlag == false) {
         onceFlag = true;
         robotstandtowalk01();
     }
« Last Edit: February 20, 2013, 01:23:14 am by mistergreen » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you need an extra condition perhaps like:
Yes, but that code is less than professional.

Code:
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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: