Variable increment in C

Status
Not open for further replies.

djc

Advanced Member level 1
Joined
Jan 27, 2013
Messages
402
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
India
Visit site
Activity points
4,554
Is there any way to determine if a variable in C, let it be a 'unsigned int a is still incrementing or stopped incrementing. So that i can continue with further process.
 

You need another unsigned int variable which holds the previous value of the first variable. If the value of first variable is > the value of the second variable used to hold previous value then the first variable has incremented. You then have to assign the value of first variable to 2nd variable.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
unsigned int counter = 0, oldCounter = 0;
 
void main() {
 
    while(1){
 
        if(oldCounter < counter){
 
            //Code to be executed if counter has incremented
            //counter gets incremented or dectemented elsewhere in the program
 
            oldCounter = counter;
        }
        else if(oldCounter > counter){
 
            //if counter has decremented
    
            oldCounter = counter;
        }
 
    }
}

 

I have switches connected on PORTB.7 of ATMEGA8 through parallel in serial out ic 74hc589. Switched are initially pulled up. So when any switch is pressed '0' volt appears on input of 74hc589. Then clock has to be given to move the data out serially. Now this will run continuously. And i am checking the condition using' if, else' statement.Such that
Code:
if(output is high){
 do nothing;
}
else{
make switch variable high or low depending how many times switch is pressed;
}
clock_high=1;
asm("nop")
clock_low=1;
Now if switch is pressed continuously then always 'else' condition will be executed.So i was thinking of incrementing a variable continuously till the key is pressed. And it will stop incrementing when key is released. Now by checking the status of the variable i.e whether variable has stopped incrementing or not i can change status of another variable to make switch variable status 'OFF'. Else what happens is if i increment the variable for particular count for ON and OFF status, LED for particular switch will show continue blinking i.e ON and OFF. Which is not feasible. It should not matter that for how much duration switch is pressed. If pressed once LED should be ON and if pressed next time LED should be OFF.
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…