ReNoiser Sketch

#define ENCA_A 14
#define ENCA_B 15
#define ENCA_SW   16
#define ENCA_PORT PINC

#define ENCB_A 8
#define ENCB_B 9
#define ENCB_SW   10
#define ENCB_PORT PINB

//These are the pins that will talk to the shift register through SPI
#define SDIB    11    //Data
#define CLKB    12    //Clock
#define LEB    13    //Latch

#define SDIA    17    //Data
#define CLKA    18    //Clock
#define LEA     19    //Latch

#include 

int scaledCounter = 0;  //The LED output is based on a scaled veryson of the rotary encoder counter
int sequenceNumber=0;   //The output sequence for the LEDs

int flagA=0;
int flagB=0;

int buttonStateA = 0;         // current state of the button
int lastButtonStateA = 0;     // previous state of the button
int buttonStateB = 0;         // current state of the button
int lastButtonStateB = 0;     // previous state of the button

// define dimensions

const int BUTNUM=4;

// define arduino pins

const int button[BUTNUM] = {5,4,3,2};


boolean button_state[BUTNUM];


unsigned int sequence[3][16] = {{0x0,0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800,0x1000,0x2000,0x4000},
                                {0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff},
                                {0x0,0x7fff,0x3ffe,0x1ffc,0xff8,0x7f0,0x3e0,0x1c0,0x80,0x1c0,0x3e0,0x7f0,0xff8,0x1ffC,0x3ffe,0x7fff},
                               };

 
void setup()
{
  
  //Set SPI pins to output
  pinMode(SDIA, OUTPUT);
  pinMode(CLKA, OUTPUT);
  pinMode(LEA,OUTPUT);
  
  pinMode(SDIB, OUTPUT);
  pinMode(CLKB, OUTPUT);
  pinMode(LEB,OUTPUT);
  
  // Setup encoder pins as inputs
  pinMode(ENCA_A, INPUT);
  digitalWrite(ENCA_A, HIGH);
  pinMode(ENCA_B, INPUT);
  digitalWrite(ENCA_B, HIGH);
  pinMode(ENCA_SW, INPUT);
  digitalWrite(ENCA_SW, HIGH);
  
  pinMode(ENCB_A, INPUT);
  digitalWrite(ENCB_A, HIGH);
  pinMode(ENCB_B, INPUT);
  digitalWrite(ENCB_B, HIGH);
  pinMode(ENCB_SW, INPUT);
  digitalWrite(ENCB_SW, HIGH);
  
    // initialize buttons
  for (int i=0; i> 8));
        shiftOut(SDIA,CLKA,MSBFIRST,(0x8000));              
        digitalWrite(LEA,HIGH);
      }
      else {
        flagB=0;
        digitalWrite(LEA,LOW);  
        shiftOut(SDIA,CLKA,MSBFIRST,((0x0000) >> 8));
        shiftOut(SDIA,CLKA,MSBFIRST,(0x8000));              
        digitalWrite(LEA,HIGH);
      }
    }
  
  lastButtonStateA=buttonStateA;
  
  //detect if the button state changed
  
  buttonStateB=!digitalRead(ENCB_SW);
  
    if (buttonStateB != lastButtonStateB) {
      // if the button is pushed invert the flag
      if (buttonStateB == HIGH){
        flagB=!flagB;
      }
      
      // indicate the sellected channel with the led
      if (flagB){
        
        flagB=1;
        digitalWrite(LEB,LOW);  
        shiftOut(SDIB,CLKB,MSBFIRST,((0x8000) >> 8));
        shiftOut(SDIB,CLKB,MSBFIRST,(0x8000));              
        digitalWrite(LEB,HIGH);
      }
      else {
        flagB=0;
        digitalWrite(LEB,LOW);  
        shiftOut(SDIB,CLKB,MSBFIRST,((0x0000) >> 8));
        shiftOut(SDIB,CLKB,MSBFIRST,(0x8000));              
        digitalWrite(LEB,HIGH);
      }
    }
  
  lastButtonStateB=buttonStateB;
  
  
  if( tmpdataA ) {
// Change controller value (used for knobs, etc):
//   midi_controller_change(byte channel, byte controller, byte value);
    scaledCounter = map(counterA,0,50,0,127);
    
    counterA += tmpdataA;
    midi_controller_change(0, 0+flagA, scaledCounter);
    scaledCounter = map(counterA,0,100,0,15);
    
    //Send the LED output to the shift register 
    digitalWrite(LEA,LOW);
    shiftOut(SDIA,CLKA,MSBFIRST,(sequence[sequenceNumber][scaledCounter] >> 8));    //High byte first
    shiftOut(SDIA,CLKA,MSBFIRST,sequence[sequenceNumber][scaledCounter]);           //Low byte second
    digitalWrite(LEA,HIGH);
    
        if (flagA)
  {
    digitalWrite(LEA,LOW);  
    shiftOut(SDIA,CLKA,MSBFIRST,((sequence[sequenceNumber][scaledCounter]|0x8000) >> 8));
    shiftOut(SDIA,CLKA,MSBFIRST,sequence[sequenceNumber][scaledCounter]);              
    digitalWrite(LEA,HIGH);   
  }
  }
  

  
    if( tmpdataB ) {
// Change controller value (used for knobs, etc):
//   midi_controller_change(byte channel, byte controller, byte value);
    scaledCounter = map(counterB,0,50,0,127);
    midi_controller_change(0, 2+flagB, scaledCounter);
    counterB += tmpdataB;
    
    scaledCounter = map(counterB,0,100,0,15);
    
    //Send the LED output to the shift register 
    digitalWrite(LEB,LOW);
    shiftOut(SDIB,CLKB,MSBFIRST,(sequence[sequenceNumber][scaledCounter] >> 8));    //High byte first
    shiftOut(SDIB,CLKB,MSBFIRST,sequence[sequenceNumber][scaledCounter]);           //Low byte second
    digitalWrite(LEB,HIGH);
    
      
      if (flagB)
  {
    digitalWrite(LEB,LOW);  
    shiftOut(SDIB,CLKB,MSBFIRST,((sequence[sequenceNumber][scaledCounter]|0x8000) >> 8));
    shiftOut(SDIB,CLKB,MSBFIRST,sequence[sequenceNumber][scaledCounter]);              
    digitalWrite(LEB,HIGH);   
  }
  
  }

  
  buttonRead();
}
 
// returns change in encoder state (-1,0,1)
int8_t read_encoderA()
{
  static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENCA_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);
}

int8_t read_encoderB()
{
  static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  /**/
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENCB_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);
}

  //read all the button states
void buttonRead(){  
  for (int i=0; i
				    

1 comment for “ReNoiser Sketch

  1. Pingback: Tomash Ghz

Leave a Reply

Your email address will not be published. Required fields are marked *