Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ============================================================
//
// Program: ArdCore Rotating Pattern Emitter
//
// Description: Given a clock, output two patterns based on divisions
// of the clock. These patterns are preset but can be
// rotated with knobs.
//
// I/O Usage:
// Knob 1: Pattern 1 select
// Knob 2: Pattern 2 select
// Knob 3: Pattern 1 rotate
// Knob 4: Pattern 2 rotate
// Digital Out 1: Clock pattern stream 1
// Digital Out 2: Clock pattern stream 2
// Clock In: External clock
// Analog Out: unused
//
// Input Expander: unused
// Output Expander: unused
//
// Created: 7 July 2013
// Derived from some code provided by 20 Objects - http://20objects.com/
// constants related to the Arduino Nano pin use
const int pinOffset = 5; // the first DAC pin (from 5-12)
const int clkIn = 2; // the digital (clock) input
const int digPin[2] = {3, 4}; // the digital output pins
// variables for interrupt handling of the clock input
volatile int clkState = LOW;
// Patterns
const int NUM_PATTERNS = 6;
const int PATTERN_LENGTH = 4;
const int TRIGGER_LENGTH = 25;
int patterns[NUM_PATTERNS][PATTERN_LENGTH] = {
{ 0, 0, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 0, 1, 0 },
{ 1, 1, 0, 0 },
{ 1, 1, 1, 0 },
{ 1, 1, 1, 1 }
};
int pattern1Counter = 0;
int pattern2Counter = 0;
// Setup
void setup()
{
// set up the digital (clock) input
pinMode(clkIn, INPUT);
// set up the digital outputs
for (int i=0; i<2; i++) {
pinMode(digPin[i], OUTPUT);
digitalWrite(digPin[i], LOW);
}
// set up the 8-bit DAC output pins
for (int i=0; i<8; i++) {
pinMode(pinOffset+i, OUTPUT);
digitalWrite(pinOffset+i, LOW);
}
// set up an interrupt handler for the clock in. If you
// aren't going to use clock input, you should probably
// comment out this call.
// Note: Interrupt 0 is for pin 2 (clkIn)
attachInterrupt(0, isr, RISING);
}
void loop() {
if(clkState == HIGH) {
clkState = LOW;
// Select patterns from knobs
int pattern1Index = analogRead(0) / (1024/(NUM_PATTERNS-1));
int pattern2Index = analogRead(1) / (1024/(NUM_PATTERNS-1));
// Rotate patterns from knobs
int pattern1Rotate = analogRead(2) / (1024/(PATTERN_LENGTH-1));
int pattern2Rotate = analogRead(3) / (1024/(PATTERN_LENGTH-1));
// Fire off current pattern value
int pattern1Value = patterns[pattern1Index][(pattern1Counter + pattern1Rotate) % PATTERN_LENGTH];
if(pattern1Value) {
digitalWrite(digPin[0], HIGH);
}
int pattern2Value = patterns[pattern2Index][(pattern2Counter + pattern2Rotate) % PATTERN_LENGTH];
if(pattern2Value) {
digitalWrite(digPin[1], HIGH);
}
pattern1Counter = (pattern1Counter+1) % PATTERN_LENGTH;
pattern2Counter = (pattern2Counter+1) % PATTERN_LENGTH;
delay(TRIGGER_LENGTH);
digitalWrite(digPin[0], LOW);
digitalWrite(digPin[1], LOW);
}
}
// isr() - quickly handle interrupts from the clock input
void isr()
{
clkState = HIGH;
}
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.