int out = 13; //The Jack Mono Output int OSC1State = LOW; unsigned long OSC1cycle = 0; unsigned long OSC1microcycle = 0; unsigned long randcycle=0; unsigned long gencycle=0; unsigned long noisecycle=0; unsigned int in=0; //The increment for the ramps int ramp=0; //The ramp int dau=0; //El dau int noisesignal = 0; unsigned int k=0; //lo que ve de serial void setup() { pinMode(out, OUTPUT); Serial.begin(9600); randomSeed(analogRead(0)); } boolean microcycle(unsigned long *lastMicros, unsigned int microcycle) { unsigned long currentMicros = micros(); if(currentMicros - *lastMicros >= microcycle) { *lastMicros = currentMicros; return true; } else return false; } boolean mscycle(unsigned long *lastMillis, unsigned int cycle) { unsigned long currentMillis = millis(); if(currentMillis - *lastMillis >= cycle) { *lastMillis = currentMillis; return true; } else return false; } ///////////////////// void SIMPLE(int OSC1) { if(mscycle(&OSC1cycle, OSC1)) { if (OSC1 != 0){ digitalWrite(out, OSC1State); OSC1State = !OSC1State; } } } //////////////////// void SIMPLEPW(int OSC1, float asym) { if(mscycle(&OSC1cycle, OSC1*asym)) { if (OSC1 != 0){ digitalWrite(out, HIGH); } } if(mscycle(&OSC1cycle, OSC1*(1-asym))) { if (OSC1 != 0){ digitalWrite(out, LOW); } } } //////////////////// void MICRO(unsigned long OSC1){ if(microcycle(&OSC1microcycle, OSC1)) { if (OSC1 != 0){ digitalWrite(out, OSC1State); OSC1State = !OSC1State; } } } //////////////////// void PULSE(int del){ digitalWrite(out, HIGH); delayMicroseconds(del); //digitalWrite(out,LOW); //delay(500); } //////////////////// void MICROPW(int del, int del2){ digitalWrite(out, HIGH); delayMicroseconds(del); digitalWrite(out,LOW); delayMicroseconds(del2); } ////////////////////// int NOISE(int startf, int endf, int tempo){ if(microcycle(&noisecycle, tempo)) { int range = endf-startf; noisesignal = (random(range))+startf; } return noisesignal; } ///////////////////// void reset() { digitalWrite(out, LOW); in=0; } /////////////////// void loop() { if (Serial.available() > 0) { reset(); int inByte = Serial.read(); k=inByte; } switch (k){ case 0: reset(); break; case 1: MICRO(10); break; case 2: MICRO(100); break; case 3: MICRO(dado(1000,1)); break; case 4: MICRO(dado(100,1)); break; case 5: MICRO(dado(10,1)); break; case 6: MICRO(random(rampa(10,10000,10,5,'+'))); break; case 7: MICRO(dado(10000000,1)); break; case 8: PULSE(random(100)); break; case 9: PULSE(random(100)+100); break; case 10: PULSE(random(100)+200); break; case 11: PULSE(random(100)+500); break; case 12: MICROPW(100,1); break; case 13: MICROPW(100,random(1000)); break; case 14: MICROPW(100,random(100)); break; case 15: MICROPW(100,random(10)); break; case 16: MICROPW(10,random(10)); break; case 17: MICROPW(10,random(10000)); break; case 18: MICROPW(100,random(rampa(10,5000,10,5,'+'))); break; case 19: MICROPW(rampa(10,5000,10,100,'+'),rampa(10,5000,10,100,'+')); break; case 20: MICRO(random(10000)); break; case 21: SIMPLE(random(100)); break; case 22: MICRO(random(1000)+10000); break; case 23: MICRO(random(10000)+10000); break; case 24: MICRO(20000); break; case 25: MICRO(dado(10000, 10)); break; case 26: MICRO(dado(random(1000), 1)); break; case 27: MICRO(dado(random(1000), random(10))); break; } } /////////// int dado(int v, int n){ if(mscycle(&randcycle, n)) { dau=random(v); } return dau; } //////////// int rampa(int inici, int rang, int tempo, int pas, char signe){ if(microcycle(&gencycle, tempo)){ if(signe == '+'){ in+=pas; ramp=inici+(in%rang); if(ramp>=inici+rang){ ramp=0; } } if(signe == '-'){ in-=pas; ramp=inici-(in%rang); if(ramp<=inici-rang){ ramp=0; } } } return ramp; }