Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define BUTTON_PIN "P8_11"
- #define LED_PIN "P8_13"
- int main(int argc, char *argv[]) {
- // Export the button and LED pins
- system("echo " BUTTON_PIN " > /sys/class/gpio/export");
- system("echo " LED_PIN " > /sys/class/gpio/export");
- // Set the button pin as an input
- system("echo in > /sys/class/gpio/gpio" BUTTON_PIN "/direction");
- // Set the LED pin as an output
- system("echo out > /sys/class/gpio/gpio" LED_PIN "/direction");
- while (1) {
- // Read the button state
- FILE *button_file = fopen("/sys/class/gpio/gpio" BUTTON_PIN "/value", "r");
- int button_state = fgetc(button_file) - '0';
- fclose(button_file);
- // If the button is pressed, turn on the LED
- if (button_state == 0) {
- system("echo 1 > /sys/class/gpio/gpio" LED_PIN "/value");
- } else {
- system("echo 0 > /sys/class/gpio/gpio" LED_PIN "/value");
- }
- // Wait for a bit before reading the button state again
- usleep(100000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment