Skip to content

Instantly share code, notes, and snippets.

@equaliser
Created March 5, 2012 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equaliser/1981269 to your computer and use it in GitHub Desktop.
Save equaliser/1981269 to your computer and use it in GitHub Desktop.
Simple voltage controlled clock with pulse width
/*******************************************************/
/* 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