PIC- making potentiometer match second potentiometer not working

Thread Starter

z_iron

Joined May 24, 2020
23
I'm tryng to make a DC motor into a servo motor. The motor axle has a gear that is in sync with a gear on a potentiometer (MOTORPOT in the code) and I have another potentiometer that works as an input that the motor potentiometer should match in position however I set this pot(CONTROLPOT in code). Right now the motor doesn't turn for testing purposes and im moving the motor pot myself. I'm getting really odd results, I have two LED's to tell me which condition is active but they both behave in a way I can't explain, sometimes they are on but dim or one is one and fully bright. The LED's arent signalling the right condition when they are supposed to, even when both potentiometers are in the same position. I dont want to make this an XY problem but here are some reasons why I think it might be the case but have no idea how to fix.

  • I'm using a wrong method of getting potentiometer values from ADC?
  • There's something wrong with the conditions?

C:
while(1){
        // MOTOR POT ADC CONVERSION
        ADPCH = 0b00001111;        // MOTOR POT PORT
        ADCON0bits.GO = 1;
        while (ADCON0bits.GO);
        MOTORPOT = ADRES;

        // CONTROL POT ADC CONVERSION
        ADPCH = 0b00001110;        // CONTROL POT PORT
        ADCON0bits.GO = 1;
        while (ADCON0bits.GO);
        CONTROLPOT = ADRES;
        
        MOTORPOT = mapVal(MOTORPOT,0 ,4096,0,200);         // 200 steps
        CONTROLPOT = mapVal(CONTROLPOT,0 ,4096,0,200);
        
        if(MOTORPOT < CONTROLPOT - ERROR){
            RA2PPS = 0b00001001;       // Route PWM Output to MOTOR IN1
            PORTAbits.RA4 = 0;         // Set IN2 Low
            CCPR1 = 150;               // PWM Speed
            
            PORTBbits.RB4 = 1;         // Set Test LED A
            PORTBbits.RB5 = 0;         // Clear Test LED B
        }
        if(MOTORPOT > CONTROLPOT + ERROR){
            RA4PPS = 0b00001001;       // Route PWM Output to MOTOR IN2
            PORTAbits.RA2 = 0;         // Set IN1 Low
            CCPR1 = 150;               // PWM Speed
            
            PORTBbits.RB5 = 1;         // Set Test LED B
            PORTBbits.RB4 = 0;         // Clear Test LED A
        }
        if((MOTORPOT < CONTROLPOT + ERROR) &&(MOTORPOT > CONTROLPOT - ERROR)){
            CCPR1 = 0;                  // Set PWM Speed to 0
            PORTBbits.RB5 = 1;          // Set Test LED A
            PORTBbits.RB4 = 1;         // Set Test LED B
        }
        
    }
Can someone please help me fix the code to make the two leds turn on when the value of the motor potentiometer is the same is as the control potentiometer within +/- the error value but also account for the fact the motor has to turn clockwise/anti depending on the direction of turn to get to the desired value as I've tried to do already.
 

AlbertHall

Joined Jun 4, 2014
11,129
A motor drives a pot (via a gearbox?). How far does the motor continue to run after power is removed? As far as the error?
Maybe the system drives the motor to reduce the error. When the error is low enough the motor is turned off but continues to turn for a while until the error limit is exceeded but now in the opposite direction.

Does it behave better with a larger value of error allowed, arranged to be much greater than the distance the motor continues under its own momentum?
 
Top