Detect When Power Button Is Hit

Get help and show off your TV-B-Gone kit!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

Detect When Power Button Is Hit

Post by Tanner8 »

Well, I am trying to make it so that if you hit the power button it will stop scanning if it already is. I wrote the code out and stuff and I thought it would work but then I realized that everytime you hit the powerbutton it just starts the WHOLE program over. I don't really program C, I am a Java programmer so I may be wrong. But the way it looks it that each time I click the button the program starts again causing variables to go down the drain. I looked through the source and can't find anywhere that it knows if the button was clicked. I sort of want to have that code in the main code scanning loop to kick it out if needed. Such as

if(buttonIsPressed)
break;

Since I couldn't figure that out I was doing the work with a variable but like I said, it gets wiped on reclick.

Once again, this is like my 3rd time doing C so things are a little shaky. Thanks to Mitches superfluous comments I can understand what is going on easier.

User avatar
caitsith2
 
Posts: 217
Joined: Thu Jan 18, 2007 11:21 pm

Post by caitsith2 »

There is a way to define a variable, so that it doesn't go down the drain, on reset.

Code: Select all

static volatile uint8_t KeepTrack __attribute__ ((section (".noinit")));
This tells the C compiler, and the initialization code, that the ram that this variable resides in, is to be left at whatever value it is. (The chip does NOT reset the ram to 0 on code reset, its the init code, whether it be C's crt0 or an assembler routine that does that.)

For this example, you would in your main at start, check to see if the variable is set to 0. (when it is done its loop.) If the variable is not 0, then it will set it to 0, and go to sleep right away. If it is 0, it will go into the loop,which will set it non 0. When the loop is finished, then the variable will be set 0, so that the loop can be started once again.

This type of code will work on both V1.0 and V1.1 hardware.

Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

Post by Tanner8 »

Ok thanks. I can't figure this out, now all I get are compile errors or it just doesnt work at all...

How could I define an int to a value of 1 and add 1 to it. Like this...

int count=1;//Pretend This Is The Permenant Var
count++;

Now how the hell do you do that using a perminent variable? I just keep getting compile errors when I try the simple stuff.

I even tried making another variable and setting it equal to the one that cant change but that just give me even more errors...

int count=1;//Pretend This Is The Permenant Var
int count2;
count2=count;

User avatar
darus67
 
Posts: 246
Joined: Wed Sep 26, 2007 10:25 pm

Post by darus67 »

If you post the exact compile error, and the code it is complaining about, we could provide more help.

caladan
 
Posts: 19
Joined: Mon Dec 17, 2007 12:16 pm

Post by caladan »

Where do you declare that variable?
In or before main()?
I had some problems when variables had set initializers, so you have to init them in main, by hand.

Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

Post by Tanner8 »

All I need to know is how to use this piece of code.

Say I want to set the variable "test" to -1. How would I do that? Then say I wanted to add 1 to the variable to make it 0. How would I do that? All without resetting test back to -1 when the code is re-run.

Here is exactly what I want to do....

Set a variable to -1 when the button is hit the first time. Then if it is hit again it will bump 0 to 1(The bump will be below the set so it will make '-1', '0' instantly). Have the code scan in an if statment that is if(test==0){scan}. So if it is hit again it will bump to 1 which would make it just go to the shutdown statements. In the shutdown statements it will also set the variable 'test' back to -1. Then if the button is hit again it will start with test at -1.

Ask me if it is really confusing.

I just don't know how to go about doing this without setting the variable back to '-1' everytime the button is hit.

Thanks

caladan
 
Posts: 19
Joined: Mon Dec 17, 2007 12:16 pm

Post by caladan »

I think you should change ISR (interrupt service routine), so it can check if the scanning is in progress (there's one variable that iterates thru all codes in device), and if it is, set it to the last value,so it ends blinking and goes t sleep.

Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

Post by Tanner8 »

Got it working!

Using the variables seems weird... It's weird how you never define the variable but yet you can ++ it... And if you don't add to it in a method it gives you a compile error...Strange stuff but I guess I gotta get used to it.

Thanks guys

Locked
Please be positive and constructive with your questions and comments.

Return to “TV-B-Gone Kit”