Skip to content

Instantly share code, notes, and snippets.

@junpeitsuji
Created October 28, 2014 16:16
Show Gist options
  • Save junpeitsuji/c9046b62c5e8721c7c0a to your computer and use it in GitHub Desktop.
Save junpeitsuji/c9046b62c5e8721c7c0a to your computer and use it in GitHub Desktop.
int analogPin = 0;
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
int v = 0;
void setup()
{
Serial.begin(9600);
pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
analogWrite(pwm_a, 100); //set both motors to run at (100/255 = 39)% duty cycle (slow)
analogWrite(pwm_b, 100);
forw(); //Set Motors to go forward Note : No pwm is defined with the for function, so that fade in and out works
}
void loop()
{
val = analogRead(analogPin);
v = map(val, 0, 1024, 0, 256);
Serial.println(v);
forward(v); //continue full speed forward
delay(100);
}
void forw() // no pwm defined
{
digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low
digitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high
}
void forward(int sp) //full speed forward
{
digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low
digitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high
analogWrite(pwm_a, sp); //set both motors to run at (100/255 = 39)% duty cycle
analogWrite(pwm_b, sp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment