AttachIntrerrupt stops NRF24L01+ from working – ARDUINO

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

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.

  1. #include <SPI.h>
  2. #include <nRF24L01.h>
  3. #include <RF24.h>
  4. RF24 radio(7, 8); // CE, CSN
  5. const byte address[6] = "00001";
  6. struct InputData // define stuct
  7. {
  8. int x;
  9. int y;
  10. };
  11. InputData data;
  12. // Motor A connections
  13. int motor_enA = 9;
  14. int motor_in1 = 10;
  15. int motor_in2 = 6;
  16. int encoder1 = 2;
  17. int encoder2 = 3;
  18. int counter = 0;
  19. int angle = 0;
  20. void setup() {
  21. Serial.begin(9600);
  22. radio.begin();
  23. radio.openReadingPipe(1, address);
  24. radio.setPALevel(RF24_PA_MIN);
  25. radio.startListening();
  26. // Set all the motor control pins to outputs
  27. pinMode(motor_enA, OUTPUT);
  28. pinMode(motor_in1, OUTPUT);
  29. pinMode(motor_in2, OUTPUT);
  30. // Turn off motors - Initial state
  31. digitalWrite(motor_in1, LOW);
  32. digitalWrite(motor_in2, LOW);
  33. analogWrite(motor_enA, 255);
  34. pinMode (encoder1, INPUT);
  35. pinMode (encoder2, INPUT);
  36. attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
  37. }
  38. void loop() {
  39. readEncoder();
  40. if (radio.available()) {
  41. radio.read(&data, sizeof(data));
  42. // Serial.println(data.y);
  43. if (data.y > 5) {
  44. digitalWrite(motor_in1, HIGH);
  45. digitalWrite(motor_in2, LOW);
  46. }
  47. else if (data.y < -5) {
  48. digitalWrite(motor_in1, LOW);
  49. digitalWrite(motor_in2, HIGH);
  50. }
  51. else {
  52. digitalWrite(motor_in1, LOW);
  53. digitalWrite(motor_in2, LOW);
  54. }
  55. }
  56. if(counter>1){
  57. counter=0;
  58. angle+=2;
  59. }else if(counter<-1){
  60. counter=0;
  61. angle-=2;
  62. }
  63. Serial.print("Position: ");
  64. Serial.println(angle);
  65. }
  66. void readEncoder()
  67. {
  68. if(digitalRead(encoder1)==HIGH){
  69. int b = digitalRead(encoder2);
  70. if(b>0){
  71. counter++;
  72. }
  73. else{
  74. counter--;
  75. }
  76. }
  77. }

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

答案1

得分: 0

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

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

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

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

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:

确定