Pages: [1]   Go Down
Author Topic: Sorte de deuxième boucle  (Read 760 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bonjour à tous, débutant en Arduino je souhait faire comme 2 boucles pour traiter deux informations en même temps sans fin...

Exemple :

J'ai une bouche qui fait un chenillard va et vient (déjà fait) mais j'aimerais que les yeux clignotent tout le temps et en même temps que le chenillard.

Merci.
Logged

Geneva
Offline Offline
Faraday Member
**
Karma: 30
Posts: 3227
Yoplait... le pt'it suisse
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Voilà, voilà :

Maitriser le temps

Tu remplaces SDL_GetTicks par millis() et le tour est joué... bon d'accord y un peu de taf, mais c'est comme ça au début pour comprendre ce que l'on fait  smiley-wink
« Last Edit: July 27, 2010, 09:35:15 am by jfs » Logged

MacBook intel core 2 duo  os X snow Leopard 10.6<br/> eMac PPc G4  os X Leopard 10.5<br/>powerbook G4 os X Leopard 10.5
imac PPC G3 os X Pa

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sinon il y a une lib que s'appele Protothreads pour l'arduino.

Il y a eu une discussion dessus à cette adresse :http://arduino.cc/pipermail/developers_arduino.cc/2009-January/000456.html

La lib est dispo ici : http://www.kukkuk.dk/blog/uploads/protothreads1401.zip

Je précise que je n'ai pas testé.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

J'ai utilisé ProtoThread mais int led2 et int led1 restent allumés tout le temps et les 2 leds spéciales s'allument qu'à la fin du va et vient.

Code:
/**
 * This is a very small example that shows how to use
 * protothreads. The program consists of two protothreads that wait
 * for each other to toggle a variable.
 */

/* We must always include pt.h in our protothreads code. */
#include "pt.h"

int led1 =  0; // Led 6
int led2 =  1; // Led 5
int led3 =  2; // Led 7
int led4 =  3; // Led 8
int led5 =  4; // Led 9
int led6 =  5; // Led 4 (rouge)
int led7 =  6; // Led 1
int led8 =  7; // Led 2
int led9 =  8; // Led 3

/* Two flags that the two protothread functions use. */
static int protothread1_flag, protothread2_flag;

/* protothread state variables, one for each ptorothread. */
static struct pt pt1, pt2;

/**
 * The first protothread function. A protothread function must always
 * return an integer, but must never explicitly return - returning is
 * performed inside the protothread statements.
 *
 * The protothread function is driven by the main loop further down in
 * the code.
 */
static int protothread1(struct pt *pt)
{
  /* A protothread function must begin with PT_BEGIN() which takes a
     pointer to a struct pt. */
  PT_BEGIN(pt);

  /* We loop forever here. */
  while(1) {
    /* Wait until the other protothread has set its flag. */
    PT_WAIT_UNTIL(pt, protothread2_flag != 0);
    Serial.println("Protothread 1 running");

    /* We then reset the other protothread's flag, and set our own
       flag so that the other protothread can run. */
    protothread2_flag = 0;
    protothread1_flag = 1;

    /* And we loop. */
  digitalWrite(led7, HIGH);
  delay(50);
  digitalWrite(led7, LOW);
  delay(50);
  digitalWrite(led8, HIGH);
  delay(50);
  digitalWrite(led8, LOW);
  delay(50);
  digitalWrite(led9, HIGH);
  delay(50);
  digitalWrite(led9, LOW);
  delay(50);
  digitalWrite(led6, HIGH);
  delay(50);
  digitalWrite(led6, LOW);
  delay(50);
  digitalWrite(led2, HIGH);
  delay(50);
  digitalWrite(led2, LOW);
  delay(50);
  digitalWrite(led1, HIGH);
  delay(50);
  digitalWrite(led1, LOW);
  delay(50);
  digitalWrite(led3, HIGH);
  delay(50);
  digitalWrite(led3, LOW);
  delay(50);
  
  digitalWrite(led1, HIGH);
  delay(50);
  digitalWrite(led1, LOW);
  delay(50);
  digitalWrite(led2, HIGH);
  delay(50);
  digitalWrite(led2, LOW);
  delay(50);
  digitalWrite(led6, HIGH);
  delay(50);
  digitalWrite(led6, LOW);
  delay(50);
  digitalWrite(led9, HIGH);
  delay(50);
  digitalWrite(led9, LOW);
  delay(50);
  digitalWrite(led8, HIGH);
  delay(50);
  digitalWrite(led8, LOW);
  delay(50);
  }

  /* All protothread functions must end with PT_END() which takes a
     pointer to a struct pt. */
  PT_END(pt);
}

/**
 * The second protothread function.
 * This one is almost the same as the first one.
 */
static int protothread2(struct pt *pt)
{
  PT_BEGIN(pt);

  while(1) {
    /* Let the other protothread run. */
    protothread2_flag = 1;

    /* Wait until the other protothread has set its flag. */
    PT_WAIT_UNTIL(pt, protothread1_flag != 0);
    Serial.println("Protothread 2 running");

    /* We then reset the other protothread's flag. */
    protothread1_flag = 0;

    /* And we loop. */
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(50);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  delay(50);
  }
  PT_END(pt);
}

/**
 * setup() is where the protothreads are initialized.
 * The state variables pt1 and pt2 holdthe state of the two protothreads.
 */
 
void setup()
{
  /* Initialize the protothread state variables with PT_INIT(). */
  PT_INIT(&pt1);
  PT_INIT(&pt2);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);

  Serial.begin(9600);
}
 
/**
 * Finally, we have the main loop.
 * This is where the protothreads are scheduled.
 */
void loop()
{
  /*
   * We schedule the two protothreads by repeatedly calling their
   * protothread functions and passing a pointer to the protothread
   * state variables as arguments.
   */
  protothread1(&pt1);
  protothread2(&pt2);
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

UP
Logged

Pages: [1]   Go Up
Jump to: