Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "U8glib.h" // U8glib library for the OLED
- #include "FastLED.h"
- static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
- static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
- 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
- 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)
- 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
- 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)
- 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
- #define NUM_LEDS 100 // Hur många cues i en seqvens.
- #define DATA_PIN 13 // Vilken pin som signalen skickas ut via
- CRGB leds[NUM_LEDS];
- U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
- const int buttonPin = 8; // den i/o som knappen är kopplad till.
- 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.
- // variables will change:.
- int buttonState = 0; // vari
- void draw(void)
- {
- u8g.setFont(u8g_font_profont17r); // select font
- u8g.drawStr(18, 12, "Delay ms");
- u8g.setPrintPos(45,40);
- u8g.drawRFrame(15, 20, 100, 30, 10); // draws frame with rounded edges
- u8g.println(encoderPos); //Prints the voltage
- }
- void setup() {
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- delay(100);
- FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
- pinMode(13, OUTPUT);
- pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
- pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
- attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
- attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
- Serial.begin(115200); // start the serial monitor link
- }
- void PinA(){
- cli(); //stop interrupts happening before we read pin values
- reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
- 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
- encoderPos --; //decrement the encoder's position count
- bFlag = 0; //reset flags for the next turn
- aFlag = 0; //reset flags for the next turn
- }
- else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
- sei(); //restart interrupts
- }
- void PinB(){
- cli(); //stop interrupts happening before we read pin values
- reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
- 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
- encoderPos ++; //increment the encoder's position count
- bFlag = 0; //reset flags for the next turn
- aFlag = 0; //reset flags for the next turn
- }
- else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
- sei(); //restart interrupts
- }
- void loop(){
- digitalWrite(ledPin, LOW);
- if(oldEncPos != encoderPos) {
- Serial.println(encoderPos);
- oldEncPos = encoderPos;
- }
- u8g.firstPage();
- do
- {
- draw();
- }
- while( u8g.nextPage() );
- buttonState = digitalRead(buttonPin);
- if (buttonState == HIGH) {
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
- // Turn our current led on to white, then show the leds
- leds[whiteLed] = CRGB::White;
- // Show the leds (only one of which is set to white, from above)
- FastLED.show();
- delay (encoderPos);
- leds[whiteLed] = CRGB::Black;
- }
- } else {
- // turn LED off:
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment