Advertisement
Guest User

Untitled

a guest
Apr 11th, 2020
1,747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include <elapsedMillis.h>
  2. elapsedMillis timeElapsed;
  3.  
  4. int RPWM = 10;
  5. int LPWM = 11;
  6. int downPin = 12;
  7. int upPin = 13;
  8. int autoPin = 8;
  9. int manPin = 9;
  10.  
  11. int sensorPin = A0;
  12. int potPin = A2;
  13. int potVal;
  14. int sensorVal;
  15. int Buffer = 10;
  16.  
  17. int maxAnalogReading;
  18. int minAnalogReading;
  19.  
  20. void setup() {
  21. pinMode(RPWM, OUTPUT);
  22. pinMode(LPWM, OUTPUT);
  23. pinMode(downPin, INPUT_PULLUP);
  24. pinMode(upPin, INPUT_PULLUP);
  25. pinMode(autoPin, INPUT_PULLUP);
  26. pinMode(manPin, INPUT_PULLUP);
  27. pinMode(sensorPin, INPUT);
  28. pinMode(potPin, INPUT);
  29. Serial.begin(9600);
  30. maxAnalogReading = moveToLimit(1); // otwieracie monitor portu szeregowego i odczytujecie wartoscii poprzez sterowanie potenjometrem, w jakim zakresie ma działać was siłownik, usuwacie movetolimit(1) i zmieniacie np na 937
  31. minAnalogReading = moveToLimit(-1) ; // otwieracie monitor portu szeregowego i odczytujecie wartoscii poprzez sterowanie potenjometrem, w jakim zakresie ma działać was siłownik, usuwacie movetolimit(-1) i zmieniacie np na 242
  32. }
  33.  
  34. void loop() {
  35. if(digitalRead(manPin)==LOW)
  36. {
  37. if(digitalRead(upPin)==LOW){ //wysuwaj jesli guzik jest wcisniety
  38. analogWrite(RPWM, 0);
  39. analogWrite(LPWM, 255);
  40. }
  41.  
  42. else if(digitalRead(downPin)==LOW){ //wsuwaj jesli guzik jest wcisniety
  43. analogWrite(RPWM, 255);
  44. analogWrite(LPWM, 0);
  45. }
  46.  
  47. else{ //jesli guzik nie jest wcisniety stoj
  48. analogWrite(RPWM, 0);
  49. analogWrite(LPWM, 0);
  50. }
  51. }
  52.  
  53. else if(digitalRead(autoPin)==LOW)
  54. {
  55. potVal = map(analogRead(potPin), 0, 1023, minAnalogReading, maxAnalogReading);
  56. sensorVal = analogRead(sensorPin);
  57. if(potVal > (sensorVal+Buffer)){ //Dodatkowy bufor, zapobiegajacy wibracja silnika z powodu zaklocen danych wejscia
  58. driveActuator(1, 100);
  59. }
  60. else if(potVal < (sensorVal-Buffer)){
  61. driveActuator(-1, 100);
  62. }
  63. else{
  64. driveActuator(0, 100);
  65. }
  66. Serial.print("Potentiometer Reading: "); //po ustaleniu zakresy pracy na potencjometrze można usunąć ten kod od tej linijki do delay(10)
  67. Serial.print(potVal);
  68. Serial.print("\tActuator reading: ");
  69. Serial.println(sensorVal);
  70. delay(10);
  71. }
  72.  
  73. else{ //jesli zaden przycisk trybu nie jest przelaczony, stoj
  74. analogWrite(RPWM, 0);
  75. analogWrite(LPWM, 0);
  76. }
  77. }
  78.  
  79. int moveToLimit(int Direction){ //po ustaleniu zakresy pracy na potencjometrze można usunąć ten kod od tej linijki do return currreading i znak } który znajduje się w linijce pod spodem
  80. int prevReading=0;
  81. int currReading=0;
  82. do{
  83. prevReading = currReading;
  84. driveActuator(Direction, 255);
  85. timeElapsed = 0;
  86. while(timeElapsed < 200){ delay(1);}
  87. currReading = analogRead(sensorPin);
  88. }while(prevReading != currReading);
  89. return currReading;
  90. }
  91.  
  92. void driveActuator(int Direction){
  93. switch(Direction){
  94. case 1: //wysuwanie
  95. analogWrite(RPWM, 100);
  96. analogWrite(LPWM, 0);
  97. break;
  98.  
  99. case 0: //stop
  100. analogWrite(RPWM, 0);
  101. analogWrite(LPWM, 0);
  102. break;
  103.  
  104. case -1: //wsuwanie
  105. analogWrite(RPWM, 0);
  106. analogWrite(LPWM, 100);
  107. break;
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement