You can save a lot of repeated code by putting the switch pin numbers in an array instead of having one name per pin.
Are you sure you don't want to eliminate those eight external pull-down resistors by using pinMode(INPUT_PULLUP)? You would wire the switches between the switch pin and Ground. To do that, change:
pinMode (DigitalInSwitches[i], INPUT);
to:
pinMode (DigitalInSwitches[i], INPUT_PULLUP);
and change:
if (digitalRead (DigitalInSwitches[i]) == HIGH)
to:
if (digitalRead (DigitalInSwitches[i]) == LOW)
/* ======================================================================
Arduino Punk Console
A simple programmable 8 step tone sequencer
by dano/beavisaudio.com
Revs
-----------------------------------
15 Sept djh initial version
======================================================================*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// Map all the input and output pins
const byte AnalogInDuration = A0;
const byte AnalogInFrequency = A1;
const byte AnalogInTempo = A2;
const byte DigitalInSwitches[] = {2, 3, 4, 5, 6, 7, 8, 9};
const byte DigitalInStartStop = 10;
const byte DigitalOutSignal = 11;
const byte DigitalOutLED = 12;
// Set up the array for pitch at each step
int steps[] = {100, 120, 140, 160, 180, 200, 220, 240};
// misc housekeeping
int duration = 50;
int pitchval = 1;
int fPlayMode = true;
int lastPushedStep = -1;
// Initialize the tempo
int tempo = 100;
void setup()
{
// setup pin modes (Digital pins are input by default, but
// I like to set 'em explicitly just so the code is clear.
for (size_t i = 0; i < 8; i++)
pinMode (DigitalInSwitches[i], INPUT);
pinMode (DigitalInStartStop, INPUT);
pinMode (DigitalOutSignal, OUTPUT);
pinMode (DigitalOutLED, OUTPUT);
// setup comms for the LCD display
Serial.begin(9600);
StartupMessage();
}
void StartupMessage()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print ("BEAVIS: Arduino");
delay(300);
Serial.write (254);
Serial.write (192);
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print ("Punk Console!");
delay (2000);
lcd.init();
lcd.print ("Beavis: APC");
}
void clearLCD()
{
lcd.init(); // initialize the lcd
Serial.write(254);
Serial.write(1);
}
void loop()
{
// Main sequence loop
for (int i = 0; i < 8; i++)
{
// Are we playing or stopping?
fPlayMode = digitalRead (DigitalInStartStop);
digitalWrite (DigitalOutLED, HIGH);
// Check the Hardware
readSwitches();
readPots();
// update the display
updateDisplay();
// Make the noise
if (fPlayMode)
{
freqout (steps[i], duration);
}
digitalWrite (DigitalOutLED, LOW);
// Pause between steps
delay (tempo);
}
}
void updateDisplay()
{
lcd.init(); // initialize the lcd
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print ("Beavis: APC");
Serial.write (254);
Serial.write (192);
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print ("T:");
lcd.print (tempo);
lcd.print (" d:");
lcd.print (duration);
if (lastPushedStep != -1)
{
lcd.print ("*");
lcd.print (lastPushedStep);
}
}
// Read the current values of the pots, called from the loop.
void readPots ()
{
tempo = (analogRead (AnalogInTempo) * 1.9);
duration = (analogRead (AnalogInDuration));
}
// Read the current values of the switches and
// if pressed, replace the switch's slot frequency
// by reading the frequency pot.
void readSwitches()
{
// reset last pushed button number
lastPushedStep = -1;
for (size_t i = 0; i < 8; i++)
{
// check switch 0, if pressed, get the current freq into step 0, etc. etc.
if (digitalRead (DigitalInSwitches[i]) == HIGH)
{
steps[i] = analogRead(AnalogInFrequency);
lastPushedStep = i + 1;
}
}
}
//freqout code by Paul Badger
// freq - frequency value
// t - time duration of tone
void freqout(int freq, int t)
{
int hperiod; //calculate 1/2 period in us
long cycles;
// subtract 7 us to make up for digitalWrite overhead - determined empirically
hperiod = (500000 / ((freq - 7) * pitchval));
// calculate cycles
cycles = ((long)freq * (long)t) / 1000; // calculate cycles
for (long i = 0; i <= cycles; i++)
{
// play note for t ms
digitalWrite(DigitalOutSignal, HIGH);
delayMicroseconds(hperiod);
digitalWrite(DigitalOutSignal, LOW);
delayMicroseconds(hperiod - 1); // - 1 to make up for fractional microsecond in digitaWrite overhead
}
}