Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make pretty lights using a Raspberry Pi and a 74374N octal flip-flop
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define INPUT 17
#define OE 18
#define CLK 27
void setup_pin(int pin) {
FILE *f;
char buf[512] = {0};
f = fopen("/sys/class/gpio/export", "w");
fprintf(f, "%d\n", pin);
fclose(f);
sprintf(buf, "/sys/class/gpio/gpio%d/direction", pin);
f = fopen(buf, "w");
fprintf(f, "out\n");
fclose(f);
}
void set_pin(int pin, int value) {
FILE *f;
char buf[512] = {0};
sprintf(buf, "/sys/class/gpio/gpio%d/value", pin);
f = fopen(buf, "w");
fprintf(f, "%d\n", value);
fclose(f);
}
int main(void) {
setup_pin(CLK);
setup_pin(OE);
setup_pin(INPUT);
set_pin(OE, 0);
while(1) {
for(int i = 0; i < 8; i++) {
set_pin(CLK, 0);
set_pin(INPUT, random() & 1);
set_pin(CLK, 1);
}
sleep(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.