Advertisement
WayGroovy

Arduino Car Line Follower Code v1.0

Apr 18th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. /*
  2.   5/22/2012
  3.   Timothy Holmberg
  4.   SparkFun Electronics
  5.  
  6.   This code includes the addition of fade in and out PWM. Also a stop feature. And the addition of individual functions for motor control
  7.  
  8.   This was a revision of the example sketch that originated from Pete Dokter's code for Arduino that shows very basically how to control an Ardumoto
  9.   motor driver shield with a 5V Arduino controller board. http://www.sparkfun.com/datasheets/DevTools/Arduino/Ardumoto_test_3.pde
  10.  
  11.   This also includes parts of the Fading Example,  Created 1 Nov 2008 By David A. Mellis, modified 30 Aug 2011 By Tom Igoe http://arduino.cc/en/Tutorial/Fading
  12.  
  13. */
  14.  
  15. int pwm_a = 3;   //PWM control for motor outputs 1 and 2 is on digital pin 3
  16. int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11
  17. int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
  18. int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13
  19. int val = 0;     //value for fade
  20.  
  21. void setup()
  22. {
  23.   pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  24.   pinMode(pwm_b, OUTPUT);
  25.   pinMode(dir_a, OUTPUT);
  26.   pinMode(dir_b, OUTPUT);
  27.  
  28.   //set both motors to stop
  29.   analogWrite(pwm_a, 0);
  30.   analogWrite(pwm_b, 0);
  31.  
  32.   Serial.begin(9600);
  33. }
  34.  
  35. void loop()
  36. {
  37.  
  38.  
  39.   // read the input on analog pin 0:
  40.   int sensorValue0 = analogRead(A0);
  41.   // read the input on analog pin 1:
  42.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  43.   float voltage0 = (sq(sensorValue0  / 1023.0)) * 125;
  44.   int speed0 = round(voltage0);
  45.   int sensorValue1 = analogRead(A1);
  46.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  47.   float voltage1 = (sq(sensorValue1 / 1023.0)) * 125;
  48.   int speed1 = round(voltage1);
  49.   /*
  50.     Serial.print("L:");
  51.     Serial.print(speed0);
  52.     Serial.print(" R:");
  53.     Serial.println(speed1);
  54.   */
  55.  
  56.   int leftSpeed = speed0 - speed1 + 125;
  57.   int rightSpeed = speed1 - speed0 + 125;
  58.  
  59.   analogWrite(pwm_a, leftSpeed);
  60.   analogWrite(pwm_b, rightSpeed);
  61.  
  62.   Serial.print("L:");
  63.   Serial.print(leftSpeed);
  64.   Serial.print(" R:");
  65.   Serial.println(rightSpeed);
  66.  
  67.   delay(10);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement