fork(1) download
  1. //----------------------------------------------------------------
  2. #include <Arduino.h>
  3.  
  4. static int PIN_A PB0; // Encoder PIN A
  5. static int PIN_B PB1; // Encoder PIN B
  6. static int BUTTON PA7; // Button PIN
  7.  
  8. volatile bool aFlag = 0;
  9. volatile bool bFlag = 0;
  10. volatile uint16_t reading = 0;
  11. volatile int16_t count = 0; // +1 from CW and -1 from CCW (for sample)
  12. volatile bool button = 0; // Button pressed flag (for event out of interrupt)
  13. volatile bool cw_flag = 0; // Clockwise flag (for event out of interrupt)
  14. volatile bool ccw_flag = 0; // Counterclockwise flag (for event out of interrupt)
  15.  
  16. void CW(){
  17. noInterrupts();
  18. reading = GPIOB->IDR & 0x3;
  19. if(reading == 0b0000000000000011 && aFlag)
  20. {
  21. bFlag = 0;
  22. aFlag = 0;
  23. cw_flag = 1; // Set the clockwise rotation flag
  24. count ++; // Counter +1
  25. }
  26. else if (reading == 0b0000000000000001) bFlag = 1;
  27. interrupts();
  28. }
  29.  
  30. void CCW(){
  31. noInterrupts();
  32. reading = GPIOB->IDR & 0x3;
  33. if (reading == 0b0000000000000011 && bFlag)
  34. {
  35. bFlag = 0;
  36. aFlag = 0;
  37. ccw_flag = 1; // Set the Counterclockwise rotation flag
  38. count --; // Counter -1
  39. }
  40. else if (reading == 0b0000000000000010) aFlag = 1;
  41. interrupts();
  42. }
  43.  
  44. void BUT(){
  45. noInterrupts();
  46. button = 1; // Set button pressed flag
  47. interrupts();
  48. }
  49.  
  50. void setup() {
  51. Serial1.begin(115200);
  52. Serial.println("");
  53. Serial.println("Please turn the encoder!");
  54. pinMode(PIN_A, INPUT_PULLUP);
  55. pinMode(PIN_B, INPUT_PULLUP);
  56. pinMode(BUTTON, INPUT_PULLUP);
  57. attachInterrupt(digitalPinToInterrupt(PIN_A), CW, RISING);
  58. attachInterrupt(digitalPinToInterrupt(PIN_B), CCW, RISING);
  59. attachInterrupt(digitalPinToInterrupt(BUTTON), BUT, RISING);
  60. }
  61.  
  62. void loop() {
  63. if (cw_flag==1) // out of interrupt handling CW
  64. {
  65. Serial.println("-->");
  66. Serial.println(count);
  67. cw_flag = 0;
  68. }
  69. if (ccw_flag == 1) // out of interrupt handling CCW
  70. {
  71. Serial.println("<--");
  72. Serial.println(count);
  73. ccw_flag = 0;
  74. }
  75. if (button == 1) // out of interrupt handling button
  76. {
  77. Serial.println("PUSHED");
  78. button = 0;
  79. }
  80. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:10: fatal error: Arduino.h: No such file or directory
 #include <Arduino.h>
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty