Skip to content
Snippets Groups Projects
Commit 14237409 authored by Malte Schmitz's avatar Malte Schmitz
Browse files

Add I/O and scheduler

parent b482a17d
No related branches found
No related tags found
No related merge requests found
// Simple NeoPixel test. Lights just a few pixels at a time so a
// 1m strip can safely be powered from Arduino 5V pin. Arduino
// may nonetheless hiccup when LEDs are first connected and not
// accept code. So upload code first, unplug USB, connect pixels
// to GND FIRST, then +5V and digital pin 6, then re-plug USB.
// A working strip will show a few pixels moving down the line,
// cycling between red, green and blue. If you get no response,
// might be connected to wrong end of strip (the end wires, if
// any, are no indication -- look instead for the data direction
// arrows printed on the strip).
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define N_LEDS 144
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
}
static void chase(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels()+4; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i-4, 0); // Erase pixel a few steps back
strip.show();
delay(25);
}
}
void loop() {
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 0, 255)); // Blue
}
\ No newline at end of file
......@@ -15,5 +15,6 @@ framework = arduino
lib_deps =
Adafruit NeoPixel
TaskScheduler
upload_port = /dev/cu.wchusbserial1420
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <TaskScheduler.h>
#define NEOPIXEL_PIN 6
#define NEOPIXEL_LEN 105
// Pin A and Pin B are connected if the button is pressed.
// Pin A and Pin No are connected if no external button is connected.
#define EXTERNAL_BUTTON_PIN_A 7
#define EXTERNAL_BUTTON_PIN_B 9
#define EXTERNAL_BUTTON_PIN_NO 8
#define INTERNAL_BUTTON_PIN_A 10
#define INTERNAL_BUTTON_PIN_B 11
Adafruit_NeoPixel pixels(NEOPIXEL_LEN, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
Scheduler scheduler;
void handleButton();
Task taskButton(TASK_IMMEDIATE, TASK_FOREVER, &handleButton);
void handleStep();
Task taskStep(25, TASK_FOREVER, &handleStep);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(EXTERNAL_BUTTON_PIN_A, OUTPUT);
digitalWrite(EXTERNAL_BUTTON_PIN_A, LOW);
pinMode(EXTERNAL_BUTTON_PIN_B, INPUT_PULLUP);
pinMode(EXTERNAL_BUTTON_PIN_NO, INPUT_PULLUP);
pinMode(INTERNAL_BUTTON_PIN_A, OUTPUT);
digitalWrite(INTERNAL_BUTTON_PIN_A, LOW);
pinMode(INTERNAL_BUTTON_PIN_B, INPUT_PULLUP);
pixels.begin();
scheduler.init();
scheduler.addTask(taskButton);
taskButton.enable();
scheduler.addTask(taskStep);
taskStep.enable();
}
int pos = 0;
void loop() {
scheduler.execute();
}
uint32_t color = 0xFFFFFF;
int position = 0;
void show() {
for (int i = 0; i < NEOPIXEL_LEN; i++) {
pixels.setPixelColor(i, 0xFFFFFF);
pixels.setPixelColor(i, 0);
}
for (int i = position; i < position + 10; i++) {
pixels.setPixelColor(i, color);
}
pixels.setPixelColor(pos, 0xFF0000);
pixels.show();
pos = (pos + 1) % NEOPIXEL_LEN;
}
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
void handleButton() {
if (digitalRead(INTERNAL_BUTTON_PIN_B) == LOW) {
color = 0x0000FF;
} else if (digitalRead(EXTERNAL_BUTTON_PIN_NO) == LOW) {
color = 0xFF0000;
} else if (digitalRead(EXTERNAL_BUTTON_PIN_B) == LOW) {
color = 0x00FF00;
}
show();
}
boolean forward = true;
void handleStep() {
if (forward) {
position = position + 1;
if (position > NEOPIXEL_LEN - 11) {
position = position - 2;
forward = false;
}
} else {
position = position - 1;
if (position < 0) {
position = 1;
forward = true;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment