Sous programme

Bonjour à tous,

Je suis dans une nouvelle phase de programmation de mon automate d'orgue de barbarie ( le faire parler avec synchronisation de la bouche ), après une programmation réaliste de mouvements de tête ,yeux, bouche très fluide.

Le programme devenant conséquent, je souhaite faire des sous programmes façon " GOSUB / RETURN " en Basic.

Ne connaissant pas encore toutes les riches subtilités du langages Arduino, j'ai besoin de votre aide.

Comment faire des sous programmes lorsque l'on a besoin de répète un grand nombre fois la même routine ?

J'ai essayé de faire un programme d'essai à base de " GOTO " ( Oui je sais ce n'est pas bien !! ), mais en plus il tourne mais ne fais pas ce que je lui demande.
Pour ne pas mourir idiot, merci de me dire pourquoi.

D'avance merci

Didier ( Dijon )

//Programmation avec sous programme  27 Mai 2014

int a;
void setup()
{
Serial.begin(19200); 
}

void loop()
{

 label:
delay(2000);
//a=3;
a=random(1,5);
Serial.println ("        ");
Serial.println (a);
Serial.println ("        ");

if (a=1)
{
goto Cmd1;
}
if (a=2)
{
goto Cmd2;
}
if (a=3)
{
goto Cmd3;
}

Cmd2:
Serial.println ("        ");
Serial.print (" Cmd 2 : ");
Serial.println (a);
goto label;

Cmd1:
Serial.println ("        ");
Serial.print (" Cmd 1 : ");
Serial.println (a);
goto label;


Cmd3:
Serial.println ("        ");
Serial.print (" Cmd 3 : ");
Serial.println (a);
goto label;

}

Utilise des fonctions, ce qui est en fait de la programmation procédurale.

Le BASIC n'avait pas cette notion et il fallait tricher avec des "goto", mais en C, c'est une commande très rare, utilisée principalement lors de code généré automatiquenment (des systèmes d'analyse lexicale ou machines à état par exemple).

Ton programme avec le goto empêche la fonction loop de se dérouler normalement.

Il y a une erreur sur cette ligne

if (a=1){
     .....
}

le signe = c'est l'affectation là tu mets 1 dans la variable a
le == c'est le test d'égalité
La bonne écriture c'est donc

if (a==1){
    .....
}

Voilà ton code modifié

//Programmation avec sous programme  27 Mai 2014

int a;
void setup(void)
{
Serial.begin(19200); 
}

void loop(void)
{

	delay(2000);
	//a=3;
	a=random(1,5);
	Serial.println ("        ");
	Serial.println (a);
	Serial.println ("        ");

	if (a==1)
	{
		Cmd1());
	}
	if (a==2)
	{
		Cmd2());
	}
	if (a==3)
	{
		Cmd3());
	}

}

void Cmd2(void){
	Serial.println ("        ");
	Serial.print (" Cmd 2 : ");
	Serial.println (a);
}

void Cmd1(void){
	Serial.println ("        ");
	Serial.print (" Cmd 1 : ");
	Serial.println (a);
}

void Cmd3(void){
	Serial.println ("        ");
	Serial.print (" Cmd 3 : ");
	Serial.println (a);
}

Bonsoir Merci pour vos réponses.

En effet je me suis fait avoir avec cette syntaxe "==" et ce n'est pas la première fois. j'ai déjà cherché (et trouvé !!) ce beug dans mon programme actuel.

@+
Didier