Hey there
I have a project in which I need to implement an asymetrical cycle(up faster, down slower and vice-versa)
for this am using an arduino uno, h bridge and a 12v dc motor. with a proportional controller code.
To trigger the change of speeds, I'm using a hall sensor that senses a magnet attached to a wheel on the motor. Everything works fine with just one speed.
I have made the following code for this asymmetrical cycle(two turns with two different speeds/ or half a turn with two different speeds)
But when I load it to the arduino nothing happens
#define encoder 2
#define hallSensor 3
#define enA 9
#define in1 6
#define in2 7
volatile long encoderPos = 0;
volatile byte hallValue = HIGH;
int interval = 100;
long prevMillis = 0;
long nowMillis = 0;
int pot;
long outP = 0;
int ref = 0;
void setup() {
TCCR2B = TCCR2B & 0b11111000 | 1;
Serial.begin(57600);
pinMode (encoder, INPUT_PULLUP);
pinMode (hallSensor, INPUT_PULLUP);
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
attachInterrupt(digitalPinToInterrupt(encoder), updateEncoder, RISING);
attachInterrupt(digitalPinToInterrupt(hallSensor), toggle, RISING),
prevMillis = millis();
}
void loop() {
nowMillis = millis();
if (nowMillis - prevMillis > interval){
prevMillis = nowMillis;
if (hallValue == LOW){
encoderPos = 0;
}
if (encoderPos < 40){
int ref = 25;
} else { int ref = 55;
}
int erro = ref - encoderPos;
int P = 20;
int outP = erro * P;
pwmOut(outP);
encoderPos = 0;
Serial.print(encoderPos);
Serial.print(" ");
Serial.println(outP);
}
}
void pwmOut(int out) {
if (out > 255)
out = 255;
if (out < 0)
out = 0;
analogWrite(enA, out);
}
void updateEncoder (){
encoderPos++;
}
void toggle (){
hallValue != hallValue;
}
P_asy_3.ino (1.6 KB)