Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

use of potentiometer as delay

Status
Not open for further replies.

jorginho1877

New Member
Hello everyone

I am doing the programming of some step by step motors, but I have a doubt in which I want to control the delay with a potentiometer, but I have not been able to do it since the resolution values are not well defined to work with the potentiometer

this is the code

#include <16f877A.h>
#fuses XT, NOWDT
#use delay (clock = 4000000)
#use fast_io(B)
#use standard_io(C)
#use standard_io(D)
#use standard_io(A)

void main()
{

int POT= (input (PIN_A1), 0, 1023, 0, 500); ;

set_tris_B(0b00000000);

while (1){

if (input (PIN_A0)== 1){

output_B(0b00000001);
delay_ms(POT);
output_B(0b00000010);
delay_ms(POT);
output_B(0b00000100);
delay_ms(POT);
output_B(0b00001000);
delay_ms(POT);


}
}

}
 
I'm not familiar with that compiler but I'm assuming that the pot value is remapped to 0 to 500. If so can you change it to delay uS?
You also need to move the pot read inside the while loop and make sure it's reading the ADC.

Mike.
BTW, when posting code use code tags so it keeps it's formatting,
Code:
void main(){
	int POT= (input (PIN_A1), 0, 1023, 0, 500); ;
	set_tris_B(0b00000000);
	while (1){
		if (input (PIN_A0)== 1){
			output_B(0b00000001);
			delay_ms(POT);
			output_B(0b00000010);
			delay_ms(POT); 
			output_B(0b00000100);
			delay_ms(POT); 
			output_B(0b00001000);
			delay_ms(POT); 
		}
	}
}
 
If you are using XC8 or CCS, the inbuilt delay isn't allowed to use variables..

I always wrap the delay...

use it like this:-
C:
void DelayMs(int dly)
   {
    while(dly--)
     delay_us(989);
   }

you have to tweak delay_us to get the correct value... It depends on frequency.. while(); takes 11clock cycles.. the call take two..
 
Ian, I'm not familiar with that compiler (or don't recognize it) but it looks like the line if (input (PIN_A0)== 1){ is reading A0 as digital. So the delay will be either 0mS or 500mS.

OP, what processor/compiler is this?

Mike.
 
Also the analogue isn't read in the while loop, which makes the variable pot superfluous... In fact it isn't read at all..
I have just looked at the documentation.. CCS C compiler and the delay_ms(); does indeed take a variable..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top