Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 5/22/2012
- Timothy Holmberg
- SparkFun Electronics
- This code includes the addition of fade in and out PWM. Also a stop feature. And the addition of individual functions for motor control
- 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
- motor driver shield with a 5V Arduino controller board. http://www.sparkfun.com/datasheets/DevTools/Arduino/Ardumoto_test_3.pde
- 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
- */
- int pwm_a = 3; //PWM control for motor outputs 1 and 2 is on digital pin 3
- int pwm_b = 11; //PWM control for motor outputs 3 and 4 is on digital pin 11
- int dir_a = 12; //direction control for motor outputs 1 and 2 is on digital pin 12
- int dir_b = 13; //direction control for motor outputs 3 and 4 is on digital pin 13
- int val = 0; //value for fade
- void setup()
- {
- pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
- pinMode(pwm_b, OUTPUT);
- pinMode(dir_a, OUTPUT);
- pinMode(dir_b, OUTPUT);
- //set both motors to stop
- analogWrite(pwm_a, 0);
- analogWrite(pwm_b, 0);
- Serial.begin(9600);
- }
- void loop()
- {
- // read the input on analog pin 0:
- int sensorValue0 = analogRead(A0);
- // read the input on analog pin 1:
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- float voltage0 = (sq(sensorValue0 / 1023.0)) * 125;
- int speed0 = round(voltage0);
- int sensorValue1 = analogRead(A1);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- float voltage1 = (sq(sensorValue1 / 1023.0)) * 125;
- int speed1 = round(voltage1);
- /*
- Serial.print("L:");
- Serial.print(speed0);
- Serial.print(" R:");
- Serial.println(speed1);
- */
- int leftSpeed = speed0 - speed1 + 125;
- int rightSpeed = speed1 - speed0 + 125;
- analogWrite(pwm_a, leftSpeed);
- analogWrite(pwm_b, rightSpeed);
- Serial.print("L:");
- Serial.print(leftSpeed);
- Serial.print(" R:");
- Serial.println(rightSpeed);
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement