I'm doing a practice problem where I need to read pseudo code and understand what it does. I know some of it, but can someone read my logic for the pieces of code and tell me if they're correct?
For this code, I believe it will move the robot forward while i<650, so it moves it forward for 650 steps in total. Then it sets the motors to 0 when it's finished.
Code:
while(i<650)
{ rotateCW(2);
rotateCCW(1);
Delay1KTCYx(50);
i++;
LATD=0x00; //Set all Port D to 0
}
This code I'm having the most trouble with. So while(1) the pot(potentiometer) read's the ADC. Then it scales the adc, not 100% sure what the +20 and *.22 mean, I think it has to do with binary conversions?
Now it sets pin 1 of port 1 to 0, delays based on pot above, and sets it back to 1, and delays again. I'm confused what this function is actually doing, I assume something to do with controlling motor speed based on the potentiometer.
Code:
void main(void){TRISD = 0x00; // Setting port D to outputs
LATDbits.LATD0 = 1; // Setting bit 0 of port D to 1
while(1)
{
pot = readADC();
pot = pot * 0.2297 + 20;
LATDbits.LATD1 = 0;
Delay10KTCYx(pot);
LATDbits.LATD1 = 1;
Delay10KTCYx(pot);
}
return;
}
My best guess is: The car moves forward 400 steps, stop, turn right for 230 steps, stop. At this point it would begin moving straight, and it would turn left/right as needed to keep the robot in the center of the track.
Code:
void main(void)
{
for(i=0;i<400;i++)
{
rotateCW(2,motor); //Rotate motor two (one step)
rotateCCW(1,motor); //Rotate motor one (one step)
Delay1KTCYx(50);
i++;
LATD=0x00; //Set all Port D to 0
}
i=0;
while(i < 230)
{
rotateCW(2); // Rotate motor two by one step
rotateCW(1); // Rotate motor by one step
Delay1KTCYx(50); // Changing the argument in the delay function will change the speed at which
// the motor rotates. Delay equals 1000 x 50 x 1/12 = 4.167 ms
i++;
LATD = 0x00;
}
i=0;
for (i=0;i<400;i++)
{
SetChanADC (ADC_CH0);
ConvertADC();
while (BusyADC());
leftsensor = ReadADC(); If (leftsensor>400)
{
TurnRight(5);
GoForwardx(1);
}
If (leftsensor<300)
{
TurnLeft(5);
GoForwardx(5);
}
If (leftsensor<400)&&(leftsensor>300)
{
GoForwardx(5);
i++;
}
}