Advertisement
RybaSG

dzialaszyfr

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