This is my code so far:
Code:
/* Using 1 potentiometer to control h-bridge
* forward from zero to highest rpm
* stop
* reverse from zero to highest rpm
* by Leo Groeneveld
*/
int potpin2 = 2; // analog pin used to connect the potentiometer2
int val2; // variable to read the value2 from the analog pin2
int pwmval2; // variable to set the value of the PWM signal
int pwmval3; // variable to set the value of the PWM signal
void setup() {
pinMode(2, OUTPUT); // declare pin 2 to be an output:
pinMode(3, OUTPUT); // declare pin 3 to be an output:
analogWrite(2, 0); // start with no output on PWMpin2
analogWrite(3, 0); // start with no output on PWMpin3
Serial.begin(9600);
}
void loop() {
val2 = analogRead(potpin2); // reads the value of potentiometer2 (value between 0 and 1023)
if (val2 > 461 and val2 < 561) { // dead-band
analogWrite(2, 0); // when in dead-band no signal to digital pin2
analogWrite(3, 0); // when in dead-band no signal to digital pin3
}
if (val2 >= 0 and val2 <= 461) { // motor rotating one direction
pwmval2 = map(val2, 0, 461, 255, 0); // motor rotating in one direction slow to highest rpm
analogWrite(2, pwmval2); // writes PWM value to digital pin2
}
if (val2 >= 561 and val2 <= 1023) { // motor rotating in other direction
pwmval3 = map(val2, 561, 1023, 0, 255); // motor rotating in other direction slow to highest rpm
analogWrite(3, pwmval3); // writes PWM value to digital pin3
}
Serial.println(pwmval2, DEC);
}
* forward from zero to highest rpm
* stop
* reverse from zero to highest rpm
* by Leo Groeneveld
*/
int potpin2 = 2; // analog pin used to connect the potentiometer2
int val2; // variable to read the value2 from the analog pin2
int pwmval2; // variable to set the value of the PWM signal
int pwmval3; // variable to set the value of the PWM signal
void setup() {
pinMode(2, OUTPUT); // declare pin 2 to be an output:
pinMode(3, OUTPUT); // declare pin 3 to be an output:
analogWrite(2, 0); // start with no output on PWMpin2
analogWrite(3, 0); // start with no output on PWMpin3
Serial.begin(9600);
}
void loop() {
val2 = analogRead(potpin2); // reads the value of potentiometer2 (value between 0 and 1023)
if (val2 > 461 and val2 < 561) { // dead-band
analogWrite(2, 0); // when in dead-band no signal to digital pin2
analogWrite(3, 0); // when in dead-band no signal to digital pin3
}
if (val2 >= 0 and val2 <= 461) { // motor rotating one direction
pwmval2 = map(val2, 0, 461, 255, 0); // motor rotating in one direction slow to highest rpm
analogWrite(2, pwmval2); // writes PWM value to digital pin2
}
if (val2 >= 561 and val2 <= 1023) { // motor rotating in other direction
pwmval3 = map(val2, 561, 1023, 0, 255); // motor rotating in other direction slow to highest rpm
analogWrite(3, pwmval3); // writes PWM value to digital pin3
}
Serial.println(pwmval2, DEC);
}
I can control speed too.
I have taken an old printer apart and would like to be able to steer the printing head left and right to a certain position using a linear encoder but that's also an A B type. The current motor is small and easy to experiment with.
In the future I would like to build a robot arm with a friend of mine and we would like to use a DC motor to rotate the whole arm and model servos for the other movements.
)