Kamuro

Firstlight

Jun 10th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1.  
  2. #include "U8glib.h" // U8glib library for the OLED
  3. #include "FastLED.h"
  4. static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
  5. static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
  6. volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
  7. volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
  8. volatile uint16_t encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
  9. volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
  10. volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
  11.  
  12. #define NUM_LEDS 100 // Hur många cues i en seqvens.
  13. #define DATA_PIN 13 // Vilken pin som signalen skickas ut via
  14. CRGB leds[NUM_LEDS];
  15.  
  16.  
  17.  
  18. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
  19.  
  20. const int buttonPin = 8; // den i/o som knappen är kopplad till.
  21. const int ledPin = 12; // Den i/o lysdioden är kopplad till. Tänds då knappen trycks in. Och lyser så länge seqvensen körs.
  22.  
  23. // variables will change:.
  24. int buttonState = 0; // vari
  25.  
  26. void draw(void)
  27. {
  28. u8g.setFont(u8g_font_profont17r); // select font
  29. u8g.drawStr(18, 12, "Delay ms");
  30. u8g.setPrintPos(45,40);
  31. u8g.drawRFrame(15, 20, 100, 30, 10); // draws frame with rounded edges
  32. u8g.println(encoderPos); //Prints the voltage
  33.  
  34. }
  35.  
  36. void setup() {
  37.  
  38. pinMode(ledPin, OUTPUT);
  39. // initialize the pushbutton pin as an input:
  40. pinMode(buttonPin, INPUT);
  41. delay(100);
  42. FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  43. pinMode(13, OUTPUT);
  44. pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  45. pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  46. attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  47. attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  48. Serial.begin(115200); // start the serial monitor link
  49. }
  50.  
  51. void PinA(){
  52. cli(); //stop interrupts happening before we read pin values
  53. reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  54. if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  55. encoderPos --; //decrement the encoder's position count
  56. bFlag = 0; //reset flags for the next turn
  57. aFlag = 0; //reset flags for the next turn
  58. }
  59. else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  60. sei(); //restart interrupts
  61. }
  62.  
  63. void PinB(){
  64. cli(); //stop interrupts happening before we read pin values
  65. reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  66. if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  67. encoderPos ++; //increment the encoder's position count
  68. bFlag = 0; //reset flags for the next turn
  69. aFlag = 0; //reset flags for the next turn
  70. }
  71. else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  72. sei(); //restart interrupts
  73. }
  74.  
  75. void loop(){
  76. digitalWrite(ledPin, LOW);
  77. if(oldEncPos != encoderPos) {
  78. Serial.println(encoderPos);
  79. oldEncPos = encoderPos;
  80. }
  81.  
  82. u8g.firstPage();
  83. do
  84. {
  85. draw();
  86. }
  87. while( u8g.nextPage() );
  88. buttonState = digitalRead(buttonPin);
  89. if (buttonState == HIGH) {
  90. // turn LED on:
  91. digitalWrite(ledPin, HIGH);
  92. for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
  93. // Turn our current led on to white, then show the leds
  94. leds[whiteLed] = CRGB::White;
  95.  
  96. // Show the leds (only one of which is set to white, from above)
  97. FastLED.show();
  98. delay (encoderPos);
  99. leds[whiteLed] = CRGB::Black;
  100. }
  101.  
  102.  
  103. } else {
  104. // turn LED off:
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment