Array for status history?

This is a discussion on Array for status history? within the C Programming forums, part of the General Programming Boards category; Hi! (beginner here) I have an external interrupt (occurring every second) driven program loop that ends with setting a status ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    62

    Array for status history?

    Hi! (beginner here)

    I have an external interrupt (occurring every second) driven program loop that ends with setting a status variable to ether 1 or 0.

    I need to set a flag to "yes"... only after 30 interrupts has resulted in 30 consecutive "0's from my loop .... otherwise flag set to "no". The flag must be checked after every loop.

    I don't know if an array can be appended to in a FILO fashion for this.

    Any ideas on how to best go about this?

    Thanks!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why bother with an array? It seems that a simple count of consecutive 0's is enough:
    Code:
        int cnt = 0;
    
        // inside your loop
            if (status == 0) {
                if (++cnt >= 30)
                   flag = 1; // you may also want to reset cnt to 0
            }
            else
                cnt = 0;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    62
    Brutal Juice! ...... thanks! : )

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    [edit] Whilst replying you seem to have sorted it out - So don't worry about this. [/edit]

    I'm guessing you are programming some micro-controller

    I'm guessing by FILO, you mean First-in-last-out -> Or more commonly "LIFO" (Last in first out)

    You need to create a stack.
    Last edited by Click_here; 10-10-2012 at 05:56 PM.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Rick19468 View Post
    Brutal Juice! ...... thanks! : )
    That sounds nasty!
    You're welcome, though.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    62
    Thanks TEIAM!

    Yes ..... a GPS module sending location x and y info to my MCU. I needed to disable an external device (via flag) until I am assured that my "status" was stable for at least 30 seconds.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    62
    Brutal Juice = Awesome!! : )

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Not sure that I like the variable incrementing during a conditional, but that's a minor niggle and is easily resolved.

    It is amazing how often you can overlook the simple way, though. I watched a small group of people once trying to compensate for what happened when you needed to switch between two players (numbered player one and player two). The sheer amount of them that fell into the trap of:

    Code:
    if(player == 1)
         player = 2;
    if(player == 2)
        player = 1;
    was astounding and they came up with all manner of ways to compensate for the problem with that code with extra variables, using min(), max(), modulo arithmetic and all sorts.

    When they asked to see my line, they kicked themselves. We had suitable-enough preconditions and constraints to be able to do just:

    Code:
    player = 3 - player;
    Not saying it was *good* code but it was amazing how many of them missed the simple.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,775
    Quote Originally Posted by ledow
    We had suitable-enough preconditions and constraints to be able to do just:
    Code:
    player = 3 - player;
    Not saying it was *good* code but it was amazing how many of them missed the simple.
    In my opinion, the simple is:
    Code:
    player = (player == 1) ? 2 : 1;
    What you came up with is not so simple because it relies on a magic number (and mathematical property), rather than the simple "if it is this player, then the next player must be that, otherwise it must be this" logic. Rather, it is elegant, being more concise, and probably more efficient.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,392
    I have to admit that I like laserlight's solution because it's self documenting -> It's something you would know exactly what is happening in the code.

    However, being the person I am, I came up with my own small solution that would make no sense to the next person coming along!
    Code:
    player ^= 3;
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Linked Lists: Status Access Violation!!
    By Zyreal687 in forum C Programming
    Replies: 8
    Last Post: 09-02-2010, 07:59 PM
  2. I need help in history!
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-11-2003, 08:18 AM
  3. History
    By Linus in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-15-2002, 03:46 AM
  4. History Lesson
    By CAP in forum C Programming
    Replies: 6
    Last Post: 06-16-2002, 09:07 PM

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