Hello,
First, i want to thank you all for this forum and participations.
I'm a beginner in arduino and eletronics (since few weeks).
I faced an issue and i can't find any way to resolve i checked lots of videos and tutorials but still no solution.
Let me explain :
My project is simple -->to drive a 2 wheels robot.
1)
I use a transmitter with just a joystick, an NRF24L01+ and an arduino nano (connected on my computer or with an extenal 9V battery).
Here is my code :
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN du module wireless
int VRx = A5; // Pin A5, Axe des X du Joystick --> entre 0 (gauche) et 1023 (droite)
int VRy = A4; // Pin A4, Axe des Y du Joystick --> entre 1023 (bas) et 0 (haut)
int SW = 6; // Pin D6 pour le bouton du Joystick
int vX = 0;
int vY = 0;
int SW_state = 0;
const byte address[6] = "00001"; // Canal de communication entre les modules wireless
void setup() {
Serial.begin(115200);
// initialisation pour le module Wireless
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
// initialisation des pin du Joystick
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
}
void loop() {
radio.stopListening();
vX = analogRead(VRx);
vY = analogRead(VRy);
SW_state = digitalRead(SW);
radio.write(&vX, sizeof(vX));
radio.write(&vY, sizeof(vY));
radio.write(&SW_state, sizeof(SW_state));
Serial.print("X: ");
Serial.print(vX);
Serial.print(" | Y: ");
Serial.print(vY);
Serial.print(" | Bouton: ");
Serial.println(SW_state);
delay(100);
}
and the result in my Serial Monitor of this code is giving a stable information as follow :
X: 517 | Y: 513 | Bouton: 1
X: 517 | Y: 513 | Bouton: 1
X: 518 | Y: 513 | Bouton: 1
X: 517 | Y: 513 | Bouton: 1
X: 517 | Y: 513 | Bouton: 1
X: 517 | Y: 513 | Bouton: 1
X: 517 | Y: 513 | Bouton: 1
This is OK.
2)
I use a receiver with a NRF24L01+, an arduino Uno (and a Shield Pololu to manage motors) (connected on my computer or with an extenal 9V battery).
Here is my code :
#include "DualVNH5019MotorShield.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
DualVNH5019MotorShield md; // declaration pour le shield Pololu
RF24 radio(5, 3); // CE, CSN du module wireless
const byte address[6] = "00001"; // Canal de communication entre les modules wireless
int vX = 0;
int vY = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
void setup()
{
Serial.begin(115200);
md.init(); // initialisation pour utiliser les fonctions du shield
// initialisation pour le module Wireless
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
}
void loop()
{
radio.startListening();
while (radio.available()) {
radio.read(&vX, sizeof(vX));
radio.read(&vY, sizeof(vY));
radio.read(&SW_state, sizeof(SW_state));
mapX = map(vX, 0, 1023, -200, 200);
mapY = map(vY, 0, 1023, 200, -200);
if (mapY > 50) { // en avant
md.setSpeeds(mapY,mapY);
}
if (mapY < -50) { // en arriere
md.setSpeeds(mapY,mapY);
}
if ((mapX > 50) && (mapY > 50)) { // en avant a droite
md.setM1Speed(mapY-mapX); // moteur de droite
md.setM2Speed(mapY); // moteur de gauche
}
if ((mapX < -50) && (mapY > 50)) { // en avant a gauche
md.setM1Speed(mapY); // moteur de droite
md.setM2Speed(mapY+mapX); // moteur de gauche
}
if ((mapX > 50) && (mapY < -50)) { // en arriere a droite
md.setM1Speed(mapY+mapX); // moteur de droite
md.setM2Speed(mapY); // moteur de gauche
}
if ((mapX < -50) && (mapY < -50)) { // en arriere a gauche
md.setM1Speed(mapY); // moteur de droite
md.setM2Speed(mapY-mapX); // moteur de gauche
}
if ((mapY >= -50) && (mapY <= 50)) { // a l'arret
md.setSpeeds(0,0);
}
}
Serial.print("X: ");
Serial.print(mapX);
Serial.print(" | Y: ");
Serial.print(mapY);
Serial.print(" | Bouton: ");
Serial.println(SW_state);
delay(100);
}
and the result in my Serial Monitor of this code is giving a NOT stable information as follow :
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: -200 | Y: 200 | Bouton: 0
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: -200 | Y: 200 | Bouton: 0
X: -200 | Y: 200 | Bouton: 0
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
X: 2 | Y: 0 | Bouton: 1
When i move the joystick, it works well but sometimes data like above is coming.
For more stability, i read some tutorials and forum that it is better to add capacitor --> what i did !
I added a 10uF capacitor on each side between GND and VCC.
I also try to change my NRF24L01 modules in case of any degraded module, but still the same.
I really can't find anything else to do if you have any idea to share with me would be great for me to try ...
Thank you for reading me and for your help.
Cheers !!