Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. int vrX = 0;
  2. int vrY = 1;
  3. int sw = 10;
  4. int in1 = 8;
  5. int in2 = 7;
  6. int enA = 9;
  7. int in3 = 13;
  8. int in4 = 12;
  9. int enB = 11;
  10.  
  11. int motorSpeedA = 0;
  12. int motorSpeedB = 0;
  13. void setup() {
  14. // put your setup code here, to run once:
  15. pinMode(in3, OUTPUT);
  16. pinMode(in4, OUTPUT);
  17. pinMode(enB, OUTPUT);
  18. pinMode(sw, INPUT);
  19. pinMode(in1, OUTPUT);
  20. pinMode(in2, OUTPUT);
  21. pinMode(enA, OUTPUT);
  22. }
  23.  
  24. void loop() {
  25. // put your main code here, to run repeatedly:
  26. int xAxis = analogRead(A0);
  27. int yAxis = analogRead(A1);
  28.  
  29. if (yAxis < 470){
  30. digitalWrite(in3, HIGH);
  31. digitalWrite(in4, LOW);
  32. digitalWrite(in1, HIGH);
  33. digitalWrite(in2, LOW);
  34.  
  35. motorSpeedA = map(yAxis, 470, 0, 0, 255);
  36. motorSpeedB = map(yAxis, 470, 0, 0, 255);
  37. }
  38. else if (yAxis > 550){
  39. digitalWrite(in3, LOW);
  40. digitalWrite(in4, HIGH);
  41. digitalWrite(in1, LOW);
  42. digitalWrite(in2, HIGH);
  43. motorSpeedA = map(yAxis, 550, 1023, 0, 255);
  44. motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  45. }
  46. else{
  47. motorSpeedA = 0;
  48. motorSpeedB = 0;
  49. }
  50.  
  51. if (xAxis < 470){
  52. int xMapped = map(xAxis, 470, 0, 0, 255);
  53. motorSpeedA = motorSpeedA - xMapped;
  54. motorSpeedB = motorSpeedB + xMapped;
  55. if(motorSpeedA < 0){
  56. motorSpeedA = 0;
  57. }
  58. if(motorSpeedB > 255){
  59. motorSpeedB = 255;
  60. }
  61. }
  62. else if(xAxis > 550){
  63. int xMapped = map(xAxis, 550, 1023, 0, 255);
  64. motorSpeedA = motorSpeedA + xMapped;
  65. motorSpeedB = motorSpeedB - xMapped;
  66. if(motorSpeedA > 255){
  67. motorSpeedA = 255;
  68. }
  69. if(motorSpeedB < 0){
  70. motorSpeedB = 0;
  71. }
  72. }
  73. analogWrite(enA, motorSpeedA);
  74. analogWrite(enB, motorSpeedB);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement