AttachIntrerrupt stops NRF24L01+ from working – ARDUINO

huangapple go评论50阅读模式
英文:

AttachIntrerrupt stops NRF24L01+ from working - ARDUINO

问题

如果我删除 attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); ,代码可以运行。但一旦添加了这行,radio.available 下面的任何内容都不会运行。

英文:

If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.

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

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

const byte address[6] = "00001";


struct InputData  // define stuct
{
   int x;
   int y;
};

InputData data;


// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;


int encoder1 = 2;
int encoder2 = 3;


int counter = 0;
int angle = 0;


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


  // Set all the motor control pins to outputs
  pinMode(motor_enA, OUTPUT);
  pinMode(motor_in1, OUTPUT);
  pinMode(motor_in2, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(motor_in1, LOW);
  digitalWrite(motor_in2, LOW);
  analogWrite(motor_enA, 255);


  pinMode (encoder1, INPUT);
  pinMode (encoder2, INPUT);

  attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}

void loop() {
  readEncoder();
  if (radio.available()) {
    radio.read(&data, sizeof(data));
//    Serial.println(data.y);
    if (data.y > 5) {
      digitalWrite(motor_in1, HIGH);
      digitalWrite(motor_in2, LOW);
    }
    else if (data.y < -5) {
      digitalWrite(motor_in1, LOW);
      digitalWrite(motor_in2, HIGH);
  
    }
    else {
      digitalWrite(motor_in1, LOW);
      digitalWrite(motor_in2, LOW);
    }
  }
  
  if(counter>1){
    counter=0;
    angle+=2;
  }else if(counter<-1){
    counter=0;
    angle-=2;
  }
  Serial.print("Position: ");
  Serial.println(angle);
}


void readEncoder()
{
  if(digitalRead(encoder1)==HIGH){
    int b = digitalRead(encoder2);
    if(b>0){
      counter++;
    }
    else{
      counter--;
    }
  }
}

I have tried removing and adding the line, as described above^^

答案1

得分: 0

根据 Hcheung 的建议,将计数器设为 volatile 并从循环中移除 readEncoder();。
我稍微简化了 ISR readEncoder();

volatile int counter = 0;
[....] 
void readEncoder() {
  //if(digitalRead(encoder1)==HIGH){  //我们在这里是因为 digitalRead(encoder1) = HIGH!
    if(digitalRead(encoder2)) counter++;
    else counter--;
}
英文:

as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();

volatile int counter = 0;
[....] 
void readEncoder() {
  //if(digitalRead(encoder1)==HIGH){  //we are precisely here because digitalRead(encoder1) = HIGH !
    if(digitalRead(encoder2)) counter++;
    else counter--;
}

huangapple
  • 本文由 发表于 2023年2月19日 23:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定