Hackster is hosting Impact Spotlights: Industrial Automation. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Industrial Automation. Stream on Thursday!
Monica Houston
Created December 1, 2015 © CC BY-SA

Blink an LED with ARTIK 5

Quickstart guide to Samsung's ARTIK 5

IntermediateProtip4 hours78
Blink an LED with ARTIK 5

Things used in this project

Hardware components

ARTIK 5
Samsung ARTIK 5
×1

Story

Read more

Code

Blink.c

C/C++
Upload this to the Artik, compile with gcc, and run!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
 
#define HIGH 1
#define LOW 0
#define INPUT 1
#define OUTPUT 0

int outputPin = 135;

 bool digitalPinMode(int pin, int dir){
  FILE * fd;
  char fName[128];

  // Exporting the pin to be used
  if(( fd = fopen("/sys/class/gpio/export", "w")) == NULL) {
    printf("Error: unable to export pin\n");
    return false;
  }
  fprintf(fd, "%d\n", pin);
  fclose(fd);

  // Setting direction of the pin
  sprintf(fName, "/sys/class/gpio/gpio%d/direction", pin);
  if((fd = fopen(fName, "w")) == NULL) {
    printf("Error: can't open pin direction\n");
    return false;
  }
  if(dir == OUTPUT) {
    fprintf(fd, "out\n");
  } else {
    fprintf(fd, "in\n");
  }
  fclose(fd);

  return true;
}

bool digitalWrite(int pin, int val) {
  FILE * fd;
  char fName[128];

  // Open pin value file
  sprintf(fName, "/sys/class/gpio/gpio%d/value", pin);
  if((fd = fopen(fName, "w")) == NULL) {
    printf("Error: can't open pin value\n");
    return false;
  }
  if(val == HIGH) {
    fprintf(fd, "1\n");
  } else {
    fprintf(fd, "0\n");
  }
  fclose(fd);

  return true;
}

int setup() {
   if (!digitalPinMode(outputPin, OUTPUT))
     return -1;
   return 0;
}

int main(void) {
  if (setup() == -1)
  {
    exit(1);
  }
	
  while(1){
    digitalWrite(outputPin, HIGH);
    sleep(1);
    digitalWrite(outputPin, LOW);
    sleep(1);
  }

  return 0;
}

Credits

Monica Houston
80 projects • 463 followers
I don't live on a boat anymore.
Contact

Comments

Please log in or sign up to comment.