Dear Robot guys,
Im working on the speed control for a mobile spinning machine that drives mobiles for my baby son.
The speed control only partly works, when I turn the button the speed only eventually increases or decreases.
Here is my programm so far:
#include <Stepper.h>
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, dirA, dirB);
int stepCount = 0; // number of steps the motor has taken
int pos = 0; // variable to store the servo position
int vspeed = 100;
int ispeed = 10;
const int pin_A = A8;
const int pin_B = A9;
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev = 0;
void setup() {
// initialize the serial port:
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
// Log some shit
Serial.begin(9600);
pinMode(pin_A, INPUT);
pinMode(pin_B, INPUT);
}
void loop()
{
for(pos = 0; pos < 500; pos += 1) // goes from 0 degrees to 180 degrees
{
// step one step:
myStepper.step(1);
stepCount++;
//Serial.print("Position:" );
//Serial.println(pos);
//Serial.print("Schritte:" );
//Serial.println(stepCount);
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the brightness, dont go over 255
if(vspeed + ispeed <= 255) vspeed += ispeed;
}
else {
// B is low so counter-clockwise
// decrease the brightness, dont go below 0
if(vspeed - ispeed >= 0) vspeed -= ispeed;
}
encoder_A_prev = encoder_A;
delay(vspeed);
Serial.print(vspeed);
}
}
for(pos = 500; pos >= 1; pos -= 1) // goes from 0 degrees to 180 degrees
{
// step one step:
myStepper.step(-1);
stepCount++;
//Serial.print("Position:" );
//Serial.println(pos);
//Serial.print("Schritte:" );
//Serial.println(stepCount);
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the brightness, dont go over 255
if(vspeed + ispeed <= 255) vspeed += ispeed;
}
else {
// B is low so counter-clockwise
// decrease the brightness, dont go below 0
if(vspeed - ispeed >= 0) vspeed -= ispeed;
}
}
encoder_A_prev = encoder_A;
delay(vspeed);
Serial.println(vspeed);
}
}
I hope you have an idea why its not working.
Greetings con