Gate_Mod

Here’s the code (and schematics) from the AniModule Gate_Mod
This is NOT the original code that shipped with the First Batch.
This is a Firmware Update.
The Original code had a bug in the delay out section that did not allow it to rest at 0V.
That has been fixed in this new revision 🙂
All Gate_Mods that shipped after Mid August 2015 have the new revision installed.

Gate_Mod_b_sch

You can copy/paste directly to your Arduino IDE.
it won’t compile unless you have Paul Stoffregen’s digitalWriteFast Library installed. There’s a little more about this on the Schematics / Code page.

There are pads for a 5 pin header at the bottom right of the ATMEGA328.
Installing a Header here facilitates an ICSP to use upload sketches directly to the Module without having to pull the IC.
From top to bottom the pins connect to the ATMEGA328:
VCC (I’m using +5V)
VSS (GND)
TX
RX
Reset

You can connect directly from an Arduino with the IC removed to program it or use a USB to TTL converter etc.
The VCC is connected to the Modules +5V via a diode so no need to fear for your USB port if you forgot to disconnect your -/+12V EuroRack PSU before you connected your programmer 😉

If you make changes to the code, improvements, or come up with something interesting to do with the Gate_Mod I’d LOVE to hear about it! Please take the time to register and post up in the comments! You can also catch me via email, or PM / Post on Muff’s or Electro Music or the AniModule FaceBook Page.

gate_mod_2h

#include <digitalWriteFast.h>

const int gateA = 2;
const int gateOutA = 3;
const int gateInvA = 4;
const int AnInA = A0;
const int bhvA = 9;

const int gateB = 5;
const int gateOutB = 6;
const int gateInvB = 7;
const int AnInB = A5;
const int bhvB = 10;

int invInterval = 10;

// Variables //
 
  unsigned long gateTimeA = 0;
  int intervalA = 0;
  boolean inStateA = 0;
  boolean outStateA = 0;
  boolean resetA = 1;
  boolean bhvStateA = 0;
  boolean prevInStateA = 0;
  boolean invStateA = 0;
  boolean prevInvStateA = 0;
  unsigned long invTimeA;
  
  unsigned long gateTimeB = 0;
  int intervalB = 0;
  boolean inStateB = 0;
  boolean outStateB = 0;
  boolean resetB = 1;
  boolean bhvStateB = 0;
  boolean prevInStateB = 0;
  boolean invStateB = 0;
  boolean prevInvStateB = 0;
  unsigned long invTimeB;
  
void setup(){
 pinModeFast(gateOutA, OUTPUT);
 pinModeFast(gateInvA, OUTPUT);
 
 digitalWriteFast(gateOutA, LOW);
 digitalWriteFast(gateInvA, LOW);
 
 pinModeFast(gateOutB, OUTPUT);
 pinModeFast(gateInvB, OUTPUT);
 
 digitalWriteFast(gateOutB, LOW);
 digitalWriteFast(gateInvB, LOW);
 
 Serial.begin(9600);
}

void bhvfnA0(){
  int cvA = analogRead(AnInA);
  /*Serial.println("cvA:");
  Serial.println(cvA);
  cvA = analogRead(AnInA);*/
  int intervalA = map(cvA, 0, 1024, 1, 1024 ) ;
  unsigned long  intervalA10 = intervalA * 10;
  //int invMarkA = 0;
  
  if ((inStateA == 1) && (resetA == 1) && (prevInStateA != inStateA)){
   resetA = 0;
   gateTimeA = millis();
   invTimeA = millis();  
   outStateA = HIGH;
   digitalWriteFast(gateOutA, outStateA);
  }
  
  if ((millis() - invTimeA) <= (intervalA10)){
   invStateA = LOW;
   digitalWriteFast(gateInvA, invStateA);
  }
 
  if ((millis() - gateTimeA) >= intervalA10) {
   outStateA = LOW;
   digitalWriteFast(gateOutA, outStateA);
   //invMarkA = 1;
  }
 
  if (((millis() - invTimeA) >= (intervalA10))&&
    ((millis() - invTimeA) <= (intervalA10 + invInterval))/*&&
    (invMarkA == 1)*/){
    //Serial.println("gateInvA:");
    Serial.println(gateInvA);
    invStateA = HIGH;
    digitalWriteFast(gateInvA, invStateA);    
  }
    
  if ((millis() - invTimeA) >= (intervalA10 + invInterval)){
   invStateA = LOW;
   digitalWriteFast(gateInvA, invStateA);
   resetA = 1;
   //invMarkA = 0;
   //Serial.println("gateInvA:");
   //Serial.println(gateInvA);
  }
  prevInStateA = inStateA;
  
}


void bhvfnA1(){
  int cvA = analogRead(AnInA);
  int intervalA = map(cvA, 0, 1024, 1, 1024 ) ;
  unsigned long  intervalA10 = intervalA * 10;
  
  if ((inStateA == 1) && (prevInStateA != inStateA)){
   gateTimeA = millis();
   invTimeA = millis(); 
   outStateA = HIGH;
   digitalWriteFast(gateOutA, outStateA);
  }
  
  if ((millis() - invTimeA) <= (intervalA10)){
   invStateA = LOW;
   digitalWriteFast(gateInvA, invStateA);
  }
 
  if ((millis() - gateTimeA) >= intervalA10) {
   outStateA = LOW;   
   digitalWriteFast(gateOutA, outStateA);   
  }
  
  if ((millis() - invTimeA) >= (intervalA10)&&
    ((millis() - invTimeA) <= (intervalA10 + invInterval))){
    //Serial.println("gateInvA:");
    Serial.println(gateInvA);
    invStateA = HIGH;
    digitalWriteFast(gateInvA, invStateA);
  }
  
  if ((millis() - invTimeA) >= ( intervalA10 + invInterval)){
   invStateA = LOW;
   digitalWriteFast(gateInvA, invStateA);
   //Serial.println("gateInvA:");
   //Serial.println(gateInvA);
  }
   prevInStateA = inStateA; 
}

void bhvfnB0(){
  int cvB = analogRead(AnInB);
  int intervalB = map(cvB, 0, 1024, 1, 1024 ) ;
  unsigned long  intervalB10 = intervalB * 10;
  
  if ((inStateB == 1) && (resetB == 1) && (prevInStateB != inStateB)){
   resetB = 0;
   gateTimeB = millis();
   invTimeB = millis();  
   outStateB = HIGH;
   digitalWriteFast(gateOutB, outStateB);
  }
  
  if ((millis() - invTimeB) <= (intervalB10)){
   invStateB = LOW;
   digitalWriteFast(gateInvB, invStateB);
  }
 
  if ((millis() - gateTimeB) >= intervalB10) {
   outStateB = LOW;
   digitalWriteFast(gateOutB, outStateB);
  }
  
  if ((millis() - invTimeB) >= (intervalB10)&&
    ((millis() - invTimeB) <= (intervalB10 + invInterval))){
    //Serial.println("gateInvB:");
    Serial.println(gateInvB);
    invStateB = HIGH;
    digitalWriteFast(gateInvB, invStateB);
  }
    
  if ((millis() - invTimeB) >= (intervalB10 + invInterval)){
   invStateB = LOW;
   digitalWriteFast(gateInvB, invStateB);
   resetB = 1;
   //Serial.println("gateInvB:");
   //Serial.println(gateInvB);
  }
  prevInStateB = inStateB;
  
}


void bhvfnB1(){
  int cvB = analogRead(AnInB);
  int intervalB = map(cvB, 0, 1024, 1, 1024 ) ;
  unsigned long  intervalB10 = intervalB * 10;
  
  if ((inStateB == 1) && (prevInStateB != inStateB)){
   gateTimeB = millis();
   invTimeB = millis(); 
   outStateB = HIGH;
   digitalWriteFast(gateOutB, outStateB);
  }
  
  if ((millis() - invTimeB) <= (intervalB10)){
   invStateB = LOW;
   digitalWriteFast(gateInvB, invStateB);
  }
 
  if ((millis() - gateTimeB) >= intervalB10) {
   outStateB = LOW;   
   digitalWriteFast(gateOutB, outStateB);   
  }
  
  if ((millis() - invTimeB) >= (intervalB10)&&
    ((millis() - invTimeB) <= (intervalB10 + invInterval))){
    //Serial.println("gateInvB:");
    Serial.println(gateInvB);
    invStateB = HIGH;
    digitalWriteFast(gateInvB, invStateB);
  }
  
  if ((millis() - invTimeB) >= ( intervalB10 + invInterval)){
   invStateB = LOW;
   digitalWriteFast(gateInvB, invStateB);
   //Serial.println("gateInvB:");
   //Serial.println(gateInvB);
  }
   prevInStateB = inStateB; 
}

void loop(){
  bhvStateA = digitalReadFast(bhvA);
  inStateA = digitalReadFast(gateA);
  bhvStateB = digitalReadFast(bhvB);
  inStateB = digitalReadFast(gateB);
  
  if (bhvStateA == LOW){
   bhvfnA0(); 
  }
  if (bhvStateA == HIGH){
    bhvfnA1();
  }
  if (bhvStateB == LOW){
   bhvfnB0(); 
  }
  if (bhvStateB == HIGH){
    bhvfnB1();
  }
}