RE: How Do You Create a Loop With Multiple Variables?

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

Sofa,

Close but no cigar. :slight_smile:

Instead of using a single equals-sign in the test conditions of the loops and ifs, you need to remember to use a double-equals-sign.

The former makes an assignment. The latter compares.

Blake
PS: Some folks avoid making this mistake by putting their constants on the left side of the comparision, then if they forget an equals-sign, the preprocessor or compiler will catch the mistake because 1=x; is an invalid assignment.

Oh right. Thanks for that. I didn’t even bother compiling the code to make sure it was all good… Yes, if you use the code use ==

s0FaB0y;

I need to check the K&R, but I think you will need to do a little more than just change the ‘=’ to ‘==’.

I am not sure if the following Will evaluate properly…
if ( LineFollowerOne == LineFollowerTwo == LineFollowerThree == 1 ) // If they all return true

It will evaluate properly if written this way:
if ( (LineFollowerOne == 1) && (LineFollowerTwo == 1) && (LineFollowerThree == 1 ) ) // If they all return true

Also if your Values are ‘0’ or NOT ‘0’, you could even write it this way:

        if ( LineFollowerOne && LineFollowerTwo && LineFollowerThree ) // If they all return true

      unsigned int LineFollowerOne; 
      unsigned int LineFollowerTwo; 
      unsigned int LineFollowerThree; 

      while ( 1 )
      {
            LineFollowerOne = GetAnalogInput ( 1 ) ;
            LineFollowerTwo = GetAnalogInput ( 2 ) ;
            LineFollowerThree = GetAnalogInput ( 3 ) ;
            if (  (LineFollowerOne ==  1) && (LineFollowerTwo == 1) && (LineFollowerThree == 1 ) ) // If they all return true
            {
                  SetPWM ( 1 , 127 ) ; // STOP  Right Side
                  SetPWM ( 2 , 127 ) ; // STOP Left Side
            }
            else if (  (LineFollowerOne ==  0) && (LineFollowerTwo == 0) && (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
            }
      }