Hi Guys,
I can control a Nema23 stepper motor with NRF24L01 modules. My transmitter is Arduino Nano and receiver is Arduino Mega 2560. I however have a problem using the encoders. Normally I can read an incremental encoder with attachinterrupt (PinChangeInt.h). When I combine this with NRF24, my communication does not work. I think the interrupts conflict somehow.
I found I can handle this problem by connecting IRQ pin but I confused a little bit.
Looking forward to your response.
My Transmitter Code:
#include "Arduino.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 2
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int data[2];
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT); digitalWrite(led, HIGH);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00001
//radio.openReadingPipe(1, addresses[0]); // 00002
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
// Read Joystick
int angleY = analogRead(A0); // Y Axis
int angleX = analogRead(A1); // X Axis
int angY = map(angleY, 0, 1023, -100, 100);
int angX = map(angleX, 0, 1023, -100, 100);
if (angX >= 90 || angX <= -90 || angY >= 90 || angY <= -90){
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
// Send Data to Receiver
data[0] = angX;
data[1] = angY;
radio.write(&data, sizeof(data));
// Control Radio Connection for Bi-Directional
//delay(5);
//radio.startListening();
//while (!radio.available());
}
My Receiver Code:
#include "Arduino.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Stepper.h>
#include <PinChangeInt.h>
//Stepper IGUS(1, STEPPER_STEP_PIN, STEPPER_DIR_PIN);
Stepper IGUS_1(STEPS_1, STEPPER_1_STEP_PIN, STEPPER_1_DIR_PIN);
//NRF24L01 Receiver
RF24 radio(48, 49); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int data[2];
int XVal = 0;
int YVal = 0;
int Elimit1 = 900; // Encoder Limit 1
int Elimit2 = 64535; // Encoder Limit 2
volatile unsigned int IGUS_1_Rot = 0;
void setup() {
Serial.begin(9600);
//NRF24L01
radio.begin();
//radio.openWritingPipe(addresses[0]); // 00002
radio.openReadingPipe(1, addresses[1]); // 00001
radio.setPALevel(RF24_PA_MIN);
// IGUS
pinMode(IGUS_1_Hall, INPUT);
pinMode(IGUS_1_Encoder_Index, INPUT); digitalWrite(IGUS_1_Encoder_Index, HIGH);
pinMode(IGUS_1_Encoder_A, INPUT); digitalWrite(IGUS_1_Encoder_A, HIGH);
pinMode(IGUS_1_Encoder_B, INPUT); digitalWrite(IGUS_1_Encoder_B, HIGH);
// External Interrupts to Encoders
PCintPort::attachInterrupt(IGUS_1_Encoder_A, Update_IGUS_1A, CHANGE);
PCintPort::attachInterrupt(IGUS_1_Encoder_B, Update_IGUS_1B, CHANGE);
// set the speed of the motors
IGUS_1.setSpeed(120);
delay(5);
radio.startListening();
}
void loop() {
if (radio.available()) {
radio.read(&data, sizeof(data));
XVal = data[0];
YVal = data[1];
if (XVal < -50 || XVal > 50 || YVal < -50 || YVal > 50){
IGUS_1.step(YVal);
}
// Control Radio Connection for Bi-Directional
//delay(5);
//radio.stopListening();
}
Serial.print(XVal);
Serial.print("\t");
Serial.println(YVal);
/*
Serial.print(IGUS_1_Rot);Serial.print(" | ");
Serial.print(IGUS_2_Rot);Serial.print(" | ");
Serial.print(IGUS_3_Rot);Serial.print(" | ");
Serial.print(IGUS_4_Rot);Serial.print(" | ");
Serial.println(IGUS_5_Rot);
*/
}
// IGUS_1_Encoder_A&B
void Update_IGUS_1A() {
if (digitalRead(IGUS_1_Encoder_A) == HIGH) { // found a low-to-high on channel A
if (digitalRead(IGUS_1_Encoder_B) == LOW) { // check channel B
IGUS_1_Rot++; // CW
}
else {
IGUS_1_Rot--; // CCW
}
}
else // found a high-to-low on channel A
{
if (digitalRead(IGUS_1_Encoder_B) == HIGH) { // check channel B
IGUS_1_Rot++; // CW
}
else {
IGUS_1_Rot--; // CCW
}
}
}
void Update_IGUS_1B() {
if (digitalRead(IGUS_1_Encoder_B) == HIGH) { // found a low-to-high on channel B
if (digitalRead(IGUS_1_Encoder_A) == HIGH) { // check channel A
IGUS_1_Rot++; // CW
}
else {
IGUS_1_Rot--; // CCW
}
}
else { // found a high-to-low on channel B
if (digitalRead(IGUS_1_Encoder_A) == LOW) { // check channel A
IGUS_1_Rot++; // CW
}
else {
IGUS_1_Rot--; // CCW
}
}
}