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.
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
}
}
}
Attachments
-
309 bytes Views: 2