/*
* Pico minicodes: Richard 07/15/2024
* Cascading LED Bar Graph [KnightRider]
*/
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include <stdio.h>
// Define the GPIO pins for the LED bar graph
#define LED_COUNT 10
uint led_pins[LED_COUNT] = {0, 1, 2, 3, 4,
5, 6, 7, 8, 9};
void setup();
void knight_rider();
void first_half();
void second_half();
int main() {
stdio_init_all();
sleep_ms(1000);
printf("KnightRider cascading LEDs...\n");
sleep_ms(4000);
setup();
while (true) {
knight_rider();
sleep_ms(170);
//first_half();
//sleep_ms(1000);
//second_half();
//sleep_ms(1000);
}
return 0;
}
void setup() {
// Initialize the LED pins as output
for (int i = 0; i < LED_COUNT; i++) {
gpio_init(led_pins[i]);
gpio_set_dir(led_pins[i], GPIO_OUT);
}
}
void knight_rider() {
// 1 forward, -1 reverse
static int direction = 1;
static int position = 0;
// Turn off all LEDs
for (int i = 0; i < LED_COUNT; i++) {
gpio_put(led_pins[i], 0);
}
// Turn on the current LED
gpio_put(led_pins[position], 1);
// Update the position
position += direction;
if (position == LED_COUNT - 1 || position == 0) {
// Change direction at the ends
direction = -direction;
}
}
void first_half() {
// light first 5 LEDs and turn off last 5
for (int i = 0; i < LED_COUNT; i++) {
if (i < 5) {
gpio_put(led_pins[i], 1);
} else {
gpio_put(led_pins[i], 0);
}
}
}
void second_half() {
for (int i = 0; i < LED_COUNT; i++) {
if (i >= 5) {
gpio_put(led_pins[i], 1);
} else {
gpio_put(led_pins[i], 0);
}
}
}