0

Detect When Power Button Is Hit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Detect When Power Button Is Hit

by Tanner8 on Tue Dec 25, 2007 1:12 am

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.
Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

by caitsith2 on Tue Dec 25, 2007 2:35 am

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

Code: Select all | TOGGLE FULL SIZE
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.
caitsith2
 
Posts: 217
Joined: Thu Jan 18, 2007 11:21 pm

by Tanner8 on Tue Dec 25, 2007 2:12 pm

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;
Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

by darus67 on Wed Jan 02, 2008 3:08 pm

If you post the exact compile error, and the code it is complaining about, we could provide more help.
"He's just this guy. You know?"
User avatar
darus67
 
Posts: 246
Joined: Wed Sep 26, 2007 10:25 pm
Location: Minnesota, USA

by caladan on Wed Jan 02, 2008 3:26 pm

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.
caladan
 
Posts: 19
Joined: Mon Dec 17, 2007 12:16 pm

by Tanner8 on Thu Jan 03, 2008 5:00 pm

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
Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

by caladan on Fri Jan 04, 2008 4:45 am

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.
caladan
 
Posts: 19
Joined: Mon Dec 17, 2007 12:16 pm

by Tanner8 on Sat Jan 05, 2008 2:24 am

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
Tanner8
 
Posts: 26
Joined: Mon Dec 17, 2007 9:27 pm

Please be positive and constructive with your questions and comments.