Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created July 13, 2013 22:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jakevsrobots/5992509 to your computer and use it in GitHub Desktop.
A sketch for the Ardcore module.
// ============================================================
//
// Program: ArdCore Rotating Pattern Emitter (multi-length)
//
// 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: 13 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_1 = 12;
const int NUM_PATTERNS_2 = 15;
const int TRIGGER_LENGTH = 25;
String patterns1[NUM_PATTERNS_1] = {
"0000",
"1000",
"1010",
"1110",
"1111",
"10000000",
"10100000",
"10010000",
"11001000",
"11001100",
"11110110",
"11111110"
};
String patterns2[NUM_PATTERNS_2] = {
"000",
"100",
"110",
"111",
"10000",
"11000",
"10100",
"11100",
"11110",
"100000",
"110000",
"101000",
"111000",
"111010",
"111110"
};
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-1));
int pattern2Index = analogRead(1) / (1024/(NUM_PATTERNS_2-1));
int pattern1Length = patterns1[pattern1Index].length();
int pattern2Length = patterns2[pattern2Index].length();
// Rotate patterns from knobs
int pattern1Rotate = analogRead(2) / (1024/(pattern1Length-1));
int pattern2Rotate = analogRead(3) / (1024/(pattern2Length-1));
// Fire off current pattern value
char pattern1Value = patterns1[pattern1Index][(pattern1Counter + pattern1Rotate) % pattern1Length];
if(pattern1Value == '1') {
digitalWrite(digPin[0], HIGH);
}
char pattern2Value = patterns2[pattern2Index][(pattern2Counter + pattern2Rotate) % pattern2Length];
if(pattern2Value == '1') {
digitalWrite(digPin[1], HIGH);
}
pattern1Counter = (pattern1Counter+1) % pattern1Length;
pattern2Counter = (pattern2Counter+1) % pattern2Length;
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