[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

using a timer to save average reading to database

im trying to read an average from an input source over a minute and then write this to an access db.

saving to the db isnt the problem.

i have an external class that is feeding me a current reading at regular intervals of 1sec. I want to use a timer to count down 60 secs and from that sixty secs i need to work out the average reading and save this to a database.

most of it i have an idea of how to acomplish but the timer is giving me grief.

any ideas would be most appreciated.


 -- also, lol,  once i have this db full of average readings i want to read from the db using a chart object to draw charts on the fly
    - whats the best way to go about this?

again, any help, pointers or ideas would be most appreciated.

cheers,
   Ray
0
raynimmo
Asked:
raynimmo
  • 7
  • 6
1 Solution
 
Bob LearnedCommented:
1) .NET version?

2) What kind of timer are you using (there are 3 different types)?

3) What grief are you having?

4) Basically, how can I best answer your questions?

Bob
0
 
raynimmoAuthor Commented:
bob,

im using vs.net 2003

trying to use a form timer ( System.Windows.Forms.Timer)
dont know if a different type would suit my task better.

basically once i have my average reading im fine, its getting it.

i know to divide the sum of the readings buy the interval taken to give me the average - 1st year maths

its what is the best way to tackle storing the readings per second

should i build an array and populate it with my readings then recall them and work out the average? - lecturer told me not to use array !!

or something else - its the 'somthing else' im not sure of.

im a software tech student with a crap lecturer that doesent really help - you guys have been my help.

cheers,

   Ray
0
 
Bob LearnedCommented:
Ray,

1) I feel your pain, man!!!

2) I am not exactly sure what you are describing?

3) What is a reading?

4) One way would be to not use a timer, but get the start time, run the process, get the end time and calculate the total seconds.  A TimeSpan structure would be useful for this, because it has a TotalSeconds property, and it can be easily used to subtract one time from another.

Bob
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
raynimmoAuthor Commented:
right - heres the long winded description

lecturer has supplied us with a dll control, the control when ran generates a reading, he is trying to make it like it is a pipe and the readings are the pressures from it.

i am using a combobox to change the reading interval from the control using SetInterval().

We have been told that the control has four methods we can access a SetInterval(), GetReading(),SetThresholdUpper() and SetThresholdLower() and using these to get readings from the control - a KnobControl he calls it.

we have to set the max and the min readings and when they are exceeded sound an alarm.

The reading im trying to take the average of is the current recalled from the GetReading() method.

we have to take those pressures pass it from one form to the other using a valueUpdatedEvent class and when a button is clicked we have to save the pressures generated at that time to an access db. The pressures we have to save are the average pressures over 1 minute and then display those readings on a chart.

and we have to do all this for three separate pipes, all to be ran simultaneously.

i posted a question previously for some help on a different part of this

here -->  http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21823615.html

its a part of our uni coursework, due in tomorrow - aarrgghhh

the sections in question are only worth 5% of the mark but im a bit of a perfectionist and i figure if i at least attempt it then i may get some of that 5%

The main bit that is giving me trouble is what to do with the readings generated over the 60 seconds? where in the timer code should i take the readings, and how?


any help would be appreciated.

   Ray.
0
 
Bob LearnedCommented:
>>where in the timer code should i take the readings, and how?
If you are using a form Timer object, do you have a Tick event handler defined?

Bob
0
 
raynimmoAuthor Commented:
not yet

whats the best way to go about that?
0
 
Bob LearnedCommented:
Double click on the Timer object on the component window.

Bob
0
 
Bob LearnedCommented:
Make sure that you have set the Interval property and the Enabled property, or the timer won't function.

Bob
0
 
raynimmoAuthor Commented:
i thought thats what you meant,

yeah i done that already.

should i stick my code for writing to the db in there?

i assume then that it will write to the db every 60 secs, but i want to store a temporary value every second and then average those values and then write the average to the db every 60 secs.

  Ray

0
 
Bob LearnedCommented:
It sounds like you have 2 timing operations:

1) Write to the database every minute
2) Store a temporary value

Do you have 2 timers, or only 1?

Bob
0
 
raynimmoAuthor Commented:
sorry for late reply, been travelling home.

i know what you mean with the two timers

i can use as many as i want/need

the only component that is supplied as a control we need to add to the toolbox

ill hava a bash at it with two timers and paste my code to see what you think, hopefully it works.
feel free to make any comments/code examples you think i may need.

cheers bob

    Ray
0
 
raynimmoAuthor Commented:
still aint got it;

been working on it this morning, came up with whats below.


    'Timer initially set for 1 second intervals
    Private Sub p1Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p1Timer.Tick
        p1TempReadings.Add(P1CurrentValue)  'adding to arraylist
    End Sub

    'Timer initially set for 60 second intervals
    Private Sub p1ArrayTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p1ArrayTimer.Tick
        Try
            Dim Averages As System.Data.DataRow
            For i As Integer = 0 To p1TempReadings.Count - 1  
                Averages(i) = DSp1Averages1.Pipe1AveragePressures.NewRow
                Averages.Item("AveragePressure") = p1TempReadings(i)
                DSp1Averages1.Pipe1AveragePressures.Rows.Add(Averages(i))
                ODBDAp1Averages.Update(DSp1Averages1)
            Next i
        Catch ex As Exception
            MsgBox("Error writing to Pipe 1 Average Database")
        End Try
        p1TempReadings.Clear()
    End Sub

using two timers, first one goes off every second and saves reading to arraylist - or does it?
second timer goes off every 60 secs, i havent worked out how to get the average of all the values in the arraylist, could mayby do it as sql command, but then id need to save to db and average that then resave to db ?????

i think im along the right lines here but could do with some help from someone who knows better.

its now 13:30 here in edinburgh, report is due in by 16:15

any ideas before 15:00 GMT would be most appreciated, before i get it on disk and finish the write up for it.

cheers in advance.
   Ray
0
 
raynimmoAuthor Commented:
oh well, thats me away to hand in the report, thanks for the help bob, ill award you the points.

hopefully i dont lose too many marks for not completing that section.

cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now