GPIO

Reference functions

  1. pinMode()
  2. digitalRead()
  3. digitalWrite()
  4. pulseIn()

Sample

Turn on or off LED by pressing and releasing the button connected to the GPIO.

Connect

Sample code

int led_pin = 5;
/*
* Button LED test program
*/

#include <core.h>

int led_pin = 13;
int btn_pin = 6;

void setup() {
    if ( argc == 3 ) {
        btn_pin = atoi(argv[1]);
        led_pin = atoi(argv[2]);
    }
//set the gpio to input or output mode
    pinMode(led_pin, OUTPUT);
    pinMode(btn_pin, INPUT);
}

void loop() {
//press btn_pin to turn on LED
    int value = digitalRead(btn_pin);
    if ( value == HIGH )
    { // button pressed
        digitalWrite(led_pin, HIGH); // turn on LED
    } else { // button released
        digitalWrite(led_pin, LOW); // turn off LED
    }

    delay(100);
}

results matching ""

    No results matching ""