Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. #include <AccelStepper.h>
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include "menuStuff.h"
  7. #include "functions.h"
  8.  
  9. /*
  10. AccelStepper Website:
  11. http://www.airspayce.com/mikem/arduino/AccelStepper/
  12. Direct Download:
  13. http://www.airspayce.com/mikem/arduino/AccelStepper/AccelStepper-1.57.zip
  14. Adafruit GFX:
  15. https://github.com/adafruit/Adafruit-GFX-Library
  16. Adafruit SSD1306:
  17. https://github.com/adafruit/Adafruit_SSD1306
  18. */
  19.  
  20.  
  21. #define OLED_RESET 4
  22. Adafruit_SSD1306 display(OLED_RESET);
  23.  
  24. //~~~~~~~~rotary encoder stuff~~~~~~~~~~//
  25. volatile byte seqA = 0;
  26. volatile byte seqB = 0;
  27. volatile byte cnt1 = 0;
  28. volatile byte cnt2 = 0;
  29. volatile boolean right = false;
  30. volatile boolean left = false;
  31. volatile boolean button = false;
  32. //~~~~~~~~end of rotary encoder stuff~~~~~~~~~~//
  33.  
  34. int menuitem=1;
  35. int numCount=1;
  36.  
  37. AccelStepper stepper(AccelStepper::DRIVER, 3,2);
  38.  
  39.  
  40. void setup() {
  41.  
  42.  
  43. //~~~~~~~~rotary encoder stuff~~~~~~~~~~//
  44. pinMode(A8, INPUT);
  45. pinMode(A9, INPUT);
  46. pinMode(A10, INPUT);
  47.  
  48. // Enable internal pull-up resistors
  49. digitalWrite(A8, HIGH);
  50. digitalWrite(A9, HIGH);
  51. digitalWrite(A10, HIGH);
  52.  
  53. PCICR |= (1 << PCIE2); // 1. PCIE2: Pin Change Interrupt Enable K Group
  54. PCMSK2 = 0b00000111; // Enable Pin Change Interrupt for A8, A9, A10
  55. // references
  56. // https://www.allaboutcircuits.com/projects/how-to-use-a-rotary-encoder-in-a-mcu-based-project/
  57. // https://thewanderingengineer.com/2014/08/11/arduino-pin-change-interrupts/
  58. // http://forum.arduino.cc/index.php?topic=45239.30
  59.  
  60. //~~~~~~~~end of rotary encoder stuff~~~~~~~~~~//
  61.  
  62. //~~~~~~~~Stepper Stuff~~~~~~~~~
  63.  
  64. // defines the pin that enable/disable the motor
  65. stepper.setEnablePin(7);
  66. // inverts the enable pin. Change the third variable if your pin is not inverted
  67. stepper.setPinsInverted(false, false, true);
  68. stepper.setMaxSpeed(5000);
  69. // stepper.setAcceleration(1000);
  70. // deactivates the motor
  71. stepper.disableOutputs();
  72.  
  73. //~~~~~~~~end Stepper Stuff~~~~~~~~~
  74.  
  75. pinMode(8,INPUT);
  76. Serial.begin(9600);
  77. // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  78. display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
  79. display.setTextSize(2);
  80. display.setTextColor(WHITE);
  81. display.setCursor(0,0);
  82. display.println("Hello!");
  83. display.display();
  84. delay(1000);
  85.  
  86. display.clearDisplay();
  87. }
  88.  
  89. void loop() {
  90. menuSelect(menuCounter(4));
  91. }
  92.  
  93. void menuSelect(int menuitem)//main menu
  94. {
  95. button=false;
  96. int numItems=4;
  97. display.clearDisplay();
  98. display.setTextSize(2);
  99. display.setTextColor(WHITE);
  100. display.setCursor(40,0);
  101. display.println("Menu");
  102. display.drawLine(0,15,127,15,WHITE);
  103.  
  104. String mainMenu[] = {">Quick Access",">Program",">Manual",">Zero"};
  105. menuGeneration(numItems,mainMenu);
  106. if(button == true)//if button is pressed
  107. {
  108. delay(500); // time delay to ensure button is released
  109. button=false;//set button state to false, otherwise it will remain true. it is toggle.
  110. switch(menuitem){
  111. case 1:
  112. quickAccess();
  113. break;
  114. case 2:
  115. createSlide();
  116. break;
  117. case 3:
  118. manualMove();
  119. break;
  120. case 4:
  121. zeroSystem();
  122. break;
  123. }
  124. }
  125. }
  126.  
  127.  
  128. //Rotary encoder stuff. This will always run in the background allowing you to use the knob
  129. //as the primary control mechanism
  130. ISR (PCINT2_vect) {
  131.  
  132. // If interrupt is triggered by the button
  133. if (!digitalRead(A8)) {
  134.  
  135. button = true;}
  136.  
  137. // Else if interrupt is triggered by encoder signals
  138. else {
  139.  
  140. // Read A and B signals
  141. boolean A_val = digitalRead(A9);
  142. boolean B_val = digitalRead(A10);
  143.  
  144. // Record the A and B signals in seperate sequences
  145. seqA <<= 1;
  146. seqA |= A_val;
  147.  
  148. seqB <<= 1;
  149. seqB |= B_val;
  150.  
  151. // Mask the MSB four bits
  152. seqA &= 0b00001111;
  153. seqB &= 0b00001111;
  154.  
  155. // Compare the recorded sequence with the expected sequence
  156. if (seqA == 0b00001001 && seqB == 0b00000011) {
  157. cnt1++;
  158. left = true;
  159. }
  160.  
  161. if (seqA == 0b00000011 && seqB == 0b00001001) {
  162. cnt2++;
  163. right = true;
  164. }
  165. }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement