Pages: [1]   Go Down
Author Topic: Execute subroutine/function once?  (Read 288 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
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: 23
Posts: 665
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

0
Offline Offline
Full Member
***
Karma: 4
Posts: 196
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: 548
Posts: 46042
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1]   Go Up
Jump to: