Well, lets break it down a bit. And first off, I think what you really mean to ask is, “How do you create a loop with multiple requirments?” A variable is simple an int, unsigned char, char, signed char, etc. It hold the value of something. Like int NumberOne = 1; or you could say something like
int LimitSwitchValue = GetLimitSwitch(1, 1) //Not exact syntax but whenever you call LimitSwitchValue you will get the value returned by GetLimitSwitch(1, 1)
I have never used a line tracking sensor, but I think the code would look something like this.
unsigned int LineFollowerOne;
unsigned int LineFollowerTwo;
unsigned int LineFollowerThree;
while ( 1 )
{
LineFollowerOne = GetAnalogInput ( 1 ) ;
LineFollowerTwo = GetAnalogInput ( 2 ) ;
LineFollowerThree = GetAnalogInput ( 3 ) ;
if ( LineFollowerOne = LineFollowerTwo = LineFollowerThree = 1 ) // If they all return true
{
SetPWM ( 1 , 127 ) ; // STOP Right Side
SetPWM ( 2 , 127 ) ; // STOP Left Side
}
else if ( LineFollowerOne = LineFollowerTwo = LineFollowerThree = 0 ) // If they all return false
{
SetPWM ( 1 , 200 ) ; // Drive Right side rpighly 1/2 Speed Forward
SetPWM ( 2 , 200 ) ; // Drive Left side roughly 1/2 speed forward
}
}
This one is a little easier to explain. And has a little bit cooler example too.
unsigned char LimitOne;
unsigned char LimitTwo;
while ( 1 )
{
LimitOne = GetDigitalInput ( 5 ) ;
LimitTwo = GetDigitalInput ( 6 ) ;
if ( LimitOne = LimitTwo = 1 )
{
// STOP
SetPWM ( 1 , 127 ) ;
SetPWM ( 2 , 127 ) ;
}
else if ( LimitOne = 1 && LimitTwo != 1 )
{
// TURN
SetPWM ( 1 , 255 ) ;
}
else if ( LimitOne != 1 && LimitTwo = 1 )
{
// TURN (OTHER WAY)
SetPWM ( 2 , 255 ) ;
}
else
{
// Drive Straight
SetPWM ( 1 , 255 ) ;
SetPWM ( 2 , 255 ) ;
}
}
Have fun! Post any other questions here