🎨
MadMachine-Examples
  • Introduction
  • About our project
  • Getting Started
  • Tutorial
    • What is SwiftIO
    • How to use IDE
    • Libraries
    • MM SDK
  • Built-In Examples
    • GettingStarted
      • Blink
      • ReadDigitalInput
      • ReadAnalogInput
      • PWMBrightnessControl
    • SimpleIO
      • ButtoncontrolLED
      • BlinkAnalogIn
      • BlinkTimer
      • BrightnessAnalogIn
      • Debounce
      • LEDsBrightnessControl
      • PWMSoundOutput
      • PWMMelody
    • AdvancedIO
      • ILI9341
      • LCD1602
      • MidiPlayer
      • PCA9685
      • SHT3x
      • SSD1315
    • Challenge
      • LCDFamily
      • RobotArm
      • Tetris
    • MakerKit
      • Mission 01 Blink
      • Mission 02 RGB LED
      • Mission 03 Push Button
      • Mission 04 Potentiometer RGB
      • Mission 05 Buzzer
      • Mission 06 Seven Segment Display
      • Mission 07 DC Motor
      • Mission 08 Servo Motor
      • Mission 09 LCD
      • Mission 10 Humiture Sensor
  • Library Reference
  • FAQ
Powered by GitBook
On this page
  • What you need
  • Kits that meet the experimental conditions:
  • Circuit
  • Code
  • Instruction
  • See Also

Was this helpful?

  1. Built-In Examples
  2. SimpleIO

ButtoncontrolLED

PreviousSimpleIONextBlinkAnalogIn

Last updated 4 years ago

Was this helpful?

In this example, we will use a push-button to control the LED.

The input signal will change as you press the button. Thus, you can set LED status according to different input states.

What you need

  • SwiftIO board

  • Button

  • Jumper wires

Kits that meet the experimental conditions:

Circuit

There is an onboard RGB LED. Please apply low voltage to light it.

The button has four legs. The two legs on same side are interconnected.

  • Connect the leg on left side to 3.3V pin.

  • Connect the leg on right side to digital pin D10.

In default mode, the digital pin reads false. When you press the button, the two points on the button will be connected. And the value of pin will be true.

So please be sure you connected the button in a right way.

Code

// Read the input signal controlled by a button to turn on and off the LED.

// Import the library to enable everything in it, like relevant classes and methods. 
// This is first step for your coding process.
import SwiftIO

// Declare a constant. You may choose any descriptive name you like. 
// Initialize the onboard red LED. 
// The Id of onboard LED should be capitalized.
let red = DigitalOut(Id.RED)

// Initialize a digital input pin D10 the button is connected to.
let button = DigitalIn(Id.D10)

// Allow the button to control the LED all the time.
while true {
    // Check the state of button. 
    // If it is pressed, the value will be true and then turn off the LED.
    // Modify the code according to your button if necessary.
    if button.read() {
        red.write(false)
    } else {
        red.write(true)
    }
}

Instruction

DigitalIn class is intended to detect the state of a digital input pin. The input value is either true(1) or false(0). The .read() function reads the value from a digital input pin.

If you have the experience with Arduino, you may notice there's no pull-down resistor on the button. That's because the SwiftIO Board already provides a pull-down function. Reference the DigitalIn class for more information.

See Also

​​

Here comes the code. You can find the example code at the bottom left corner of IDE: > SimpleIO > ButtoncontrolLED.

- Enumerations of all the pins on the board.

- Detect the state of a digital input pin. The input value is either true (1) or false (0).

Maker Kit for SwiftIO
Id
DigitalIn