Created
March 5, 2012 21:38
-
-
Save equaliser/1981269 to your computer and use it in GitHub Desktop.
Simple voltage controlled clock with pulse width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*******************************************************/ | |
/* Dead simple voltage controlled clock | |
/* adjustable pulse width | |
/* | |
/* intended for use as a master clock for a sequencer | |
/* designed for Arduino Duemilanove (Atmega 168) | |
/* you might need to change the port numbers for | |
/* Arduino Mega/other Arduinos | |
/* | |
/* Steve Woodward, Mar 2012 | |
/* | |
/* | |
/* speed = analogue in pin 0 | |
/* pulse width = analogue in pin 1 | |
/* | |
/* uses the timerone library | |
/* http://arduino.cc/playground/Code/Timer1 | |
/*******************************************************/ | |
#include <TimerOne.h> | |
#define CLOCK_OUT 5 // this is pin 13, (bit 5 on PORTB) | |
volatile int counter = 0; | |
volatile int countTime = 128; | |
void setup() | |
{ | |
pinMode(13, OUTPUT); | |
Timer1.initialize(1000); // timer of 1 millisecond | |
Timer1.attachInterrupt( timerIsr ); | |
} | |
void loop() | |
{ | |
countTime=((analogRead(0) >> 2) << 2) +50; // reducing accuracy, trying to avoid jitter | |
int pulseRead = analogRead(1) >> 3; | |
int pulseTime = map(pulseRead, 0, 127, 0, countTime); | |
if(counter>=pulseTime) { | |
PORTB &= ~(1 << CLOCK_OUT); | |
} | |
} | |
void timerIsr() | |
{ | |
counter++; | |
int out; | |
if(counter>=countTime) { | |
PORTB |= 1 << CLOCK_OUT; | |
counter=0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment