Making a series of Booleans act as a string of numbers

This is a discussion on Making a series of Booleans act as a string of numbers within the C Programming forums, part of the General Programming Boards category; Hello! I recently started programming in C and right now I am programming a robot using a PIC32 microcontroller. There ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Smile Making a series of Booleans act as a string of numbers

    Hello! I recently started programming in C and right now I am programming a robot using a PIC32 microcontroller. There are six input sensors and right now I have separate variables set to each of them. I would like help organizing the variables. Right now in order to test conditions I have to write a huge line like
    Code:
    if (FarL==1 && MidL==1 && InsL==1 && InsR==0 && MidR==1 && FarR==1)
    {
    /* code to turn left or something here */
    }
    I would like it organized to something like:
    Code:
    if(sensorInput=="111000")
    {
    /* code to turn left or something here */
    }
    The application of this is irrelevant; all the standard rules of C apply. I would really like help, if this prototype I have set up is not possible or not optimal, I am open to suggestions I just would like to be a bit more organized. I am also open to using case statements, pointers, arrays, linked files, functions, and any other method to help organize.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    you have six inputs so that gives plenty of combinations that can be stored in an array and referenced according to their sequence to then do a particular action, but its hard to say more because there is no such thing as just 'code to turn left' its all relative to end application in the sense that if you are making an object on screen turn left then that would need code saying stuff like >

    while left
    Decrement Xcoordinate of vehicle image
    increment animation left [clipcount]
    blit animation
    increment clip count
    if clip count == maxclips
    clip count = 0;

    but your robot would need other instructions to turn left,
    i dunno >



    while loop that performs 'newaction' until it changes
    //
    //
    get input() //include null action in case nothing changes

    switch(input_type)

    case left start steering servo, direction = anticlockwise, newaction = left

    case right, reverse of above
    case forward start main motor, direction = clockwise (or however it is built)
    case back start main motor, direction = anticlockiwse (reverse of above)
    case stop
    //
    //
    //
    return to start loop, get more instructions
    Last edited by rogster001; 12-04-2009 at 08:04 AM.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Seems like a great job for bitfields:
    Code:
    #include <stdio.h>
    
    #define FarL 0x1
    #define MidL 0x2
    #define InsL 0x4
    #define InsR 0x8
    #define MidR 0x10
    #define FarR 0x20
    
    int main(void)
    {
      unsigned int bitfield = FarL | MidL | InsL | MidR | FarR;
    
      if(bitfield == 0x37)
      {
        /* Turn left */
      }
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    use bitmaps, similar in how c programs manipulate color paletts. This may help to guide you from a simple google search. There are also forums here with code very similar.


    256-Color VGA Programming in C - bitmap.c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 11:18 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21