Transmit encoder knob pos using Rf24

I have two UNO r3 attached to RF24 radios.
transmitter has an encoder knob to transmit position.
receiver just receives it and displays number in serial monitor.

both programs are combined from two working examples.

i sure i have something formatted wrong.

transmitter knob


```cpp
#include <RF24.h>
#include <RF24_config.h>
#include <nRF24L01.h>
#include <printf.h>
#include <SPI.h>

RF24 radio(7,8); // CE, CSN
const byte address[6] = "00001";

enum PinAssignments {
  encoderPinA = 3,
  encoderPinB = 2,
  clearButton = 8
};

volatile unsigned int encoderPos = 1;
unsigned int lastReportedPos = 1;

boolean A_set = false;
boolean B_set = false;

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();//pinMode (en, OUTPUT);


  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  pinMode(clearButton, INPUT);
  digitalWrite(encoderPinA, HIGH); // turn on pullup resistor
  digitalWrite(encoderPinB, HIGH); // turn on pullup resistor
  digitalWrite(clearButton, HIGH);

  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoderA, CHANGE);
  // encoder pin on interrupt 1 (pin 3)
  attachInterrupt(1, doEncoderB, CHANGE);

  Serial.begin(9600);
}

void loop() {
  if (lastReportedPos != encoderPos) {
    Serial.print("Index:");
    Serial.print(encoderPos, DEC);
    Serial.println();
    lastReportedPos = encoderPos;
    radio.write(&encoderPos, sizeof(encoderPos));
  }
  if (digitalRead(clearButton) == LOW) {
    encoderPos = 1;
  }
}

// Interrupt on A changing state
void doEncoderA() {
  // Test transition
  A_set = digitalRead(encoderPinA) == HIGH;
  // and adjust counter + if A leads B
  encoderPos += (A_set != B_set) ? +1 : -1;
  if(encoderPos < 1) encoderPos = 300;
}

// Interrupt on B changing state
void doEncoderB() {
  // Test transition
  B_set = digitalRead(encoderPinB) == HIGH;
  // and adjust counter + if B follows A
  encoderPos += (A_set == B_set) ? +1 : -1;
  if(encoderPos > 300) encoderPos = 1;
}

receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    int encoderPos;
    radio.read(&encoderPos, sizeof(encoderPos));
    Serial.println(encoderPos);
  }
}

So what's the issue you see?

I get a bunch of zeros them some random numbers.

These shared variables must be declared volatile

boolean A_set = false;
boolean B_set = false;

Use an encoder library

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.