Advertisement
RybaSG

szyfr

Jun 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.95 KB | None | 0 0
  1. static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
  2. static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
  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
  4. 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)
  5. 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
  6. 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)
  7. 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
  8.  
  9. #define RIGHT 1
  10. #define LEFT -1
  11.  
  12. int EncoderDirection = 0;
  13. int State = 0;
  14. int State1Flag = 0;
  15. int ZeroFlag = 0;
  16.  
  17. bool CykadelkoState = true;
  18.  
  19. int Position = 0;
  20. int OldPosition = 0;
  21.  
  22. int PRINT = 1;
  23.  
  24. int i = 0;
  25.  
  26. #define Cykadelko 6
  27. #define Elektromagnes 9
  28. #define electrolockOne 10
  29. #define electrolockTwo 11
  30. #define electrolockThree 12
  31.  
  32. void Cykadlo(int cykniecia);
  33.  
  34. void setup()
  35. {
  36.   pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  37.   pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  38.   pinMode(Cykadelko, OUTPUT);
  39.   digitalWrite(Cykadelko, HIGH);
  40.  
  41.   pinMode(Elektromagnes, OUTPUT);
  42.   pinMode(electrolockOne, OUTPUT);
  43.   pinMode(electrolockTwo, OUTPUT);
  44.   pinMode(electrolockThree, OUTPUT);
  45.  
  46.   digitalWrite(electrolockOne, HIGH);
  47.   digitalWrite(electrolockTwo, HIGH);
  48.   digitalWrite(electrolockThree, HIGH);
  49.  
  50.   attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  51.   attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  52.   Serial.begin(9600); // start the serial monitor link
  53.  
  54.   digitalWrite(Elektromagnes, HIGH);
  55. }
  56.  
  57. void PinA()
  58. {
  59.   cli(); //stop interrupts happening before we read pin values
  60.   reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  61.   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
  62.     encoderPos --; //decrement the encoder's position count
  63.     if (encoderPos > 24)
  64.       encoderPos = 24;
  65.     bFlag = 0; //reset flags for the next turn
  66.     aFlag = 0; //reset flags for the next turn
  67.   }
  68.   else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  69.   sei(); //restart interrupts
  70. }
  71.  
  72. void PinB()
  73. {
  74.   cli(); //stop interrupts happening before we read pin values
  75.   reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  76.   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
  77.     encoderPos ++; //increment the encoder's position count
  78.     if (encoderPos > 24)
  79.       encoderPos = 0;
  80.     bFlag = 0; //reset flags for the next turn
  81.     aFlag = 0; //reset flags for the next turn
  82.   }
  83.   else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  84.   sei(); //restart interrupts
  85. }
  86.  
  87. void loop()
  88. {
  89.   if(oldEncPos != encoderPos)
  90.   {
  91.     Serial.println(Position);
  92.     oldEncPos = encoderPos;
  93.   }
  94.  
  95.   if (oldEncPos > 24 || encoderPos > 24)
  96.   {
  97.     oldEncPos = 0;
  98.     encoderPos = 0;
  99.   }
  100.  
  101.   // position 0-100 range
  102.   Position = map(encoderPos, 0, 24, 0, 100);
  103.   // Check direction
  104.   if ( Position-OldPosition > 0 )
  105.   {
  106.     EncoderDirection = RIGHT;
  107.   }
  108.   else if (Position-OldPosition < 0)
  109.   {
  110.     EncoderDirection = LEFT;
  111.   }
  112.   else
  113.   {
  114.     EncoderDirection = 0;
  115.   }
  116.  
  117.  
  118.   switch(State)
  119.   {
  120.     case 0: // start, obrót w prawo do momentu 20-28
  121.       if ( PRINT == 1 )
  122.       {
  123.         PRINT = 0;
  124.         Serial.println("Start zagadki, etap 1");
  125.       }
  126.  
  127.       if(Position == 0 || OldPosition == 0)
  128.       {
  129.         EncoderDirection = RIGHT;
  130.       }
  131.  
  132.       if(EncoderDirection == RIGHT && EncoderDirection != 0)
  133.       {
  134.         if(Position >= 20 && Position <= 28)
  135.         {
  136.           Cykadlo(1);
  137.           Serial.println("Przejscie do etapu 2");
  138.           PRINT = 1;
  139.           State = 1;
  140.         }
  141.         else if (Position > 28)
  142.         {
  143.           //Ponowne rozpoczecie zagadki
  144.           Cykadlo(5);
  145.           Serial.println("Etap 1 - Blad, za daleko, wroc na 0");
  146.           PRINT = 1;  
  147.           State = 0;
  148.           //Trzeba wrócić do 0
  149.           while(encoderPos != 0)
  150.           {
  151.             delay(100);
  152.             Serial.println(encoderPos);
  153.           }
  154.           Cykadlo(2);
  155.           Serial.println("Powrot do pozycji 0");
  156.         }
  157.       }
  158.       else if (EncoderDirection == LEFT)
  159.       {
  160.         //Ponowne rozpoczecie zagadki
  161.         Cykadlo(5);
  162.         Serial.println("Etap 1- Blad, zly kierunek , wroc na 0");
  163.         PRINT = 1;  
  164.         State = 0;
  165.         //Trzeba wrócić do 0
  166.         while(encoderPos != 0)
  167.         {
  168.           delay(100);
  169.           Serial.println(encoderPos);
  170.         }
  171.         Cykadlo(2);
  172.         Serial.println("Powrot do pozycji 0");
  173.       }
  174.           break;
  175.  
  176.     case 1: // etap 2, obrót w lewo
  177.  
  178.       if ( PRINT == 1 )
  179.       {
  180.         PRINT = 0;
  181.         Serial.println("Etap 2, obracaj w lewo");
  182.       }
  183.  
  184.       if(Position == 0 || OldPosition == 0)
  185.       {
  186.         EncoderDirection = LEFT;
  187.         ZeroFlag = 1;
  188.       }
  189.  
  190.       if(EncoderDirection == LEFT || ZeroFlag == 1)
  191.       {
  192.         ZeroFlag = 0;
  193.         if(Position <= 52 && Position >= 40)
  194.         {
  195.           Cykadlo(1);
  196.           Serial.println("Przejscie do etapu 3");
  197.           PRINT = 1;
  198.           State = 2;
  199.           State1Flag = 1;
  200.         }
  201.         else if (Position < 40 && State1Flag == 1 )
  202.         {
  203.           //Ponowne rozpoczecie zagadki
  204.           Cykadlo(5);
  205.           Serial.println("Etap 2 - Blad, za daleko, wroc na 0");
  206.           PRINT = 1;  
  207.           State = 0;
  208.           State1Flag = 0;
  209.           ZeroFlag = 0;
  210.           //Trzeba wrócić do 0
  211.           while(encoderPos != 0)
  212.           {
  213.             delay(100);
  214.             Serial.println(encoderPos);
  215.           }
  216.           Cykadlo(2);
  217.           Serial.println("Powrot do pozycji 0");
  218.         }
  219.       }
  220.       else if (EncoderDirection == RIGHT)
  221.       {
  222.         //Ponowne rozpoczecie zagadki
  223.         Cykadlo(5);
  224.         Serial.println("Etap 2 - Blad, zly kierunek , wroc na 0");
  225.         PRINT = 1;  
  226.         State = 0;
  227.         State1Flag = 0;
  228.         ZeroFlag = 0;
  229.         //Trzeba wrócić do 0
  230.         while(encoderPos != 0)
  231.         {
  232.           delay(100);
  233.           Serial.println(encoderPos);
  234.         }
  235.         Cykadlo(2);
  236.         Serial.println("Powrot do pozycji 0");
  237.       }
  238.           break;
  239.  
  240.     case 2: // ostatni etap, w prawo
  241.       if ( PRINT == 1 )
  242.       {
  243.         PRINT = 0;
  244.         Serial.println("Etap 3, ostatni, krec w prawo");
  245.       }
  246.  
  247.       if(Position == 0 || OldPosition == 0)
  248.       {
  249.         EncoderDirection = RIGHT;
  250.       }
  251.  
  252.       if(EncoderDirection == RIGHT)
  253.       {
  254.         if(Position == 0) // detect zero
  255.         {
  256.           ZeroFlag = 1;
  257.         }
  258.  
  259.         if(Position >= 70 && Position <= 80 && ZeroFlag == 1)
  260.         {
  261.           Cykadlo(1);
  262.           Serial.println("Sezamie otworz sie");
  263.           PRINT = 1;
  264.           State = 3;
  265.         }
  266.         else if (Position > 80 && ZeroFlag == 1)
  267.         {
  268.           //Ponowne rozpoczecie zagadki
  269.           Cykadlo(5);
  270.           Serial.println("Etap 3 - Blad, za daleko , wroc na 0");
  271.           PRINT = 1;  
  272.           State = 0;
  273.           ZeroFlag = 0;
  274.           //Trzeba wrócić do 0
  275.           while(encoderPos != 0)
  276.           {
  277.             delay(100);
  278.             Serial.println(encoderPos);
  279.           }
  280.           Cykadlo(2);
  281.           Serial.println("Powrot do pozycji 0");
  282.         }
  283.       }
  284.       else if (EncoderDirection == LEFT)
  285.       {
  286.         //Ponowne rozpoczecie zagadki
  287.         Cykadlo(5);
  288.         Serial.println("Etap 3 - Blad, zly kierunek , wroc na 0");
  289.         PRINT = 1;  
  290.         State = 0;
  291.         ZeroFlag = 0;
  292.         //Trzeba wrócić do 0
  293.         while(encoderPos != 0)
  294.         {
  295.           delay(100);
  296.           Serial.println(encoderPos);
  297.         }
  298.         Cykadlo(2);
  299.         Serial.println("Powrot do pozycji 0");
  300.       }
  301.           break;
  302.     case 3:
  303.         digitalWrite(electrolockOne, LOW);
  304.         delay(1000);
  305.         digitalWrite(electrolockTwo, LOW);
  306.         delay(1000);
  307.         digitalWrite(electrolockThree, LOW);
  308.         digitalWrite(Elektromagnes, LOW);
  309.        
  310.        
  311.       Serial.println("Zamki otwarte");
  312.       Serial.println("Delay na 5 minut");
  313.       int j = 0;
  314.       for (int i = 0; i < 1; i++)
  315.       {
  316.         for(j = 0; j < 60; j++)
  317.         {
  318.           delay(1000);
  319.         }
  320.       }
  321.      
  322.         digitalWrite(electrolockOne, HIGH);
  323.         digitalWrite(electrolockTwo, HIGH);
  324.         digitalWrite(electrolockThree, HIGH);
  325.         digitalWrite(Elektromagnes, HIGH);
  326.        
  327.       State = 0;
  328.  
  329.       Serial.println("Koniec, wroc na 0");
  330.       while(encoderPos != 0)
  331.       {
  332.         delay(100);
  333.         Serial.println(encoderPos);
  334.       }
  335.       Cykadlo(2);
  336.  
  337.       break;
  338.  
  339.   } // end of switch
  340.  
  341.   OldPosition = Position;
  342.  
  343. }
  344.  
  345. void Cykadlo(int cykniecia)
  346. {
  347.   for(int i = 0; i < cykniecia; i++)
  348.   {
  349.     CykadelkoState = !CykadelkoState;
  350.     digitalWrite(Cykadelko, CykadelkoState);
  351.     delay(300);
  352.   }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement