Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*CODE YOU CAN USE
  2.  * just copy and paste in any order.
  3.  * Be sure to change the numbers
  4. wait(2); //number time in seconds 0.1 to 5. You can use decimal numbers
  5. forward(0.5, 50); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  6. backward(0.5, 50); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  7. left(0.5, 50); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  8. right(0.5, 50); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  9. spinRight(0.5, 30); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  10. spinLeft(0.5, 100); //first number is time in seconds 0.1 to 2, second number is speed from 0 to 100
  11. accelForward(50); //number is max speed from 0 to 100
  12. deccelForward(50); //number is max speed from 0 to 100):
  13. accelBackward(50); //number is max speed from 0 to 100);
  14. deccelBackward(50); //number is max speed from 0 to 100);
  15. */
  16.  
  17. void doOnce(){
  18.   //code you put in here will happen only once
  19.   wait(2);
  20.   paintWheels();
  21.   wait(5);
  22.   //put your one time code here
  23.  
  24.  
  25.  
  26.   //yep up there
  27. }
  28. void doAgainandAgain(){
  29.   //code you put in here will happen again and again
  30.  
  31.  
  32.   //yep up there
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. //oops dont touch anything down here
  44.  
  45. //define pin numbers
  46. #define LeftMotorA D4
  47. #define LeftMotorB D5
  48. #define LeftMotorEnable D3
  49. #define RightMotorA D6
  50. #define RightMotorB D7
  51. #define RightMotorEnable D8
  52.  
  53. const int SpeedMin = 180;
  54. const int SpeedMax = 1023;
  55. const int DurationMin = 100;
  56. const int DurationMax = 2000;
  57. const int WaitMin = 100;
  58. const int WaitMax = 5000;
  59.  
  60. void feedWatchdog(){
  61.   ESP.wdtFeed();
  62. }
  63. void accelForward(int topSpeed){
  64.   // accelerate from zero to maximum speed
  65.   for (int i = 0; i <= topSpeed; i=(i+(topSpeed/10))){
  66.     forward(0.1,i);
  67.   }
  68. }
  69. void deccelForward(int topSpeed){  
  70.   // decelerate from maximum speed to zero
  71.   for (int i = topSpeed; i >= 0; i=(i-(topSpeed/10))){
  72.     forward(0.1,i);  
  73.   }
  74. }
  75.  
  76. void accelBackward(int topSpeed){
  77.   // accelerate from zero to maximum speed
  78.   for (int i = 0; i <= topSpeed; i=(i+(topSpeed/10))){
  79.     backward(0.1,i);
  80.   }
  81. }
  82. void deccelBackward(int topSpeed){  
  83.   // decelerate from maximum speed to zero
  84.   for (int i = topSpeed; i >= 0; i=(i-(topSpeed/10))){
  85.     backward(0.1,i);  
  86.   }
  87. }
  88.  
  89. void paintWheels(){
  90.  
  91.   accelForward(100);
  92.   forward(1,100);
  93.   deccelForward(100);
  94.   accelBackward(100);
  95.   backward(1,100);
  96.   deccelBackward(100);
  97.   /*//old option
  98.   forward(3,50);
  99.   delay(300);
  100.   backward(3,50);
  101.   */
  102. }
  103.  
  104. void enableSpeed(int val){
  105.   val = mapSpeed(val);
  106.   analogWrite(LeftMotorEnable, val);
  107.   analogWrite(RightMotorEnable, val);
  108. }
  109.  
  110. void disableSpeed(){
  111.   analogWrite(LeftMotorEnable, LOW);
  112.   analogWrite(RightMotorEnable, LOW);
  113. }
  114.  
  115. void pinsLow(){
  116.   digitalWrite(LeftMotorA, LOW);
  117.   digitalWrite(LeftMotorB, LOW);
  118.   digitalWrite(RightMotorA, LOW);
  119.   digitalWrite(RightMotorB, LOW);
  120. }
  121.  
  122. void stopMotors(){
  123.   disableSpeed();
  124.   pinsLow();
  125. }
  126.  
  127. void leftBackward(){
  128.   digitalWrite(LeftMotorA, HIGH);
  129.   digitalWrite(LeftMotorB, LOW);
  130. }
  131. void leftForward(){
  132.   digitalWrite(LeftMotorA, LOW);
  133.   digitalWrite(LeftMotorB, HIGH);
  134. }
  135. void rightBackward(){
  136.   digitalWrite(RightMotorA, LOW);
  137.   digitalWrite(RightMotorB, HIGH);
  138. }
  139. void rightForward(){
  140.   digitalWrite(RightMotorA, HIGH);
  141.   digitalWrite(RightMotorB, LOW);
  142. }
  143. void forward(float duration, int fast){
  144.   stopMotors();
  145.   enableSpeed(fast);
  146.   leftForward();
  147.   rightForward();
  148.   motorDuration(duration);
  149.   stopMotors();
  150. }
  151. void backward(float duration, int fast){
  152.   stopMotors();
  153.   enableSpeed(fast);
  154.   leftBackward();
  155.   rightBackward();
  156.   motorDuration(duration);
  157.   stopMotors();
  158. }
  159. void left(float duration, int fast){
  160.   stopMotors();
  161.   enableSpeed(fast);
  162.   rightForward();
  163.   motorDuration(duration);
  164.   stopMotors();
  165. }
  166. void right(float duration, int fast){
  167.   stopMotors();
  168.   enableSpeed(fast);
  169.   leftForward();
  170.   motorDuration(duration);
  171.   stopMotors();
  172. }
  173. void spinLeft(float duration, int fast){
  174.   stopMotors();
  175.   enableSpeed(fast);
  176.   leftBackward();
  177.   rightForward();
  178.   motorDuration(duration);
  179.   stopMotors();
  180. }
  181. void spinRight(float duration, int fast){
  182.   stopMotors();
  183.   enableSpeed(fast);
  184.   leftForward();
  185.   rightBackward();
  186.   motorDuration(duration);
  187.   stopMotors();
  188. }
  189.  
  190. int mapSpeed(int val){
  191.   val = constrain(val, 1, 100);
  192.   val = map(val, 1, 100, SpeedMin, SpeedMax);
  193.   return val;
  194. }
  195.  
  196. int mapDuration(int val){
  197.   val = constrain(val, 0.1, 2.0);
  198.   val = map(val, 0.1, 2.0, DurationMin, DurationMax);
  199.   val = (int) val;
  200.   return val;
  201. }
  202.  
  203. void motorDuration(float val){
  204.   val = mapDuration(val);
  205.   val = (int) val;
  206.   feedWatchdog();
  207.   delay(val);
  208. }
  209.  
  210. void wait(float val){
  211.   val = constrain(val, 0.1, 5.0);
  212.   val = map(val, 0.1, 5.0, WaitMin, WaitMax);
  213.   val = (int) val;
  214.   feedWatchdog();
  215.   digitalWrite(LED_BUILTIN, LOW);
  216.   delay(val);
  217.   digitalWrite(LED_BUILTIN, HIGH);
  218. }
  219.  
  220. /* how to map and constrain
  221.  
  222. map(value, fromLow, fromHigh, toLow, toHigh)
  223.  
  224. sensVal = constrain(sensVal, LowVal, HighVal);
  225. // limits range of sensor values to between 10 and 150
  226.  
  227. */
  228.  
  229.  
  230. void setup() {
  231.   // put your setup code here, to run once:
  232.  
  233.   //set pinouts
  234.   //   LED pin
  235.   pinMode(LED_BUILTIN, OUTPUT);
  236.   //   LeftMotor Pins ,OUTPUT);
  237.   pinMode(LeftMotorA,OUTPUT);
  238.   pinMode(LeftMotorB,OUTPUT);
  239.   pinMode(LeftMotorEnable,OUTPUT);
  240.   //   RightMotor Pins
  241.   pinMode(RightMotorA,OUTPUT);
  242.   pinMode(RightMotorB,OUTPUT);
  243.   pinMode(RightMotorEnable,OUTPUT);
  244.  
  245.   ESP.wdtDisable();
  246.   feedWatchdog();
  247.  
  248.   doOnce();
  249. }
  250.  
  251. void loop() {
  252.   // put your main code here, to run repeatedly:
  253.   doAgainandAgain();
  254.  
  255.   //tiny loop delay
  256.   delay(50);
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement