Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic program for your micro:bit to control LED and read push button state using Arduino IDE.
const int COL1 = 3; // Column #1 control
const int LED = 26; // 'row 1' led
const int BUTTON_A = 5; // The number of the pushbutton pin
const int BUTTON_B = 11; // The number of the pushbutton pin
long previousMillis = 0;
long currentMillis = 0;
int interval = 500;
boolean ledState = false;
boolean buttonAPressed = false;
boolean buttonBPressed = false;
void setup()
{
Serial.begin(9600);
Serial.println("microbit is ready!");
pinMode(BUTTON_A, INPUT);
pinMode(BUTTON_B, INPUT);
// Because the LEDs are multiplexed,
// we must ground the opposite side of the LED
pinMode(COL1, OUTPUT);
digitalWrite(COL1, LOW);
pinMode(LED, OUTPUT);
}
void loop()
{
currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
ledState = !ledState;
digitalWrite(LED, ledState);
}
if (digitalRead(BUTTON_A) == LOW &&
buttonAPressed == false) {
buttonAPressed = true;
Serial.println("Button A is pressed.");
}
else if (digitalRead(BUTTON_A) == HIGH &&
buttonAPressed == true) {
buttonAPressed = false;
}
if (digitalRead(BUTTON_B) == LOW &&
buttonBPressed == false) {
buttonBPressed = true;
Serial.println("Button B is pressed.");
}
else if (digitalRead(BUTTON_B) == HIGH &&
buttonBPressed == true) {
buttonBPressed = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment