9 years, 1 month ago.

FRDM K64F: a simple button exercise

Hi everyone here, I'm new here. I'm using a FRDM K64F for university purouses and I'm trying to learn a bit more about that by doing some simple exercises I've found in the Sunfounder Uno Basic Arduino Kit. The code for the two controllers is different, so I just want to check whether I've done the right things.

The purpose of the exercise is to just create a circuit that turns on the LED of the board whenver a button is pressed. What I've done is the following... The problem is that the button stays on by default and turns off when I push the button, any idea why (using the code below) that is the result?

include the mbed library with this snippet

#include "mbed.h"

DigitalOut led(LED_BLUE);
DigitalIn input(D12);


int main ()
{
void loop ();
    {
        if(input==1)
        {
            led=1;
            }
        else
        {
            led=0;
            }
        }
    }

Also, I'm not a programmer, so feel free to correct whatever mistake you see in the coding :P

Thanks for the help :)

2 Answers

9 years, 1 month ago.

I'd start reading about mbed | InterruptIn. In general you don't want to write code that busy loops like you do on Arduino. Here's an example of how to toggle the LED whenever you press a button:

#include "mbed.h"

InterruptIn btn(D12);
DigitalOut led(LED_BLUE);

void button_pressed() {
  led = !led; // toggle the LED
}

int main(int, char**) {
  btn.fall(&button_pressed); // whenever the button falls, execute button_pressed function

  while (1) {}
}

Accepted Answer

Thank you very much. I'm definitely checking the material you've linked :)

posted by Nicolò Bagarin 11 Aug 2016
9 years, 1 month ago.

It's a consequence of how the LED is wired: between pin and GND or (in the case of the K64F on-board LEDs) between 3V3 and the pin. So for these LEDs, logic 0 turns the LED on and logic 1 turns it off.

Thanks Colin. I didn't properly read the question before answering ;-).

posted by Jan Jongboom 11 Aug 2016

Perfect! That solves the problem :) Thank you!

posted by Nicolò Bagarin 11 Aug 2016