Advertisement
Zaganu

auto/perturbatii probleme

May 29th, 2020
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // definirea pinului de pas, respectiv de directie pentru motorul pas cu pas miscare verticala
  2. #define stepPin 48
  3. #define dirPin 46
  4. #define ls3in 32
  5. #define ls4in 22
  6.  
  7. // definirea pinului de pas, respectiv de directie pentru motorul pas cu pas miscare orizontala lopata
  8. #define stepPin2 44
  9. #define dirPin2 42
  10. #define ls2in 34
  11.  
  12. // definirea pinului de pas, respectiv de directie pentru motorul pas cu pas miscare orizontala masa
  13. #define stepPin3 40
  14. #define dirPin3 38
  15. #define ls1in 36
  16.  
  17. //////////////// senzori///////////////
  18. #define ls0in 52
  19. #define so1in 50 //intrarea primului senzor de obstacole
  20.  
  21. //////////////relee/////////////////////////
  22. #define relay1 30
  23. #define relay2 28
  24. #define relay3 26
  25. #define relay4 24
  26.  
  27. ////// definim butoanele + switch
  28. #define swin 23  //switch
  29. #define start_button 20
  30. #define stop_button 21
  31.  
  32.  
  33. int so1 = 0;
  34.  
  35. int sw_val = 0;
  36.  
  37. const byte ROWS = 4; // Four rows
  38. const byte COLS = 4; // Three columns
  39.  
  40.  
  41. char keys[ROWS][COLS] = { // Define the Keymap
  42.   {'1', '2', '3', 'A'},
  43.   {'4', '5', '6', 'B'},
  44.   {'7', '8', '9', 'C'},
  45.   {'*', '0', '#', 'D'}
  46. };
  47. char opposite_keys[ROWS][COLS] = { // Define the Keymap
  48.   {NULL, NULL, NULL, NULL},
  49.   {NULL, NULL, NULL, NULL},
  50.   {'^', '%', '$', NULL},
  51.   {NULL, '@', NULL, NULL}
  52. };
  53.  
  54. byte rowPins[ROWS] = { 2, 3, 4, 5 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  55.  
  56. byte colPins[COLS] = { 6, 7, 8 , 9}; // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  57.  
  58. int opposite_key = NULL;
  59. bool flag_alarm = true;
  60. int current_state = -1;
  61.  
  62. byte chA = 18;
  63. byte chB = 19;
  64. int encoder_value = 0;
  65.  
  66. void setup()
  67. {
  68.   pinMode(stepPin, OUTPUT);
  69.   pinMode(dirPin, OUTPUT);
  70.   pinMode(ls3in, INPUT);
  71.   pinMode(ls4in, INPUT);
  72.  
  73.   pinMode(stepPin2, OUTPUT);
  74.   pinMode(dirPin2, OUTPUT);
  75.   pinMode(ls2in, INPUT);
  76.  
  77.   pinMode(stepPin3, OUTPUT);
  78.   pinMode(dirPin3, OUTPUT);
  79.   pinMode(ls1in, INPUT);
  80.  
  81.   pinMode(so1in, INPUT);
  82.   pinMode(ls0in, INPUT);
  83.  
  84.   pinMode(relay1, OUTPUT);
  85.   pinMode(relay2, OUTPUT);
  86.   pinMode(relay3, OUTPUT);
  87.   pinMode(relay4, OUTPUT);
  88.  
  89.   // pinmode pentru cele doua butoane si switch
  90.   pinMode(swin, INPUT_PULLUP);
  91.   pinMode(start_button, INPUT_PULLUP);
  92.   pinMode(stop_button, INPUT_PULLUP);
  93.  
  94.  
  95.  
  96.   // declararea pinilor de input respectiv output pentru tastatura 4x4
  97.   pinMode(6, INPUT);
  98.   pinMode(7, INPUT);
  99.   pinMode(8, INPUT);
  100.   pinMode(9, INPUT);
  101.   pinMode(2, OUTPUT);
  102.   pinMode(3, OUTPUT);
  103.   pinMode(4, OUTPUT);
  104.   pinMode(5, OUTPUT);
  105.   ///////////////////////////////
  106.   // setarea starii high pentru pinii asociati coloanelor
  107.  
  108.   digitalWrite(6, HIGH);
  109.   digitalWrite(7, HIGH);
  110.   digitalWrite(8, HIGH);
  111.   digitalWrite(9, HIGH);
  112.  
  113.   digitalWrite(relay1, HIGH);
  114.   digitalWrite(relay2, HIGH);
  115.   digitalWrite(relay3, HIGH);
  116.   digitalWrite(relay4, HIGH);
  117.  
  118.   ///// releul e COM_NC neenergizat, pe semnal LOW se conecteaza COM_NO iar pe semnal HIGH se conecteaza COM_NC.
  119.   Serial.begin(9600);
  120.  
  121.   attachInterrupt(digitalPinToInterrupt(stop_button), alarm, FALLING);
  122.   attachInterrupt(digitalPinToInterrupt(start_button), reset_alarm, FALLING);
  123.   // attachInterrupt(digitalPinToInterrupt(stop_button), cw, RISING);
  124.   // attachInterrupt(digitalPinToInterrupt(start_button), ccw, RISING);
  125. }
  126.  
  127. void loop() {
  128.   sw_val = digitalRead(swin);
  129.   if(start_button == 0){
  130.    Serial.println("buton apasat/perturbatie");
  131.   }
  132.   if (sw_val == 0 && flag_alarm == false) {     // sw_val == 0 coincide cu modul AUTOMAT
  133.     automat();
  134.   }
  135.   else if (sw_val == 1) { //sw_val == 1 conincide cu modul MANUAL
  136.     current_state = -1;
  137.     manual();
  138.  
  139.   }
  140. }
  141.  
  142.  
  143. char read_keyboard()
  144. {
  145.   for ( uint8_t i = 2; i < 6; ++i) {
  146.     digitalWrite(i, LOW);
  147.     for ( uint8_t j = 6; j < 10; ++j) {
  148.       int key = digitalRead(j);
  149.       if (!key)
  150.       {
  151.         digitalWrite(i, HIGH);
  152.         opposite_key = (opposite_keys[i - 2][j - 6]);
  153.         return (keys[i - 2][j - 6]);
  154.       }
  155.     }
  156.     digitalWrite(i, HIGH);
  157.   }
  158.   return opposite_key;
  159. }
  160.  
  161.  
  162. void manual() {
  163.   char a = read_keyboard();
  164.   //Serial.println(a);
  165.   switch (a)
  166.   {
  167.  
  168.     case '0': //pompa pornita
  169.       {
  170.         if (digitalRead(so1in) == 0) {
  171.           pump_on();
  172.         }
  173.         break;
  174.       }
  175.     case '@': //pompa oprita
  176.       {
  177.         pump_off();
  178.         reset_opposite_key();     //opposite_character = NULL ;
  179.         break;
  180.       }
  181.     case '1': //stepper miscare sus
  182.       {
  183.  
  184.         if (digitalRead(ls3in) == 0) {
  185.           break;
  186.         }
  187.         else {
  188.           move_motor(stepPin, dirPin, HIGH, 130);
  189.           break;
  190.         }
  191.       }
  192.     case '4': //stepper miscare jos
  193.       {
  194.         if (digitalRead(ls4in) == 0) {
  195.           break;
  196.         }
  197.         else {
  198.           move_motor(stepPin, dirPin, LOW, 130);
  199.           break;
  200.         }
  201.       }
  202.     case '3': //stepper lopata miscare stanga
  203.       {
  204.  
  205.         if (digitalRead(ls2in) == 0) {
  206.           break;
  207.         }
  208.         else {
  209.           move_motor(stepPin2, dirPin2, HIGH, 1000);
  210.           break;
  211.         }
  212.       }
  213.     case '2': //stepper lopata miscare dreapta
  214.       {
  215.         move_motor(stepPin2, dirPin2, LOW, 1000);
  216.         break;
  217.  
  218.       }
  219.     case '6': //stepper masa miscare stanga
  220.       {
  221.  
  222.         if (digitalRead(ls1in) == 0) {
  223.           break;
  224.         }
  225.         else {
  226.           move_motor(stepPin3, dirPin3, HIGH, 100);
  227.           break;
  228.         }
  229.       }
  230.     case '5': //stepper masa miscare dreapta
  231.       {
  232.         move_motor(stepPin3, dirPin3, LOW, 100);
  233.         break;
  234.       }
  235.     case '9': // miscare banda
  236.       {
  237.         if (digitalRead(ls0in) == 0) { // trebuie modificat
  238.           conveyor_off();
  239.           break;
  240.         }
  241.         else
  242.         {
  243.           conveyor_on();
  244.           break;
  245.         }
  246.       }
  247.  
  248.     case '$' : // opposite 9
  249.       {
  250.         conveyor_off();
  251.         reset_opposite_key();   //opposite_character = NULL ;
  252.         break;
  253.       }
  254.     case '8': //invartire sens orar(privit de sus)
  255.       {
  256.         motor_cw();
  257.         break;
  258.       }
  259.     case '%':
  260.       {
  261.         motor_stop();
  262.         reset_opposite_key();    //opposite_character = NULL ;
  263.         break;
  264.       }
  265.     case '7': //invartire sens trigonometric(privit de sus)
  266.       {
  267.         motor_ccw();
  268.         break;
  269.       }
  270.     case '^':
  271.       {
  272.         motor_stop();
  273.         reset_opposite_key();   //opposite_character = NULL ;
  274.         break;
  275.       }
  276.  
  277.   }
  278. }
  279.  
  280. /////////////////////////////// automat /////////////////////////
  281. void automat() {
  282.  
  283.   switch (current_state)
  284.   {
  285.     case 0 :
  286.       {
  287.         initialize();
  288.         //Serial.println("Am intrat pe case 0");
  289.         break;
  290.       }
  291.  
  292.     case 1 :
  293.       {
  294. //        conveyor_on();
  295. //        current_state = 2;
  296. //        break;
  297.       }
  298.     case 2:
  299.       {
  300. //        if (digitalRead(so1in) == 0) {
  301. //          conveyor_off();
  302. //          Serial.println("all good");
  303. //        }
  304.       }
  305.       case 3:
  306.       {
  307.        
  308.       }
  309.  
  310.   }
  311. }
  312. ////////////////////////////// states///////////////////////////
  313.  
  314. void initialize()
  315. {
  316.   pump_off();
  317.   conveyor_off();
  318.   motor_stop();
  319.  
  320.   if (digitalRead(ls1in) == 1) {
  321.     move_motor(stepPin3, dirPin3, HIGH, 100);
  322.   }
  323.   else
  324.   {
  325.     if (digitalRead(ls2in) == 1) {
  326.       move_motor(stepPin2, dirPin2, HIGH, 1000);
  327.     }
  328.     else
  329.     {
  330.       if (digitalRead(ls3in) == 1) {
  331.         move_motor(stepPin, dirPin, HIGH, 130);
  332.       }
  333.     }
  334.   }
  335.   if (digitalRead(ls1in) == 0 && digitalRead(ls2in) == 0 && digitalRead(ls3in) == 0)
  336.   {
  337.     current_state = 1;
  338.   }
  339.  
  340. }
  341.  
  342.  
  343.  
  344. //////////////////////////////// helper functions/////////////////
  345.  
  346. void conveyor_on() //miscare banda pornita
  347. {
  348.   digitalWrite(relay1, LOW);
  349. }
  350.  
  351. void conveyor_off() //miscare banda oprita
  352. {
  353.   digitalWrite(relay1, HIGH);
  354. }
  355.  
  356. void motor_cw() //motor infiletare sens orar
  357. {
  358.   digitalWrite(relay4, LOW);
  359. }
  360.  
  361. void motor_ccw() // motor infiletare sens trigonometric
  362. {
  363.   digitalWrite(relay3, LOW);
  364. }
  365.  
  366. void motor_stop() //motor infiletare este oprit
  367. {
  368.   digitalWrite(relay3, HIGH);
  369.   digitalWrite(relay4, HIGH);
  370. }
  371.  
  372. void pump_on()
  373. {
  374.   digitalWrite(relay2, LOW);
  375. }
  376.  
  377. void pump_off()
  378. {
  379.   digitalWrite(relay2, HIGH);
  380. }
  381.  
  382. void reset_opposite_key()
  383. {
  384.   opposite_key = NULL;
  385. }
  386.  
  387.  
  388. void cw()
  389. {
  390.   if (digitalRead(chA) > digitalRead(chB))
  391.   {
  392.     encoder_value++ ;
  393.   }
  394. }
  395.  
  396. void ccw()
  397. {
  398.   if (digitalRead(chA) > digitalRead(chB))
  399.   {
  400.     encoder_value-- ;
  401.  
  402.   }
  403. }
  404.  
  405. // functie pentru steppere
  406. void move_motor(int motor_pin, int dir_pin, int direction, int delay) {
  407.   digitalWrite(dir_pin, direction);
  408.   digitalWrite(motor_pin, HIGH);
  409.   digitalWrite(motor_pin, LOW);
  410.   delayMicroseconds(delay);
  411.   //ST1-SW123-011, comutator 111100;  delay micro 130
  412.   //ST2-SW123-010, comutator 111011;  delay micro 1000
  413.   //ST2-SW123--010, comutator 001100; delay micro 100
  414. }
  415.  
  416. void alarm() {
  417.   motor_stop();
  418.   pump_off();
  419.   conveyor_off();
  420.   flag_alarm = true;
  421.  
  422. }
  423.  
  424. void reset_alarm() {
  425.   flag_alarm = false;
  426.   current_state = 0;
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement