DOWNLOAD CODE



//>Auduino code
#include
#include

uint16_t syncPhaseAcc;
uint16_t syncPhaseInc;
uint16_t grainPhaseAcc;
uint16_t grainPhaseInc;
uint16_t grainAmp;
uint8_t grainDecay;
uint16_t grain2PhaseAcc;
uint16_t grain2PhaseInc;
uint16_t grain2Amp;
uint8_t grain2Decay;

#define analogLDR (5)

#if defined(__AVR_ATmega8__)
//
// On old ATmega8 boards.
// Output is on pin 11
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_PIN 11
#define PWM_VALUE OCR2
#define PWM_INTERRUPT TIMER2_OVF_vect
#elif defined(__AVR_ATmega1280__)
//
// On the Arduino Mega
// Output is on pin 3
//
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 7
#define PWM_PIN 3
#define PWM_VALUE OCR3C
#define PWM_INTERRUPT TIMER3_OVF_vect
#else
//
// For modern ATmega168 and ATmega328 boards
// Output is on pin 3
//
#define PWM_PIN 3
#define PWM_VALUE OCR2B
#define LED_PIN 13
#define LED_PORT PORTB
#define LED_BIT 5
#define PWM_INTERRUPT TIMER2_OVF_vect
#endif

// Smooth logarithmic mapping
//
uint16_t antilogTable[] = {
64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
};
uint16_t mapPhaseInc(uint16_t input) {
return (antilogTable[input & 0x3f]) >> (input >> 6);
}

// Stepped chromatic mapping
//
uint16_t midiTable[] = {
17,18,19,20,22,23,24,26,27,29,31,32,34,36,38,41,43,46,48,51,54,58,61,65,69,73,
77,82,86,92,97,103,109,115,122,129,137,145,154,163,173,183,194,206,218,231,
244,259,274,291,308,326,346,366,388,411,435,461,489,518,549,581,616,652,691,
732,776,822,871,923,978,1036,1097,1163,1232,1305,1383,1465,1552,1644,1742,
1845,1955,2071,2195,2325,2463,2610,2765,2930,3104,3288,3484,3691,3910,4143,
4389,4650,4927,5220,5530,5859,6207,6577,6968,7382,7821,8286,8779,9301,9854,
10440,11060,11718,12415,13153,13935,14764,15642,16572,17557,18601,19708,20879,
22121,23436,24830,26306
};
uint16_t mapMidi(uint16_t input) {
return (midiTable[(1023-input) >> 3]);
}

// Stepped Pentatonic mapping

uint16_t pentatonicTable[54] = {
0,19,22,26,29,32,38,43,51,58,65,77,86,103,115,129,154,173,206,231,259,308,346,
411,461,518,616,691,822,923,1036,1232,1383,1644,1845,2071,2463,2765,3288,
3691,4143,4927,5530,6577,7382,8286,9854,11060,13153,14764,16572,19708,22121,26306
};

uint16_t mapPentatonic(uint16_t input) {
uint8_t value = (1023-input) / (1024/53);
return (pentatonicTable[value]);
}


void audioOn() {
#if defined(__AVR_ATmega8__)
// ATmega8 has different registers
TCCR2 = _BV(WGM20) | _BV(COM21) | _BV(CS20);
TIMSK = _BV(TOIE2);
#elif defined(__AVR_ATmega1280__)
TCCR3A = _BV(COM3C1) | _BV(WGM30);
TCCR3B = _BV(CS30);
TIMSK3 = _BV(TOIE3);
#else
// Set up PWM to 31.25kHz, phase accurate
TCCR2A = _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(CS20);
TIMSK2 = _BV(TOIE2);
#endif
}


///////////////////////////
// variables //
///////////////////////////

//> for the LDR sensor value reading, min and max for calibration
int sensorValue = 1024;
int sensorMin = 1024;
int sensorMax = 0;
//int val = 0;
//int val2 = 0;

//> for time to reading the duration of the "pulses" and to know the "state" that arrive for the input
long tiempoR = 0;
long tiempoA = 0;
long tiempoR2 = 0;
long tiempoA2 = 0;

//> input state to calculate the duration of the "pulses"
int input1State = LOW;
int input2State = LOW;
int input1StateNew = LOW ;
int input2StateNew = LOW ;

//> duration of the pulse that arrives through input1 and input2
int duracion1 = 0;
int duracion2 = 0;

//> 2 outputs and 2 inputs connected with neighbors
const int out1 = 10;
const int out2 = 9;

const int input1 = A0;
const int input2 = A1;

//> pin of the led
const int LedPin = 12;

//> State of vibration of the cell
int afinidad = 0 ;


int I1 = 0; //> the state read from the input1
int I2 = 0; //> the state read from the input2
int I1_past = 0; //> previous state of I1
int I2_past = 0; //> previous state of I2
int AR1 = 0; //> absolute valor of I1
int AR2 = 0; //> absolute valor of I2
int randii = 0; //> variable to store random numbers

//> a kind of memory that stores occurrences of the different states
int counter = 0; //>count when the input1 and input2 recives the same "state"

int counter0 = 0; //specific "state" counter (0,1,2,3,4,5,6,7)
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int counter5 = 0;
int counter6 = 0;
int counter7 = 0;


int fase = 0; //>cycle to change de states of audio
int fase2 = 0; //> cycle to generate the "pulses" and comunicated the "state" through output1 and output2
int fase3 = 0; //> cycle to do de evaluation between the input states in relation with the own state
int fase4 = 0; //> cycle to manage the media of the ldr value and the changes on the probability in the sounds

int forbiden_a = 3; //> when a "state" is very often is forbiden for a while
//int randrange = 0; //>
int mod = 0; //> modulo to make range in the cycles

int I1_old = 0; //> previous I1
int I2_old = 0; //> previous I2
int repeat1 = 0; //> repetitions of I1
int repeat2 = 0; //> repetitions of I2
int randfreq = 0; //> come from a random and multiply the freq of the audio states
int randii2 = 0; //> other random number
boolean state = LOW; //> variable to switch on//off led with the audio
int prob = 9; //> control the density of the audio more media (among of light) more probability of sound
int media = 0; //> is a mapping range of the LDR value (6 to 10)
int media6 = 0; //> real media of the range of "media" work with counter and change "prob"
int media7 = 0;
int media8 = 0;
int media9 = 0;
int media10 = 0;
//int media11 = 0;



//> array for the sound

int arsc0[]={208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,274,276,278,280,282,284,286,288,290,292,294,
296,298,300,302,304,306,308,310,312,314,316,318,320,322,324,326,328,330,332,334,336,338,340,342,344,346,348,350,352,354,356,358,360,362,364,366,368,370,372,374,376,378,380,382,
384,386,388,390,392,394,396,398,400,402,404,406,408,410,412,414,416,418,420,422,424,426,428,430,432,434,436,438,440,442,444,446,448,450,452,454,456,458,460,462,464,466,468,470,
472,474,476,478,480,482,484,486,488,490,492,494,496,498,500,502,504,506,508,510,512,514,516,518,520,522,524,526,528,530,532,534,536,538,540,542,544,546,548,550,552,554,556,558,
560,562,564,566,568,570,572,574,576,578,580,582,584,586,588,590,592,594,596,598,600,602,604,606,608,610,612,614,616,618,620,622,624,626,628,630,632,634,636,638,640,642,644,646,
648,650,652,654,656,658,660,662,664,666,668,670,672,674,676,678,680,682,684,686,688,690,692,694,696,698,700,702,704,706,708,710,712,714,716,718,720,722,724,726,728,730,732,734,
736,738,740,742,744,746,748,750,752,754,756,758,760,762,764,766,768,770,772,774,776,778,780,782,784,786,788,790,792,794,796,798,800,802,804,806,808,810,812,814,816,818,820,822,
824,826,828,830,832,834,836,838,840,842,844,846,848,850,852,854,856,858,860,862,864,866,868,870,872,874,876,878,880,882,884,886,888,890,892,894,896,898,900,902,904,906,908,910,
912,914,916,918,920,922,924,926,928,930,932,934,936,938,940,942,944,946,948,950,952,954,956,958,960,962,964,966,968,970,972,974,976,978,980,982,984,986,988,990,992,994,996,998,
1000,1002,1004,1006,1008,1010,1012,1014,1016,1018,1020,1022,1024,1026,1028,1030,1032,1034,1036,1038,1040,1042,1044,1046,1048,1050,1052,1054,1056,1058,1060,1062,1064,1066,1068,
1070,1072,1074,1076,1078,1080,1082,1084,1086,1088,1090,1092,1094,1096,1098,1100,1102,1104,1106,1108,1110,1112,1114,1116,1118,1120,1122,1124,1126,1128,1130,1132,1134,1136,1138,
1140,1142,1144,1146,1148,1150,1152,1154,1156,1158,1160,1162,1164,1166,1168,1170,1172,1174,1176,1178,1180,1182,1184,1186,1188,1190,1192,1194,1196,1198};



///////////////////////
//////////////////////
/////////////////////
////////////////////
///////////////////

void setup() {
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(input1, INPUT);
pinMode(input2, INPUT);
randomSeed(analogRead(analogLDR));
afinidad = random(8);

////////////////////autocalibrado y setup del audio presets
pinMode(PWM_PIN,OUTPUT);
audioOn();
pinMode(LED_PIN,OUTPUT);


//calibration of the LDR sensor getting min and max value

while (millis() < 14360) {
sensorValue = analogRead(analogLDR);

if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}

if(millis() < 1000){digitalWrite(LedPin, HIGH);}
if(millis() > 1000 && millis() < 2000){digitalWrite(LedPin,LOW);}
if(millis() > 2000 && millis() < 3000){digitalWrite(LedPin,HIGH);}
if(millis() > 3000 && millis() < 4000){digitalWrite(LedPin,LOW);}
if(millis() > 4000 && millis() < 5000){digitalWrite(LedPin,HIGH);}
if(millis() > 5000 && millis() < 6000){digitalWrite(LedPin,LOW);}
if(millis() > 6000 && millis() < 7000){digitalWrite(LedPin,HIGH);}
if(millis() > 7000 && millis() < 7700){digitalWrite(LedPin,LOW);}
if(millis() > 7700 && millis() < 8400){digitalWrite(LedPin,HIGH);}
if(millis() > 8400 && millis() < 9100){digitalWrite(LedPin,LOW);}
if(millis() > 9100 && millis() < 9800){digitalWrite(LedPin,HIGH);}
if(millis() > 9800 && millis() < 10300){digitalWrite(LedPin,LOW);}
if(millis() > 10300 && millis() < 10800){digitalWrite(LedPin,HIGH);}
if(millis() > 10800 && millis() < 11300){digitalWrite(LedPin,LOW);}
if(millis() > 11300 && millis() < 11800){digitalWrite(LedPin,HIGH);}
if(millis() > 11800 && millis() < 12100){digitalWrite(LedPin,LOW);}
if(millis() > 12100 && millis() < 12400){digitalWrite(LedPin,HIGH);}
if(millis() > 12400 && millis() < 12700){digitalWrite(LedPin,LOW);}
if(millis() > 12700 && millis() < 13000){digitalWrite(LedPin,HIGH);}
if(millis() > 13000 && millis() < 13300){digitalWrite(LedPin,LOW);}
if(millis() > 13300 && millis() < 13400){digitalWrite(LedPin,HIGH);}
if(millis() > 13400 && millis() < 13500){digitalWrite(LedPin,LOW);}
if(millis() > 13500 && millis() < 13600){digitalWrite(LedPin,HIGH);}
if(millis() > 13600 && millis() < 13700){digitalWrite(LedPin,LOW);}
if(millis() > 13700 && millis() < 13770){digitalWrite(LedPin,HIGH);}
if(millis() > 13770 && millis() < 13840){digitalWrite(LedPin,LOW);}
if(millis() > 13840 && millis() < 14100){digitalWrite(LedPin,HIGH);}
if(millis() > 14100 && millis() < 14150){digitalWrite(LedPin,LOW);}
if(millis() > 14150 && millis() < 14200){digitalWrite(LedPin,HIGH);}
if(millis() > 14200 && millis() < 14250){digitalWrite(LedPin,LOW);}
if(millis() > 14250 && millis() < 14280){digitalWrite(LedPin,HIGH);}
if(millis() > 14280 && millis() < 14310){digitalWrite(LedPin,LOW);}
if(millis() > 14310 && millis() < 14340){digitalWrite(LedPin,HIGH);}
if(millis() > 14340){digitalWrite(LedPin,LOW);}




}


}


//////////////////
/////////////////
////////////////
///////////////
//////////////


void loop() {



//> read duration of pulse INPUT1 and I1 value


input1StateNew = digitalRead(input1);

if ((input1StateNew == HIGH)&&(input1StateNew != input1State)){
tiempoR = millis();
input1State = input1StateNew;
}

if ((input1StateNew == LOW)&&(input1StateNew != input1State)){
tiempoA = millis();
duracion1=tiempoA-tiempoR;
input1State = input1StateNew;

I1_old = I1;

if ((duracion1 > 800)&&(duracion1 < 900)){
I1 = 0;
}

if ((duracion1 > 700)&&(duracion1 < 800)){
I1 = 1;
}
if ((duracion1 > 600)&&(duracion1 < 700)){
I1 = 2;
}
if ((duracion1 > 500)&&(duracion1 < 600)){
I1 = 3;
}
if ((duracion1 > 400)&&(duracion1 < 500)){
I1 = 4;
}
if ((duracion1 > 300)&&(duracion1 < 400)){
I1 = 5;
}
if ((duracion1 > 200)&&(duracion1 < 300)) {
I1 = 6;
}
if ((duracion1 > 100)&&(duracion1 < 200)) {
I1 = 7;
}
}


//> read duration of pulse INPUT2 and I2 value

input2StateNew = digitalRead(input2);

if ((input2StateNew == HIGH)&&(input2StateNew != input2State)){
tiempoR2 = millis();
input2State = input2StateNew;
}

if ((input2StateNew == LOW)&&(input2StateNew != input2State)){
tiempoA2 = millis();
duracion2=tiempoA2-tiempoR2;
input2State = input2StateNew;
I2_old = I2;

if ((duracion2 > 800)&&(duracion2 < 900)){
I2 = 0;
}
if ((duracion2 > 700)&&(duracion2 < 800)){
I2 = 1;
}
if ((duracion2 > 600)&&(duracion2 < 700)){
I2 = 2;
}
if ((duracion2 > 500)&&(duracion2 < 600)){
I2 = 3;
}
if ((duracion2 > 400)&&(duracion2 < 500)){
I2 = 4;
}
if ((duracion2 > 300)&&(duracion2 < 400)){
I2 = 5;
}
if ((duracion2 > 200)&&(duracion2 < 300)) {
I2 = 6;
}
if ((duracion2 > 100)&&(duracion2 < 200)) {
I2 = 7;
}

}
blinktime(afinidad); //> blinktime funtion, the core of the audio and led behavior

//> memorie ( counters ) of the states and thresholds to make a 1 sec "note" and reset all the counters
if (afinidad==0){
counter0++;
if (counter0 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 200; //> audio parameter
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}

afinidad = random(8);

counter0 = 0;


}
}
else if (afinidad==1){
counter1++;
if (counter1 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 400;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}


afinidad = random(8);

counter1 = 0;

}
}
else if (afinidad==2){
counter2++;
if (counter2 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 666;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}
forbiden_a = 2;
afinidad=random(8);

counter2 = 0;

}
}
else if (afinidad==3){
counter3++;
if(counter3 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 888;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}
forbiden_a = 3;
afinidad=random(8);


counter3 = 0;

}
}
else if (afinidad==4){
counter4++;
if(counter4 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 1111;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}
forbiden_a = 4;
afinidad = random(8);

counter4 = 0;

}
}
else if (afinidad==5){
counter5++;
if(counter5 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 1555;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}

forbiden_a = 5;
afinidad = random(8);

counter5 = 0;

}
}


else if (afinidad==6){
counter6++;
if(counter6 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 22222222;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}

forbiden_a = 6;
afinidad = random(8);

counter6 = 0;

}
}
else if(afinidad==7){
counter7++;
if(counter7 >= 20000){ randii = random(5);
if (randii == 0){
syncPhaseInc = 3333;
digitalWrite(LedPin, HIGH);
delay(1000);
digitalWrite(LedPin, LOW);
}


afinidad = random(8);

counter7 = 0;
}
}


} //close the void loop





void blinktime( int cp){

sensorValue = analogRead(analogLDR);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 1000);
sensorValue = constrain(sensorValue, 0, 600);

//> audio parameters modulated by the sensorValue LDR
grainPhaseInc = 1285 - sensorValue/2;
grainDecay = 10 + sensorValue/2;
grain2PhaseInc = 100 + sensorValue/2;
grain2Decay = 10 + sensorValue/3;


if (fase4++ < 999){} //cycle to manage the media (among of light) that change "prob"
else {
media = map(sensorValue,0,600,6,10);
if (media == 6){
media6++;
if (media6 > 10){ prob = 6;

media8 = 0;
media9 = 0;
media10 = 0;

}
}
if (media == 7){
media7++;
if (media7 > 10){ prob = 7;

media8 = 0;
media9 = 0;
media10 = 0;

}
}


if (media == 8){
media8++;
if (media8 > 10){ prob = 8;

media8 = 0;
media9 = 0;
media10 = 0;

}
}
if (media == 9){
media9++;
if (media9 > 100){ prob = 9;

media8 = 0;
media9 = 0;
media10 = 0;

}
}
if (media == 10){
media10++;
if (media10 > 300){ prob = 10;

media8 = 0;
media9 = 0;
media10 = 0;

}
}

}

///////////////////////////////////////////////////////////////////////////////////////////
if (fase++ < 999){ //> cycle to manage the audio and light states


if (cp == 0){ mod = fase % 1999;
if (mod < 499){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 2;}
else if (randii2 == 1){randfreq = 3;}
else {randfreq = 4;}
state = HIGH;
}
}


syncPhaseInc = arsc0[mod]*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}

if (cp == 1){ mod = fase % 1000;
if (mod < 499){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 1;}
else if (randii2 == 1){randfreq = 2;}
else {randfreq = 3;}
state = HIGH;
}
}


syncPhaseInc = 200*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}

if (cp == 2){ mod = fase % 750;
if (mod < 375){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 8;}
else if (randii2 == 1){randfreq = 9;}
else {randfreq = 10;}
state = HIGH;
}
}


syncPhaseInc = arsc0[mod]*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}

if (cp == 3){ mod = fase % 500;
if (mod < 250){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 11;}
else if (randii2 == 1){randfreq = 12;}
else {randfreq = 13;}
state = HIGH;
}
}


syncPhaseInc = 200*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}

if (cp == 4){ mod = fase % 187;
if (mod < 94){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 14;}
else if (randii2 == 1){randfreq = 15;}
else {randfreq = 16;}
state = HIGH;
}
}


syncPhaseInc = arsc0[mod]*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}


if (cp == 5){ mod = fase % 125;
if (mod < 62){
if(mod==1){
state = LOW;
randii=random(prob + 1);
if (randii<prob){randfreq = 0;
state = LOW;}
else {randii2 = random(3);
if (randii2 == 0) {randfreq = 17;}
else if (randii2 == 1){randfreq = 18;}
else {randfreq = 19;}
state = HIGH;
}
}


syncPhaseInc = 200*randfreq;
digitalWrite(LedPin, state);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, state);
}
}


if (cp == 6){ mod = fase % 62;
if (mod < 25){
syncPhaseInc = arsc0[mod]+650;
digitalWrite(LedPin, HIGH);
}
else {syncPhaseInc = 0;
digitalWrite(LedPin, LOW);
}
}
if (cp == 7){
syncPhaseInc = 0;
digitalWrite(LedPin, LOW);


}



}

else {

fase = 0;
}


if (fase2++ < 999) { //> cycle to manage the pulse emition in relation with the own state through the outputs

if (cp == 0){
if (fase2 < 850){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 850){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 1){
if (fase2 < 750){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 750){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 2){
if (fase2 < 650){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 650){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp ==3){
if (fase2 < 550){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 550){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 4){
if (fase2 < 450){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 450){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 5){
if (fase2 < 350){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 350){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 6){
if (fase2 < 250){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 250){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
if (cp == 7){
if (fase2 < 150){
digitalWrite(out1, HIGH);
digitalWrite(out2, HIGH);
}
if (fase2 > 150){
digitalWrite(out1, LOW);
digitalWrite(out2, LOW);
}
}
}

else {fase2 = 0;}



//////>> EVALUATION FOR THE NEXT state

if (fase3++ < 1999) {}
else { AR1 = afinidad - I1;
AR1 = abs(AR1);
AR2 = afinidad - I2;
AR2 = abs(AR2);

if (I1 == I1_old){

if (repeat1 <= 4){
repeat1++;
if (AR1 < AR2){
afinidad = I1;
}

if (AR1 > AR2){
afinidad = I2;
}

if ((AR1 == AR2)&&(I1 != I2)){
randii = random(2);
if (randii == 0){
afinidad = I1;
}
if (randii == 1){
afinidad = I2;
}
}

if ((AR1 == AR2)&&(I1 == I2)){
counter++;
afinidad = I1;
if (counter >= 20){
afinidad = random(8);
counter = 0;
forbiden_a = I1;
}
}
}



if ((repeat1>4)&&(repeat1<16)){

afinidad = I1;
repeat1++;
}
if (repeat1 > 16 ){

repeat1 = 0;
}

}



if (I2 == I2_old){

if(repeat2 <= 4){
repeat2++;
if (AR1 < AR2){
afinidad = I1;
}

if (AR1 > AR2){
afinidad = I2;
}

if ((AR1 == AR2)&&(I1 != I2)){
randii = random(2);
if (randii == 0){
afinidad = I1;
}
if (randii == 1){
afinidad = I2;
}
}

if ((AR1 == AR2)&&(I1 == I2)){
counter++;
afinidad = I1;
if (counter >= 20){
afinidad = random(8);
counter = 0;
forbiden_a = I1;
repeat2++;
}
}
}



if ((repeat2 > 4 )&&(repeat2 < 16)){
repeat2++;
afinidad = I2;
}
if (repeat2 > 16 ){
repeat2 = 0;
}

}


else {





//INVERSE EVALUATION when the counters are over the thershold 5

if((counter > 5)||(counter0 > 5)||(counter1 > 5)||(counter2 > 5)||(counter3 > 5)||(counter4 > 5)||(counter5 > 5)||(counter6 > 5)||(counter7 > 5 ) ){


if (AR1 < AR2){
afinidad = I2;
}

if (AR1 > AR2){
afinidad = I1;
}

if ((AR1 == AR2)&&(I1 != I2)){
randii = random(2);
if (randii == 0){
afinidad = I1;
}
if (randii == 1){
afinidad = I2;
}
}

if ((AR1 == AR2)&&(I1 == I2)){
counter++;
afinidad = I1;
if (counter >= 10){
afinidad = random(8);
counter = 0;
forbiden_a = I1;


}
}


}

else{ //> NORMAL EVALUATION //////////////////////////////////////


if (AR1 < AR2){
afinidad = I1;
}

if (AR1 > AR2){
afinidad = I2;
}

if ((AR1 == AR2)&&(I1 != I2)){
randii = random(2);
if (randii == 0){
afinidad = I1;
}
if (randii == 1){
afinidad = I2;
}
}

if ((AR1 == AR2)&&(I1 == I2)){
counter++;
afinidad = I1;
if (counter >= 20){
afinidad = random(8);
counter = 0;

forbiden_a = I1;

}
}









}
}

fase3 = 0; // Reset
if (afinidad == forbiden_a){ afinidad = random(8);
}

}


} //> close funcion VOID



//////////////////////////////////////////////////////
//> more auduino code

SIGNAL(PWM_INTERRUPT)
{
uint8_t value;
uint16_t output;

syncPhaseAcc += syncPhaseInc;
if (syncPhaseAcc < syncPhaseInc) {
// Time to start the next grain
grainPhaseAcc = 0;
grainAmp = 0x7fff;
grain2PhaseAcc = 0;
grain2Amp = 0x7fff;
LED_PORT ^= 1 << LED_BIT; // Faster than using digitalWrite
}

// Increment the phase of the grain oscillators
grainPhaseAcc += grainPhaseInc;
grain2PhaseAcc += grain2PhaseInc;

// Convert phase into a triangle wave
value = (grainPhaseAcc >> 7) & 0xff;
if (grainPhaseAcc & 0x8000) value = ~value;
// Multiply by current grain amplitude to get sample
output = value * (grainAmp >> 8);

// Repeat for second grain
value = (grain2PhaseAcc >> 7) & 0xff;
if (grain2PhaseAcc & 0x8000) value = ~value;
output += value * (grain2Amp >> 8);

// Make the grain amplitudes decay by a factor every sample (exponential decay)
grainAmp -= (grainAmp >> 8) * grainDecay;
grain2Amp -= (grain2Amp >> 8) * grain2Decay;

// Scale output to the available range, clipping if necessary
output >>= 9;
if (output > 255) output = 255;

// Output to PWM (this is faster than using analogWrite)
PWM_VALUE = output;
}



/*
________________________________________
/ Steady movement is more important than \
| speed, much of the time. So long as |
| there is a regular progression of |
| stimuli to get your mental hooks into, |
| there is room for lateral movement. |
| Once this begins, its rate is a matter |
| of discretion. |
| |
\ -- Corwin, Prince of Amber /
----------------------------------------
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/

*/