Advertisement
Guest User

1577 Wheelchair code

a guest
Oct 9th, 2017
1,698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None
  1. int xpin=0;
  2. int ypin=1;
  3. //int cklickypin=8;
  4. int motorRpin=9;
  5. int motorLpin=3;//*dont change* 3 and 9 are PWM on different frequency and work with the same level of hirtz
  6. float motorRout=0;
  7. float motorLout=0;
  8. float x;
  9. float y;
  10. float Nx;
  11. float Ny;
  12. //int cklickystate=0;
  13. float RmotorRout=63;//one motor was connected the other way around
  14. float RmotorLout=254;
  15. void setup()
  16. {
  17. Serial.begin(9600);
  18. //pinMode(cklickypin,INPUT);
  19. pinMode(xpin,INPUT);
  20. pinMode(ypin,INPUT);
  21. pinMode(motorRpin,OUTPUT);
  22. pinMode(motorLpin,OUTPUT);
  23. }
  24. void loop()
  25. {
  26. //cklickystate=digitalRead(cklickypin);
  27. // Serial.println(cklickystate);
  28.  
  29. x=analogRead(xpin);
  30. y=analogRead(ypin);
  31. Serial.print("x:");
  32. Serial.println(x);
  33. Serial.print("y:");
  34. Serial.println(y);
  35. if (x<530&&x>490)//dead zone in case there is a small unintentional movement
  36. {
  37. x=512;
  38. }
  39. if(y<530&&y>490)
  40. {
  41. y=512;
  42. }
  43. Serial.print("x:");
  44. Serial.println(x);
  45. Serial.print("y:");
  46. Serial.println(y);
  47. //agents the range of values to make arcade easier
  48. Nx=x-512;
  49. Ny=y-512;
  50. //y is left right 0 is left xis forward back 0 is back
  51. //arcade drive
  52. motorRout=Nx+Ny;
  53. Serial.print("NX");
  54. Serial.println(Nx);
  55. Serial.print("Ny");
  56. Serial.println(Ny);
  57. motorLout=Ny-Nx;
  58. /*remaps the vlause to match the range of the Sparks */
  59. RmotorRout=map(motorRout,-512,512,254,63);
  60. RmotorLout=map(motorLout,-512,512,63,254);
  61. Serial.print("motorRout");
  62. Serial.println(motorRout);
  63. Serial.print("motorLout");
  64. Serial.println(motorLout);
  65. RmotorRout=RmotorRout+30;//the value that makes the sparks stop moving is 30 away from the middle of the range witch is when the joystick is in the middle.
  66. RmotorLout=RmotorLout+30;
  67. /*if the Spark gets a value out side its possible range it
  68. will not work at all so this part rounds the
  69. values in to the range.*/
  70. if(RmotorLout>254)
  71. {
  72. RmotorLout=254;
  73. }
  74. if(RmotorRout>254)
  75. {
  76. RmotorRout=254;
  77. }
  78. if(RmotorLout<63)
  79. {
  80. RmotorLout=63;
  81. }
  82. if(RmotorRout<63)
  83. {
  84. RmotorRout=63;
  85. }
  86. /*outputs the value to the motors and prints them to the
  87. screen*/
  88. analogWrite(motorRpin,RmotorRout);
  89. analogWrite(motorLpin,RmotorLout);
  90. Serial.print("motor L out");
  91. Serial.println(RmotorLout);
  92. Serial.print("motor R out");
  93. Serial.println(RmotorRout);
  94. Serial.println("");
  95. delay(200);
  96. }
Advertisement
RAW Paste Data Copied
Advertisement