Pages: [1]   Go Down
Author Topic: Special button on Olimex Leonardo. How to use it?  (Read 362 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Full Member
***
Karma: 1
Posts: 138
likes to program Arduinos!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi!
I just got an Olimex Leonardo (Olimexino-32u4) and this board got a button connected to the HWB pin on the atmega32u4 (pin 33).
This button is not defined int the pins_arduino.h so it have to be defined manually. Olimex have made a sketch that show how to get the buttun working. The problem is that I don't understand it, AT ALL!
Can someone please explain the code to me?

I don't understand the two "#define" parts, and the part including "^1"


Link to the board: https://www.olimex.com/Products/Duino/AVR/OLIMEXINO-32U4/

Code:
/* **************************************
OLIMEXINO-32U4 Button Example

Pressing the hardware button toggles
the LEDs on the board.

Note that it is recommended to set the power jumper
to 5V supply.

If you have any questions, email
support@olimex.com

https://www.olimex.com
OLIMEX, Jan 2013

   ************************************** */
   
/* ******** Definitions ***************** */

// We define the button here, because the original Leonardo
// board doesn't have a button by default and there is no
// pin definition for port E2 in the pins_arduino.h header

#define BBIT (PINE & B00000100)!=0 // Check if the button has been pressed
#define BUTTONINPUT DDRE &= B11111011 // Initialize the port

// Useful definitions for the two LEDs on the board
#define GLED 7 // The GREEN  LED is on Pin 7
#define YLED 9 // The YELLOW LED is on Pin 9

/* ******** Main Body ******************* */

void setup(){

// Initialize the Button and LEDs
BUTTONINPUT;

pinMode(GLED, OUTPUT);
pinMode(YLED, OUTPUT);

// Turn ON the LEDs to indicate that the program has started running
digitalWrite(YLED, HIGH);
digitalWrite(GLED, HIGH);

}

void loop(){

// Prssing the hardware button repeatedly will toggle the LEDs

while(BBIT){}
digitalWrite(GLED, digitalRead(GLED)^1); //Toggle the Green LED

delay(500); //Wait for a bit or the button check won't be accurate

while(BBIT){}
digitalWrite(YLED, digitalRead(YLED)^1); //Toggle the Yellow LED

delay(500); //Wait for a bit or the button check won't be accurate
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define BBIT (PINE & B00000100)!=0 // Check if the button has been pressed
#define BUTTONINPUT DDRE &= B11111011 // Initialize the port
These two? The PINX and DDRX names are used for collections of pins that make up a port. The switch in question is connected to one of the pins that make up port E. The & is a bit operator that you should look up on the reference page, and work through understanding what it is doing. Notice that the bit patterns are completely opposite. Each masks (hides) everything except the pin of interest. The difference is how they are used. The first says that we are concerned only about the state of one pin. The other says to do something to every pin except the pin of interest.

The ^ is also a bit operator. Again, you should consult the reference page to understand this operator. The comment, though, describes exactly what is happening.
Logged

Pages: [1]   Go Up
Jump to: