Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 byte 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 RIGHT 1
- #define LEFT -1
- int EncoderDirection = 0;
- int State = 0;
- int State1Flag = 0;
- int ZeroFlag = 0;
- int ResetFlag = 0;
- bool CykadelkoState = true;
- int Position = 0;
- int OldPosition = 0;
- int PRINT = 1;
- int i = 0;
- #define Cykadelko 6
- #define Elektromagnes 9
- #define electrolockOne 10
- #define electrolockTwo 11
- #define electrolockThree 12
- void Cykadlo(int cykniecia);
- void setup()
- {
- 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)
- pinMode(Cykadelko, OUTPUT);
- digitalWrite(Cykadelko, HIGH);
- pinMode(Elektromagnes, OUTPUT);
- pinMode(electrolockOne, OUTPUT);
- pinMode(electrolockTwo, OUTPUT);
- pinMode(electrolockThree, OUTPUT);
- digitalWrite(electrolockOne, HIGH);
- digitalWrite(electrolockTwo, HIGH);
- digitalWrite(electrolockThree, HIGH);
- 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(9600); // start the serial monitor link
- digitalWrite(Elektromagnes, HIGH);
- }
- 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
- if (encoderPos > 24)
- encoderPos = 24;
- 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
- if (encoderPos > 24)
- encoderPos = 0;
- 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()
- {
- if(oldEncPos != encoderPos)
- {
- Serial.println(encoderPos);
- oldEncPos = encoderPos;
- }
- if (oldEncPos > 24 || encoderPos > 24)
- {
- oldEncPos = 0;
- encoderPos = 0;
- }
- // position 0-100 range
- Position = map(encoderPos, 0, 24, 0, 100);
- // Check direction
- if ( Position-OldPosition > 0 )
- {
- EncoderDirection = RIGHT;
- }
- else if (Position-OldPosition < 0)
- {
- EncoderDirection = LEFT;
- }
- else
- {
- EncoderDirection = 0;
- }
- if (ResetFlag == 1)
- {
- ResetFlag = 0;
- EncoderDirection = RIGHT;
- }
- switch(State)
- {
- case 0: // start, obrót w prawo do momentu 20-28
- if ( PRINT == 1 )
- {
- PRINT = 0;
- Serial.println("Start zagadki, etap 1");
- }
- // if(Position == 0 || OldPosition == 0)
- if(encoderPos == 0)
- {
- EncoderDirection = RIGHT;
- }
- if(EncoderDirection == RIGHT)
- {
- if(Position >= 20 && Position <= 28)
- {
- Cykadlo(1);
- Serial.println("Przejscie do etapu 2");
- PRINT = 1;
- State = 1;
- }
- else if (Position > 28 && encoderPos != 24) // nie powinno się nigdy spełnić
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 1 - Blad, za daleko, wroc na 0");
- PRINT = 1;
- State = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0 )
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- //Cykadlo(2);
- State1Flag = 0;
- ResetFlag = 1;
- Serial.println("Powrot do pozycji 0");
- }
- }
- else if (EncoderDirection == LEFT)
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 1- Blad, zly kierunek , wroc na 0");
- PRINT = 1;
- State = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0)
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- //Cykadlo(2);
- State1Flag = 0;
- ResetFlag = 1;
- Serial.println("Powrot do pozycji 0");
- }
- break;
- case 1: // etap 2, obrót w lewo
- if ( PRINT == 1 )
- {
- PRINT = 0;
- Serial.println("Etap 2, obracaj w lewo");
- }
- if(Position == 0 || OldPosition == 0)
- {
- EncoderDirection = LEFT;
- ZeroFlag = 1;
- }
- if(EncoderDirection == LEFT || ZeroFlag == 1)
- {
- ZeroFlag = 0;
- if(Position <= 52 && Position >= 40)
- {
- Cykadlo(1);
- Serial.println("Przejscie do etapu 3");
- PRINT = 1;
- State = 2;
- State1Flag = 1;
- }
- else if (Position < 40 && State1Flag == 1 )
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 2 - Blad, za daleko, wroc na 0");
- PRINT = 1;
- State = 0;
- State1Flag = 0;
- ZeroFlag = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0)
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- State1Flag = 0;
- ResetFlag = 1;
- //Cykadlo(2);
- Serial.println("Powrot do pozycji 0");
- }
- }
- else if (EncoderDirection == RIGHT)
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 2 - Blad, zly kierunek , wroc na 0");
- PRINT = 1;
- State = 0;
- State1Flag = 0;
- ZeroFlag = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0)
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- State1Flag = 0;
- ResetFlag = 1;
- //Cykadlo(2);
- Serial.println("Powrot do pozycji 0");
- }
- break;
- case 2: // ostatni etap, w prawo
- if ( PRINT == 1 )
- {
- PRINT = 0;
- Serial.println("Etap 3, ostatni, krec w prawo");
- }
- if(Position == 0 || OldPosition == 0)
- {
- EncoderDirection = RIGHT;
- }
- if(EncoderDirection == RIGHT)
- {
- if(Position == 0) // detect zero
- {
- ZeroFlag = 1;
- }
- if(Position >= 66 && Position <= 80 && ZeroFlag == 1)
- {
- Cykadlo(1);
- Serial.println("Sezamie otworz sie");
- PRINT = 1;
- State = 3;
- }
- else if (Position > 80 && ZeroFlag == 1)
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 3 - Blad, za daleko , wroc na 0");
- PRINT = 1;
- State = 0;
- ZeroFlag = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0)
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- //Cykadlo(2);
- Serial.println("Powrot do pozycji 0");
- State1Flag = 0;
- ResetFlag = 1;
- }
- }
- else if (EncoderDirection == LEFT)
- {
- //Ponowne rozpoczecie zagadki
- Cykadlo(5);
- Serial.println("Etap 3 - Blad, zly kierunek , wroc na 0");
- PRINT = 1;
- State = 0;
- ZeroFlag = 0;
- //Trzeba wrócić do 0
- while(encoderPos != 0 )
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- //Cykadlo(2);
- State1Flag = 0;
- ResetFlag = 1;
- Serial.println("Powrot do pozycji 0");
- }
- break;
- case 3:
- digitalWrite(electrolockOne, LOW);
- delay(1000);
- digitalWrite(electrolockTwo, LOW);
- delay(1000);
- digitalWrite(electrolockThree, LOW);
- digitalWrite(Elektromagnes, LOW);
- Serial.println("Zamki otwarte");
- Serial.println("Delay na 5 minut");
- int j = 0;
- for (int i = 0; i < 1; i++)
- {
- for(j = 0; j < 60; j++)
- {
- delay(1000);
- }
- }
- digitalWrite(electrolockOne, HIGH);
- digitalWrite(electrolockTwo, HIGH);
- digitalWrite(electrolockThree, HIGH);
- digitalWrite(Elektromagnes, HIGH);
- State = 0;
- Serial.println("Koniec, wroc na 0");
- while(encoderPos != 0)
- {
- delay(100);
- Serial.println(encoderPos);
- // if( encoderPos == 1 || encoderPos == 24 )
- // break;
- }
- //Cykadlo(2);
- State1Flag = 0;
- ResetFlag = 1;
- break;
- } // end of switch
- OldPosition = Position;
- }
- void Cykadlo(int cykniecia)
- {
- for(int i = 0; i < cykniecia; i++)
- {
- CykadelkoState = !CykadelkoState;
- digitalWrite(Cykadelko, CykadelkoState);
- delay(300);
- }
- } // dupa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement