Arduino-Pedal-Effects/c/New Effects PedalSHIELD UNO/Chorus/Chorus.ino
2022-07-18 18:23:07 +02:00

122 lines
3.5 KiB
C++

/* Based on the "CHORUS for Stompshield" adapted from pedal shield, and reconverted for pedalSHIELD UNO*/
//defining harware resources.
#define LED 13
#define FOOTSWITCH 12
#define TOGGLE 2
#define PUSHBUTTON_1 A5
#define PUSHBUTTON_2 A4
//defining the output PWM parameters
#define PWM_FREQ 0x00FF // pwm frequency - 31.3KHz
#define PWM_MODE 0 // Fast (1) or Phase Correct (0)
#define PWM_QTY 2 // 2 PWMs in parallel
//defining buttons parameters
#define B_MAX 100 // max value for rotary encoder input (min freq)
#define B_MIN 1 // min value for rotary encoder input (max freq)
//defining FX parameters
#define MAX_DELAY 240
#define MIN_DELAY 200
uint16_t sDelayBuffer0[MAX_DELAY*2];
uint16_t sDelayBuffer1[MAX_DELAY*2];
unsigned int DelayCounter = 5;
unsigned int Delay_Depth = 25;
unsigned int count_up=1;
int p;
int POT0 = 1;
unsigned int location = 0; // incoming data buffer pointer
byte button; // button checking timer
byte last_state; // last rotary encoder state
byte counter = 1; // rotary encoder rotation counter (and start value)
unsigned int fractional = 0x00; // fractional sample position
int data_buffer; // temporary data storage to give a 1 sample buffer
int counter2=0; //buttons timer counter
void setup() {
//setup IO
pinMode(FOOTSWITCH, INPUT_PULLUP);
pinMode(TOGGLE, INPUT_PULLUP);
pinMode(PUSHBUTTON_1, INPUT_PULLUP);
pinMode(PUSHBUTTON_2, INPUT_PULLUP);
pinMode(LED, OUTPUT);
// setup ADC
ADMUX = 0x60; // left adjust, adc0, internal vcc
ADCSRA = 0xe5; // turn on adc, ck/32, auto trigger
ADCSRB = 0x07; // t1 capture for trigger
DIDR0 = 0x01; // turn off digital inputs for adc0
// setup PWM
TCCR1A = (((PWM_QTY - 1) << 5) | 0x80 | (PWM_MODE << 1)); //
TCCR1B = ((PWM_MODE << 3) | 0x11); // ck/1
TIMSK1 = 0x20; // interrupt on capture interrupt
ICR1H = (PWM_FREQ >> 8);
ICR1L = (PWM_FREQ & 0xff);
DDRB |= ((PWM_QTY << 1) | 0x02); // turn on outputs
sei(); // turn on interrupts - not really necessary with arduino
}
void loop() {
// nothing happens here, all down in the interrupt
}
void checkButton()
{
counter2++; //to save resources, the pushbuttons are checked every 2000 times.
if(counter2==2000)
{
counter2=0;
if (!digitalRead(PUSHBUTTON_1)) {
if (counter > B_MIN) counter-=1; // if not at min, decrement
}
if (!digitalRead(PUSHBUTTON_2)) {
if (counter < B_MAX) counter+=1; // if not at max, increment
}
}
}
ISR(TIMER1_CAPT_vect) { // all processing happens here
// output the last value calculated
OCR1AL = ((data_buffer + 0x8000) >> 8); // convert to unsigned, send out high byte
OCR1BL = data_buffer; // send out low byte
// get ADC data
byte temp1 = ADCL; // you need to fetch the low byte first
byte temp2 = ADCH; // yes it needs to be done this way
int input = ((temp2 << 8) | temp1) + 0x8000; // make a signed 16b value
checkButton();
// Delay_Depth=counter;
sDelayBuffer0[DelayCounter] = input;
POT0 = counter;
DelayCounter++;
if(DelayCounter >= Delay_Depth)
{
DelayCounter = 0;
if(count_up)
{
for(p=0;p<POT0+1;p++)
sDelayBuffer0[Delay_Depth+p]=sDelayBuffer0[Delay_Depth-1];
Delay_Depth=Delay_Depth+POT0;
if (Delay_Depth>=MAX_DELAY)count_up=0;
}
else
{
Delay_Depth=Delay_Depth-POT0;
if (Delay_Depth<=MIN_DELAY)count_up=1;
}
}
// save value for playback next interrupt
data_buffer = sDelayBuffer0[DelayCounter];
}