Like Tree1Likes
  • 1 Post By Matticus

Complicated Logic (Please take a look)

This is a discussion on Complicated Logic (Please take a look) within the C Programming forums, part of the General Programming Boards category; Hello everybody and thanks for your help so far. This time different than before I would like to ask your ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    19

    Complicated Logic (Please take a look)

    Hello everybody and thanks for your help so far.

    This time different than before I would like to ask your opinion on some code. I would appreciate if you tell me if you have a simpler, better idea on how to implement the following:

    I have a sensor that goes ON or OFF. I want to check the sensor and do nothing while it is OFF. Then when it gets ON the logic of the code advances. This is quite simple as in

    Code:
      while(sensor==OFF); //holds it until it goes ON
    however, to complicate things this sensor can have some kind of flickering, meaning it can sometimes gives bad readings. So I want to modify the above code to advance the logic only when the sensor goes ON for at least 5 ms. I can use the functions wait_set and wait_check that sets a waiting time

    so I wrote something like

    Code:
    do{
         while(sensor==OFF); //hold it
         wait_set(5); //set a wait of 5 miliseconds
         if(sensor==OFF) continue; //if it was flickering go to the start of the do loop
         while((sensor==ON)&&!(wait_check());// wait for the sensor to be ON for 5 miliseconds
    
       } while(sensor==OFF);
    Anyhow, it seems awfully complicated.

    Any ideas on how to implement this in a simpler way(if there is one) will be greatly appreciated

    Thanks

    Kansai
    Last edited by KansaiRobot; 11-12-2012 at 06:14 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,273
    This code should do the same thing as the code you posted:

    Code:
    do
    {
        while(sensor == OFF);
        wait_set(5);
    } while(sensor == OFF);
    Like the code you provided, this will not check to see if the sensor is ON for the whole 5 ms - instead, when it goes ON, it waits 5 ms, checks again, and if still ON, then proceeds.

    Is this what you want? Or do you want to make sure it is on for the full 5 ms? Would it be possible to make a hardware change (adding a simple debounce circuit)?

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thank you very much for your response!
    And sorry for being vague about what the wait functions do. In fact the code I posted do check if the sensor is ON for the whole 5 ms. You see, wait_set does not actually stops the code. It just sets a waiting time that needs to be rechecked with wait_check. So the waiting is in the last while loop.
    Code:
    while((sensor==ON)&&!(wait_check());
    To rephrase

    wait_set: Sets a waiting time (just an assigment)
    wait_check: Checks if the current time has surpassed the waiting time.


    Thanks again!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,273
    Ah, I was mistaken and didn't realize that the timer was running in the background.

    Considering the limitations of the specs, I personally can't see any way to make it much more efficient, other than getting rid of line 4 ("if(sensor==OFF) continue;"). That will be taken care of if the "while sensor is ON" loop breaks from the sensor being OFF.

    Perhaps someone else will come along with a better solution. Sorry I couldn't be of more help. Best of luck!
    KansaiRobot likes this.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    834
    Assuming wait_check() returns false/zero before the set timeout has elapsed, and true/nonzero afterwards, then
    Code:
        do {
    
            /* Wait until sensor (flickers) on. */
            while (SENSOR != ON)
                nothing();
    
            /* Set timeout at 5 milliseconds. */
            wait_set(5);
    
            /* Keep checking the sensor stays on, until it changes or timeout elapses. */
            while (SENSOR == ON && !wait_check())
                nothing();
    
            /* We are done if the sensor stayed ON until timeout. */
        } while (SENSOR != ON || !wait_check());
    You only exit from the outer do..while() loop when the sensor is ON, and timeout has elapsed (wait_check() returns nonzero/true).

    The first while loop iterates until the sensor at least flickers to the ON state. When that happens, the five-millisecond timeout is also set.

    The second while loop iterates until the timeout elapses, or the sensor goes out of the ON state. (After that happens, the outer loop starts from the beginning unless the sensor is in the ON state and the timeout has happened.)

    Note that I deliberately wrote the loop to only use the ON constant. After you exit the above loop, the sensor has stayed in the ON state for 5 milliseconds.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    All sensors are subject to false reading.

    My favourite way is something I learnt as "delta modulation" - Note that I've only ever heard of it being called that in the text book I had at Uni.

    Basically, the sensor is continuously polled. If the sensor has a positive reading, increment a variable, otherwise decrement it.
    If the variable has gone over a threshold, it is considered 'on'.

    Code:
    
    int main (void)
    {
        for (;;)
        {
            if (sensor_on())
            {
                /* normal program states*/
            }
            else
            {
                /* do nothing */
            }
        
        }
        return 0;
    }
    
    
    int sensor_on(void)
    {
        static int dm = 5;
        
        if (input == HIGH)  // "input" refers to the port being read
        {
            if (dm < 10) dm++;
        }
        else
        {
            if (dm > 0) dm--;
        }
        
        return (dm >= 5);
    }
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,273
    Quote Originally Posted by Nominal Animal View Post
    ...
    For my sake, I thought it'd never come to this - but I am going to contradict you here. I offer that the code you provided is of practically no improvement to the OP.

    In fact, if you check the comparison, the only line that logically differs is the argument to the "while()" loop. But, I'm assuming that the slew rate of the oscillator is much faster than that of the "flickering" noise.

    Code:
    /* your code */                                /* their code */
    do                                             do
    {                                              {
        while (SENSOR != ON)                           while(sensor==OFF); 
            nothing();
    
        wait_set(5);                                   wait_set(5);
    
        while (SENSOR == ON && !wait_check())          while((sensor==ON)&&!(wait_check());
            nothing();
    } while (SENSOR != ON || !wait_check());       } while(sensor==OFF);
    From your previous posts, I'm probably wrong in some obscure way and will soon get my behind handed back to me...

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    Here is a picture demonstrating the DM following an analogue signal

    DM picture
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    834
    Quote Originally Posted by Matticus View Post
    I offer that the code you provided is of practically no improvement to the OP.
    Agreed. It only implements the logic specified by the OP.

    The logic described by Click_here in post #6 (specifically, the sensor_on() function) is close to what I use for buttons and switches on my microcontrollers. (I don't like the extra delay caused by hardware debounce, such as a capacitor, in principle; I prefer to debounce in software instead.)

    Quote Originally Posted by Matticus View Post
    From your previous posts, I'm probably wrong in some obscure way and will soon get my behind handed back to me...
    Nope, you're right.

    Besides, I make more than my fair share of errors and mistakes, and do my best work when reviewed by critical thinkers. I really, really appreciate cool detailed criticism, because it helps me get better at what I do.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    19
    Thanks for all the answers

    I have been told that this is a simpler and equivalent algorithm

    Code:
    do {
       if(sensor==ON) wait_set(5);// Wait if and only if sensor is ON! 
    
       while((sensor==ON) && !(wait_check()));
    
    
    } while(sensor==OFF);
    I suppose the first while was not needed...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complicated riddle?
    By eXeCuTeR in forum C Programming
    Replies: 20
    Last Post: 11-21-2007, 05:55 AM
  2. Probably way too complicated for me.
    By Necrofear in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2006, 05:41 AM
  3. complicated
    By ZakkWylde969 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2003, 09:15 AM
  4. complicated comparison
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2003, 04:25 PM
  5. Nothing complicated, just...
    By SMurf in forum Windows Programming
    Replies: 1
    Last Post: 09-01-2002, 05:59 PM

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