C# crew: can you pass some integer into a timer event

Discussion in 'OT Technology' started by enfiniti, Mar 3, 2008.

  1. enfiniti

    enfiniti How firm thy friendship ... OHIO!

    Joined:
    Sep 12, 2006
    Messages:
    4,813
    Likes Received:
    0
    Location:
    Columbus
    Code:
    [SIZE=2][COLOR=#008000]// Timer02.cs - Displaying Date and Time
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000]// Using the Timer class.
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000]// Press Ctrl+C to end program.
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000]//------------------------------------------
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2] System;
    [/SIZE][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2] System.Timers;
    
    [/SIZE][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][SIZE=2][COLOR=#008080]myApp
    [/COLOR][/SIZE][SIZE=2]{
     
    
    [/SIZE][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Main()
    {
    
    [/SIZE][SIZE=2][COLOR=#008080]Timer[/COLOR][/SIZE][SIZE=2] myTimer = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2][COLOR=#008080]Timer[/COLOR][/SIZE][SIZE=2]();
    myTimer.Elapsed += [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2][COLOR=#008080]ElapsedEventHandler[/COLOR][/SIZE][SIZE=2]( DisplayTimeEvent );
    myTimer.Interval = 1000;
    myTimer.Start();
    [/SIZE][SIZE=2][COLOR=#0000ff]while[/COLOR][/SIZE][SIZE=2] ( [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].Read() != [/SIZE][SIZE=2][COLOR=#800000]'q'[/COLOR][/SIZE][SIZE=2] )
    {
    ; [/SIZE][SIZE=2][COLOR=#008000]// do nothing...
    [/COLOR][/SIZE][SIZE=2]}
    }
    [/SIZE][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] DisplayTimeEvent( [/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2] source, [/SIZE][SIZE=2][COLOR=#008080]ElapsedEventArgs[/COLOR][/SIZE][SIZE=2] e)
    {
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].Write([/SIZE][SIZE=2][COLOR=#800000]"\r{0}\n"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#008080]DateTime[/COLOR][/SIZE][SIZE=2].Now);
    [/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] sensorSwitch;
    sensorSwitch = 1;
    sensorSwitch = sensorSwitch % 6;
    sensorSwitch++;
    
    [/SIZE][SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][SIZE=2] (sensorSwitch)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 0:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query x-Axis Gyro\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 1:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query y-Axis Gyro\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 2:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query z-Axis Gyro\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 3:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query x-Axis Accel\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 4:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query y-Axis Accel\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2] 5:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Query z-Axis Accel\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff]default[/COLOR][/SIZE][SIZE=2]:
    [/SIZE][SIZE=2][COLOR=#008080]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#800000]"Something is broken\n"[/COLOR][/SIZE][SIZE=2]);
    [/SIZE][SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];
    }
    }
    }
     
     
    [/SIZE] 
    
    I want to sample 6 different sensors but pass them in 1 at a time and only sample 1 every so often due to our shitty ADC mux. any ideas?
    I am dumb
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If you want the sensorSwitch variable to count and remember the number of timer ticks, make it a member of the myApp class, i.e. make it "global."
     
  3. enfiniti

    enfiniti How firm thy friendship ... OHIO!

    Joined:
    Sep 12, 2006
    Messages:
    4,813
    Likes Received:
    0
    Location:
    Columbus
    tried that, says it is unrecognized
     
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    let me guess, each time it only queries z-axis gyro?
     
  5. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    yea, i dont do c# but wont sensorSwitch be initialised every time its routine is called?
     
  6. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    that's exactly what I was pointing out... Because the variable is initialized and the value "1" is stored in it. Then 1 mod 6 is simply "1". Then it is incremented to "2".... Which means that the "z-axis gyro" case is always true.

     
  7. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Code:
    namespace ConsoleApplication1
    {
        using System;
        using System.Timers;
    
        class Program
        {
            private static int sensorSwitch;
    
            static void Main()
            {
                Timer myTimer = new Timer(1000);
                myTimer.Elapsed += DisplayTimeEvent;
                myTimer.Start();
    
                while (Console.ReadKey().KeyChar != 'q') ;
            }
    
            public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
            {
                Console.WriteLine("{0}\r\n", DateTime.Now);
                
                // Reset the counter if we've wrapped around past the end
                if( sensorSwitch > 5 || sensorSwitch < 0) sensorSwitch = 0;
    
                switch (sensorSwitch)
                {
                    case 0:
                        Console.WriteLine("Query x-Axis Gyro\n");
                        break;
                    case 1:
                        Console.WriteLine("Query y-Axis Gyro\n");
                        break;
                    case 2:
                        Console.WriteLine("Query z-Axis Gyro\n");
                        break;
                    case 3:
                        Console.WriteLine("Query x-Axis Gyro\n");
                        break;
                    case 4:
                        Console.WriteLine("Query y-Axis Gyro\n");
                        break;
                    case 5:
                        Console.WriteLine("Query z-Axis Gyro\n");
                        break;
                    default:
                        throw new Exception(
                            string.Format("Bad sensor switch number: {0}", sensorSwitch) );
                }
    
                sensorSwitch++;
            }
        }
    }
    
    The thing that will make this work is the static variable.

    The other changes are you don't need to instantiate the delegate, you can just pass the delegate method (line 13).

    You can pass the interval when you create the Timer; though setting the Timer property is at least self-documenting.

    Using ReadKey means you can hit the q key rather than have to hit q then Enter.

    Use Console.WriteLine instead of Console.Write and then the carriage return line feed. Also, if you're using carriage return and line feed, don't put anything between them: i.e. "\r\n" always in that order, and together.

    Or you can just use the Environment.NewLine string constant.

    Also I suggest throwing an Exception or having an assertion for important errors.
     
  8. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Haha I forgot to take the \r\n out of Console.WriteLine +D

    Code:
    Console.WriteLine(DateTime.Now);
     
  9. enfiniti

    enfiniti How firm thy friendship ... OHIO!

    Joined:
    Sep 12, 2006
    Messages:
    4,813
    Likes Received:
    0
    Location:
    Columbus
    thanks gentlemen, I am probably going to fail at the code for the serial interface so I will probably be back in here soon
     

Share This Page