Regarding your code:
1. please use [code=syntax]...code tags...
[/code] they make your code easier to read. Read this.
2. what you do next is determine what value is HIGHER, the previous value or the current value. Surely you are familiar with relational operators (you'll have to scroll down a little).