int motor1Pin = 4; // H-bridge leg 1
int motor2Pin = 5; // H-bridge leg 2
int enablePin = 9; // H-bridge enable pin
int sp;
int potPin = 2; //Analogue input from Potentiometer
int val = 0; //Value being read from Potentiometer
void setup() {
Serial.begin(9600); //Beggin serial communication
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
val = analogRead(potPin); //Read pot value
Serial.println(val); //print value to debugg
// if the switch is high, motor will turn on one direction:
if (val< 500) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
sp =map(val, 0, 500, 0,150);
analogWrite(enablePin, sp); //Write to speed pin to change speed
}
// if the switch is low, motor will turn in the other direction:
else if(val> 525) {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
sp =map(val, 525, 1023, 0,150);
analogWrite(enablePin, sp); //Write to speed pin to change speed
}
else{analogWrite(enablePin, 0); //Write to speed pin to change speed
}
delay(10); //Delay before next loop
}