Potentiometers are not connected to the microcontroller at all

Thread Starter

Samantha Groves

Joined Nov 25, 2023
160
Hello!I would like some help with my homework.The goal is to control a step motor using 3 push buttons and a potentiometer and a servo motor using 1 potentiometer.I will upload the PicsimLab configuration and the code:

C:
unsigned int voltage1 = 0;
 unsigned int voltage2 = 0;
 long data1,data2;

void turn(int direction,int speed)
{



   int i;

   if(direction==0)
   {
   //turn right
  
   for(i=0;i<50;i++)
  
   //begin turning right
  
   {
  
  
   if(PORTD.F2==1)
   {
   //if button to stop turning is pressed , stop turning
   break;
   }

   PORTB = 0b10010000;
  
   //begin sequence of turning right

   for(i=0;i<speed;i++)
   {
    //delay based on speed

   delay_ms(1);

   }


   PORTB = 0b11000000;
  
   for(i=0;i<speed;i++)
   {
   delay_ms(1);

   }




   PORTB = 0b01100000;

   for(i=0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b00110000;

   for(i=0;i<speed;i++)
   {
   delay_ms(1);

   }
  
   }
  
   }
   else if(direction==1)
   {
   //turn left
   for(i=0;i<50;i++)
    //begin turning left
   {
  
   if(PORTD.F2==1)
   {

   break;
   }

   PORTB = 0b00110000;
  
   //begin sequence of turning left

   for(i=0;i<speed;i++)
   {
   //delay based on speed
   delay_ms(1);

   }

   PORTB = 0b01100000;

   for(i=0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b11000000;

   for(i=0;i<speed;i++)
   {
   delay_ms(1);

   }

   PORTB = 0b10010000;

   for(i=0;i<speed;i++)
   {
   delay_ms(1);
   }

   }
  
   }


}


void main() {

  TRISB = 0b00001111;
 
  //set pins from B4 to B7 as outputs
 
  TRISD = 0b00000111;
 
  //set pins from D0 to D2 as inputs
 

 
  ADCON0 = 1;
 
  //configure VDD as Vref, PORTA pins as analog
 
  ADCON1 = 0x08;
 
  TRISA = 0xFF;
 
  //set PORTA as inputs


 

 
 
 
  ADC_init();
 
  //initialise ADC
 
  PWM1_init(500);
 
  //initialise PWM
 

 
 
 

 
  while(1)
  {
 
  PWM1_start();
 
  //start PWM
 
  delay_ms(200);
 
  voltage1 = ADC_read(0);
 
  // read analog value of channel 0
 
  data1 = (long)voltage1 * 255;
 
  data1 = data1/1023;
 
 
  //convert into a scale from 0-255
 
  delay_ms(200);
 
  voltage2 = ADC_read(1);
 
  data2  = (long)voltage2 * 255;
 
  data2  =  data2/1023;
 
  //convert into a scale from 0-255
 
  PWM1_Set_Duty((int)data2);
 
  //set PWM duty cycle
 

 

 
    if(PORTD.F0)
    
    {
 
    turn(1,(int)data1);
    
    // turn
    
    }
    
    if(PORTD.F1)
    {
    turn(0,(int)data2);
    
    // turn
    }
    




 
  }
 

 

 
 


}
The problem is that in my output the values of the potentiometer(s) dont seem to matter at all so I was wondering what could cause this.
 

Attachments

MrChips

Joined Oct 2, 2009
34,629
The debugging approach is to strip your code down to a bare minimum.
In other words, read the ADC and use debug to determine what value is being read from the ADC.
 

Ian0

Joined Aug 7, 2020
13,097
Check what ADC_Read() really does.
My guess is that it just reads the value from the ADC register, and you have to send some other command to actually start the ADC conversion process.
I regard peripheral driver libraries with the utmost suspicion. They are rarely efficient, sometimes they don't work, and often they get updated along with software updates and afterwards work differently.
My advice is to read the processor manual and write your own drivers by writing the appropriate values to the relevant. registers.
 

WBahn

Joined Mar 31, 2012
32,707
Hello!I would like some help with my homework.The goal is to control a step motor using 3 push buttons and a potentiometer and a servo motor using 1 potentiometer.I will upload the PicsimLab configuration and the code:

The problem is that in my output the values of the potentiometer(s) dont seem to matter at all so I was wondering what could cause this.
You appear to have taken a very common -- and very poor -- approach to designing your solution. Throw everything together, plug it in, and hope that it happens to work.

The problem with this approach is that a tiny error in one small part of your solution can make it so that nothing works, and now you are left with trying to find where it happens to be hiding since you really have no idea what parts of your solution are correct and which parts aren't.

This is why incremental design, implementation, and testing is so important.

There's no point trying to implement code to use the value produced by a potentiometer until after you have confirmed that you can write code that successfully reads the value produced by the pot.

How you do this depends on the capabilities of the platform you are using to develop your code. The PIC18F452 is not a board, but rather a particular Microchip MCU, so without knowing what is on the actual board, it's hard to make suggestions. If the board has digital I/O pins accessible, then you could simply read the ADC input from the pot and write that value to the output pins and then look at them with a logic analyzer or even just a DMM. You will quickly be able to determine whether you are able to read the values and also what those values actual are and how they relate to the pot setting. If you see no change in the readings, then either the pot isn't changing the voltage at the ADC input (by enough to make a difference), or you have something configured incorrectly, or your code is not reading the ADC correctly. If you are seeing a change, but not the type and size of changes you expect, that's a different set of problems to work through.
 
Top