Adapter un code Arduino pour une carte Moteino

Bonjour a tous, je suis novice dans le domaine et aujourd'hui j'ai besoin de votre aide.

J'essai de commande un moteur pas a pas via un encodeur optique et une carte arduino, le code que j'utilise n'est pas de moi mais il fonctionne bien.
Je voudrais commander mon moteur pas a pas en mode sans fil donc j'ai acheter 2 carte Moteino RFM69HW 433 Mhz disponible ici ==>http://lowpowerlab.com/moteino/

Le code Arduino dont je dispose fonctionne parfaitement, mais pour le faire fonctionner avec les carte sans fil Moteino il faut rajouter des librairie et adapter le code, ce que je ne sait pas faire (et c'est bien dommage) donc si quelqu’un pourrait m'aider a passer de mon code Arduino au code qui fonctionne avec une carte moteino cela m'aiderait beaucoup.

Code arduino :

#define encoder_a 2 //keep this on and interrupt pin
#define encoder_b 3 //keep this on and interrupt pin
#define motor_step 4 //can be any pin
#define motor_direction 5 //can be any pin

#include <delay.h>
volatile long motor_position, encoder;

void setup () {
  //set up the various outputs
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
  
  // then the encoder inputs
  pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);
  // enable pullup as we are using an open collector encoder
  digitalWrite(encoder_a, HIGH); 
  digitalWrite(encoder_b, HIGH); 
  
  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, encoderPinChangeA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  encoder = 0; //reseet the encoder position to 0
}

void loop() {
  //do stuff dependent on encoder position here
  //such as move a stepper motor to match encoder position
  //if you want to make it 1:1 ensure the encoder res matches the motor res by dividing/multiplying
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);// move stepper in reverse
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    delayMicroseconds(200); //_delay_us(200); //modify to alter speed
    motor_position++;
    encoder = 0; //encoder--;
  }
  else if (encoder < 0) {
    digitalWrite (motor_direction, LOW); //move stepper forward
    digitalWrite (motor_step, HIGH);
    digitalWrite (motor_step, LOW);
    delayMicroseconds(200); //_delay_us(200); //modify to alter speed
    motor_position--;
    encoder = 0; //encoder++;
  }
}

void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
    encoder--;
  }
  else{
      encoder++;
  }
}

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
    encoder--;
  }
  else {
    encoder++;
  }
}

Un exemple de code Moteino pour émetteur et un autre pour récepteur sont disponible sur la page que j'ai mis en lien un peu plus haut.

bonjour,
ton lien ne fonctionne pas.
vu qu'apparemment il fonctionne avec un ATMega328P, il ne doit pas y avoir de modif a faire.
pour faire fonctioner les 2 par radios
https://github.com/LowPowerLab/RFM69

Merci pour ta réponse rapide, j'ai modifier le lien il fonctionne maintenant, il ne devrait pas y avoir de modif a faire normalement au niveaux du" void setup" et "void loop" de mon code Arduino mais par contre je ne parvient pas a l’insérer dans le code propre a la carte Moteino qui lui permet de communiquer sans fil, j'ai les bonne librairie (RFM69.h) mais je n'arrive pas a insérer mon code Arduino dans le code de transmission de la Moteino car il y a plein de commande qui me sont inconnu.

Dans la librairie, il y a des exemples d'échanges entre 2 cartes. Ce serait je pense une bonne base de départ.
Ensuite, après avoir compris comment fonctionne la liaison entre 2 cartes, il faudrait peut aussi décrire un peu mieux ce que tu veux faire parce "Ci quelqu'un pourrait m'aider a adapter mon code arduino pour qu'il fonctionne avec les carte moteino" on ne peut pas dire que ce soit très clair comme besoin.

J'ai essayer de garder les fonctions de la librairie qui permette de faire communiquer les 2 cartes, mais il doit y avoir des modif a faire dans le code "loop" pour que ca fonctionne, car j'arrive a faire communiquer les carte mais elle n’exécute pas mon code arduino.

sans code complet des deux cotés, ca risque d'être dur de répondre :wink:
déjà, essayes de faire communiquer les cartes avec les exemples avant de te lancer dansun truc plus complexe.

J'ai essayer de faire communiquer les cartes avec le code d'exemple, et j’arrive bien a recevoir les donné en vérifiant avec le moniteur série donc déjà mes carte émette et réceptionne. Mon problème est justement d'adapter le code Arduino pour qu'il fonctionne avec les Moteino, cela me dépasse carrément. C'est pour ça que je demande de l'aide car je sait que le code brut Arduino n'est pas compatible sans être modifier pour moteino.

On a toujours pas compris ce que tu veux faire. Et on comprend mal ce qui fonctionne et ce qui ne fonctionne pas.
Tu arrive à faire communiquer 2 cartes OK
Le code d'origine de pilotage du moteur pas-à-pas, tout seul, fonctionne-t-il sur Moteino? (oui/non)
Si non pourquoi? Qu'est-ce qui pose problème?

Si oui, qu'est-ce que tu veux faire avec tes carte Moteino?

En faite le but du projet et de commander un moteur pas a pas avec un encodeur optique, et le tout sans fil entre les deux grâce au moteino. Un côté emeteur (1 moteino + 1 encodeur optique) et l'autre côté récépteur (1 moteino + 1 easydriver v4.4 + un moteur pas a pas)

J'ai un code de commande de moteur pas a pas que j'ai testé avec une carte arduino, ça fonctionne bien.
Mes carte moteino fonctionne avec un code de test juste pour voir si elle communique, donc elle fonctionne bien.

Par contre je n’arrive pas a adapter mon code qui marche sous arduino pour qu'il fonctionne sous moteino.

y a pas a adapter, c'est la même chose, sauf qu'il y a la partie 433mhz.
sans code, on peut rien pour toi.

si les exemples fonctionnent, essaye de faire allumer une led maintenant en envoyant une donnée.

Ok j'ai bien compris mais je suis vraiment débutant et j'ai beaucoup de mal a comprendre le codage, donc je vais poster le code que je me sert pour tester les cartes moteino et qui fonctionne, il suffirait d'y adapter mon code arduino pour commander le moteur pas a pas. Le problème c'est que je n'y arrive pas çà me dépasse vraiment, quel frustration de rien y comprendre !!! :sweat_smile:

CODE MOTEINO ( Emmeteur) :

#include <RFM69.h>
#include <SPI.h>

#define NODEID 2 //unique for each node on same network
#define GATAWAYID 1
#define NETWORKID 100 //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME 30 // max # of ms to wait for an ack
#define LED 9 // Moteinos have LEDs on D9
#define SERIAL_BAUD 115200
#define D4 4
#define DESTINATION_NODE 1

RFM69 radio;
bool promiscuousMode = true; //set to 'true' to sniff all packets on the same network
int D4Status = 0;

void setup()
{

//Setup the pins
	pinMode(LED, OUTPUT);
	pinMode(D4, INPUT);
   
//Start the Serial
	Serial.begin(SERIAL_BAUD);
	delay(10);

//Initialize the radio
	radio.initialize(FREQUENCY,NODEID,NETWORKID);
	#ifdef IS_RFM69HW
    radio.setHighPower(); //uncomment only for RFM69HW!
	#endif
	radio.encrypt(ENCRYPTKEY);//Turn encryption ON
	radio.promiscuous(promiscuousMode);
	char buff[50];
	sprintf(buff, "\nTransmiting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
	Serial.println(buff);
}


void loop() 
{
	if (digitalRead(D4) == HIGH)//If the button connected to Digital Input pin 4 was pushed, 
	{
		delay(10);//wait 10ms for debounce
		Serial.print("Sending a D4 Status to node "); Serial.print(DESTINATION_NODE); Serial.println(" and waiting for ACK...");
		if (radio.sendWithRetry(DESTINATION_NODE, "4:1", 3, 2, ACK_TIME))//Send the 3 byte packet to node 1 for processing, try that 2 times and wait 30ms each time for an ACK
		{	
			Serial.println("ACK Received");//If the sent packet was delivered and an ACK received, send this message to the serial monitor
			delay(3);// Allow the the radio to switch to receive mode
			Blink(LED,100);//Blink the transmitter led to show the data was successfully sent
			while (digitalRead(D4) == HIGH)//Just a loop to wait for the button to be released
			{ 
			}
		}
	}
}

void Blink(byte PIN, int DELAY_MS)//Local led blinking function
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}

CODE MOTEINO ( Récépteur) :

#include <RFM69.h>
#include <SPI.h>

#define NODEID 1 //unique for each node on same network
#define NETWORKID 100 //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY RF69_433MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME 30 // max # of ms to wait for an ack
#define LED 9 // Moteinos have LEDs on D9
#define SERIAL_BAUD 115200

RFM69 radio;
bool promiscuousMode = true; //set to 'true' to sniff all packets on the same network
char packet[3]; // Allocate some space for the packet array

void setup()
{
	Serial.begin(SERIAL_BAUD);//Initialize the Serial port at the specified baud rate
	delay(10);//Wait for the Serial port to settle down
	
	//Initialize the radio
	radio.initialize(FREQUENCY,NODEID,NETWORKID);
	#ifdef IS_RFM69HW
		radio.setHighPower();
	#endif
	radio.encrypt(ENCRYPTKEY);//Turn the radio Encryption ON
	radio.promiscuous(promiscuousMode);//Turn the radio Promiscuous mode ON
	
	//Create and display a startup message on the serial monitor
	char buff[23];//buff[] is an array that will hold the "Listening at XXX Mhz..." message
	sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);//buff will be filled with th FREQUENCY variable content
	Serial.println(buff);//Send the message to the serial monitor
}

void loop()
{
	if (radio.receiveDone())//If some packet was received by the radio, wait for all its contents to come trough
	{
		if (radio.TARGETID == 1)//Check if the packet destination is this radio (NODE 1)
		{
			
			//Display a message on the serial monitor showing the packet origin radio NODE number
			Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
			
			//If the radio Promiscuous mode is ON, show the packet destination radio NODE number on the serial monitor
			if (promiscuousMode)
			{
				Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
			}
			
			//Check the data packet length and fill the packet[] array with its contents
			for (byte i = 0; i < radio.DATALEN; i++)
			{
				packet[i] = (char) radio.DATA[i];
				
				//Display the data packet content on the serial monitor
				Serial.print (packet[i]);
			}
			//Add the transmission signal level to the message to be displayed on the serial monitor
			Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
			
			//Add a plain message stating the radio NODE number which sent the data
			byte theNodeID = radio.SENDERID;
			Serial.print("; Data received from node ");
			Serial.print(theNodeID);
			
			//If an ACK (acknowledge) was requested by the data packet transmitter radio, send the ACK back to it and display "ACK sent" on the serial monitor
			if (radio.ACK_REQUESTED)
			{
				radio.sendACK();
				Serial.println(" - ACK sent");
			}
      
			//Now the receiving Moteino can do something with the data sent in from the transmitter.
			//In the packet received, the index 0 of the array holds the digital pin number and index 2 its HIGH/LOW status
			//If it matches the next condition, set the pin accordingly, and blink the local led for 100ms
			if (packet[0]=='4' && packet[2]=='1')
			{
				Blink(LED,100);
				packet == "";
			}
		} 
		//If the destination radio was not this one, show the 3 requests for ACK from the transmitter radio
		else 
		{
			Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
			if (promiscuousMode)
			{
				Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
			}
			
			for (byte i = 0; i < radio.DATALEN; i++)
			{
				packet[i] = (char) radio.DATA[i];
				Serial.print (packet[i]);
			}

			Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
			byte theNodeID = radio.SENDERID;
			Serial.println("; Not for me");
		}
	} 
}


 
void Blink(byte PIN, int DELAY_MS)//The led blinking function
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}

Merci beaucoup!