Advertisement
Guest User

Meh

a guest
Jan 19th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. const int enable2 = 9;
  2. const int Xpin = A0; //analog pin connected to x output
  3. const int Ypin = A1; //analog pin connected to y output
  4.  
  5. /* L298N motor drive control pins */
  6. int IN1 = 7; // IN1
  7. int IN2 = 8; // IN2
  8.  
  9. /* I'm assuming this function is automatically at runtime like loop() is */
  10. void setup(){
  11.  
  12. /* motor control pins as output */
  13. pinMode(enable2,OUTPUT);
  14. pinMode(IN1,OUTPUT);
  15. pinMode(IN2,OUTPUT);
  16.  
  17.  
  18. Serial.begin(9600);
  19.  
  20. }
  21.  
  22. void loop(){
  23.  
  24.   /* Declaring variables to store pin reads */
  25.   int x_val, y_val;
  26.  
  27.   /* Variables to hold what I assume are the direction/power settings to the motor
  28.    * I would change these to better represent what they actually are.  Don't forget
  29.    * to also change their occurances further down though
  30.    */
  31.   int power, dir1, dir2;
  32.  
  33.  
  34.   /* perform reads */
  35.   x_val = analogRead(Xpin);
  36.   y_val = analogRead(Ypin);
  37.  
  38.   /* print values */
  39.   Serial.print("Xpin: ");
  40.   Serial.println(x_val);
  41.  
  42.   Serial.print("Ypin: ");
  43.   Serial.println(y_val);
  44.  
  45.   /* are you purposely going for delay between the read and the output to the motor?
  46.    * if not, that's what is happening here.  If you're just trying to delay the next iteration
  47.    * of the loop I would move this statement to the last statement of this function
  48.    */
  49.   delay(50);
  50.  
  51.   /* I'm assuming you want anything over 800 to be fast forward since the range
  52.    * that anologRead() returns will be between 0 and 1023 Xpin > 1023 will never be true
  53.    *
  54.    * Also I would double check these analog and digital values.  I don't know what the motor drive
  55.    * expects but I would think that logically both the "forward" options should share something in
  56.    * common on the digital pins whereas analog would carry power level, and the two backward ones
  57.    * likewise respectively.  I could be wrong, but I would check.
  58.    */
  59.   if(x_val > 800)
  60.   {
  61.     /* Fast Forward */
  62.     power = 100;
  63.     dir1 = HIGH;
  64.     dir2 = LOW;
  65.   }
  66.   else if(x_val > 605)
  67.   {
  68.     /* Slow Forward */
  69.     power = 150;
  70.     dir1 = LOW;
  71.     dir2 = HIGH;
  72.   }
  73.   else if(x_val > 270)
  74.   {
  75.     /* Slow Backward */
  76.     power = 170;
  77.     dir1 = LOW;
  78.     dir2 = HIGH;
  79.   }
  80.   else /* we don't need another if statement because 0 - 269 is the only option left */
  81.   {
  82.     /* Fast Backward */
  83.     power = 230;
  84.     dir1 = HIGH;
  85.     dir2 = LOW;
  86.   }
  87.  
  88.   analogWrite(enable2, power);
  89.   digitalWrite(IN1, dir1);
  90.   digitalWrite(IN2, dir2);
  91. /*
  92. if(605 < Xpin > 800 ) slowForward();
  93. if(800 < Xpin > 1023) fastforward();
  94. if(605 < Xpin > 270) slowbackward();
  95. if(270 < Xpin > 0) fastbackward();
  96. */
  97. }
  98.  
  99.  
  100. /*
  101. void slowForward(){
  102. analogWrite(enable2,100);
  103. digitalWrite(IN1,HIGH);
  104. digitalWrite(IN2,LOW);
  105.  
  106. }
  107.  
  108. void fastforward(){
  109. analogWrite(enable2,150);
  110. digitalWrite(IN1,LOW);
  111. digitalWrite(IN2,HIGH);
  112.  
  113. }
  114.  
  115. void slowbackward(){
  116. analogWrite(enable2,170);
  117. digitalWrite(IN1,LOW);
  118. digitalWrite(IN2,HIGH);
  119.  
  120. }
  121.  
  122. void fastbackward(){
  123. analogWrite(enable2,230);
  124. digitalWrite(IN1,HIGH);
  125. digitalWrite(IN2,LOW);
  126.  
  127. }
  128. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement