Paso 11: Código para sincronizar los LEDs
Nota: Asegúrese de comprobar el paso 13: "Cambios de última hora" donde he observado un por lo menos un cambio hasta que se hizo después de este Instructable fue 99% completado.
Aquí tienes! Se adjunta el archivo de código actual y se carga automáticamente en la interfaz de Arduino para usted, simplemente haga doble clic en el archivo.
Los comentarios más abajo, al principio del archivo de código, que responderá a las preguntas acerca de cómo todo esto confluye. Aunque voy a añadir aquí, una vez más, una nota sobre el bote en el sensor de los sonidos. Se convierten en amigos con esa olla, llevarlo a cenar (y bebidas), dormir con él, lo que tiene hasta para entender cuán meticuloso es (usted casado chicos entenderán). Señoras - JUST KIDDING! :-)
Algunas especificaciones aburridos son en el siguiente paso así que voy a decir adiós aquí. Mejor de la suerte debe usted tratar de éste. Sé que te juro que estoy loco y esto no funciona, que es hasta que el sensor de sonido pote bajo control. Siempre estoy dispuesto a responder preguntas o ayudar en cualquier manera que pueda así que no seas tímido si usted tiene una pregunta (límite de 2 por cliente) :-)
// written by Bradley S. Worthington-White 01/13/2016 /* * The functionality of this sketch is as follows; * * The "average" sensor value is the average of three contiguous readings * of the sensors current value. That value is then used within a range * of values to turn on specific LEDs. * * The "ON" statements that turn the LEDs on are read multiple times * before any "OFF" statements are read. How many times the "ON" * statements are read is determined by the value of "count" * under the "CheckCount" section. * * "UseSeq" determines which "ON" statements are read. There are * two choices for turning on the LEDs. Sequence one turns on * each LED individually, while sequence two groups the LEDs * together (two per group) and turns the groups on. * * Once the value in count is met the "OFF" statements are read. * There are four individual "OFF" sequences, which one is read * is determined by the value of OFFcount. Because each "OFF" sequence * resets "count" to zero and returns to "CheckCount" only one "OFF" * sequence is read before the program returns to the "ON" statements * and the process starts all over again. * */ /* NOTE: DO NOT make any adjustments to the values in this script if you * have turned on the serial monitor (Serial.begin). Running the serial * monitor greatly reduces the speed of the program and will throw * everything off. Use the serial monitor if you need to, but remember * to turn it off before changing any values in the script. */ const int SenOut = A1; // output from sensor to Analog Pin A1 const int RLED1 = 3; // assigning LEDs to digital pins 3 - 12 const int RLED2 = 4; const int RLED3 = 5; const int RLED4 = 6; const int RLED5 = 7; const int RLED6 = 8; const int RLED7 = 9; const int RLED8 = 10; const int RLED9 = 11; const int RLED10 = 12; int count = 0; // counter for # of times the "ON" functions are read int UseSeq = 0; // which "ON" function sequence to use int OFFcount = 0; // counter for "OFF" functions int sensVal [3]; // creates array for sensor data int i, j; // counter variables long average; // stores average sensor value void setup() { pinMode(RLED1, OUTPUT); // setting digital pins as OUTPUT pinMode(RLED2, OUTPUT); pinMode(RLED3, OUTPUT); pinMode(RLED4, OUTPUT); pinMode(RLED5, OUTPUT); pinMode(RLED6, OUTPUT); pinMode(RLED7, OUTPUT); pinMode(RLED8, OUTPUT); pinMode(RLED9, OUTPUT); pinMode(RLED10, OUTPUT); } void loop() { CheckCount: if (count > 1100) goto ledsOFF;// times "ON" statements read prior to "OFF" statements count = (count + 1);// increments "count" by 1 each run through the "ON" statements sensVal[(i++) % 3] = analogRead(SenOut); // Begin averaging sensor value average = 0; for (j = 0; j < 3; j++) { average += sensVal[j]; } average = average / 3; // End averaging sensor value UseSeq = 1; // determines which "ON" sequence to use if (UseSeq == 1) goto ON1; if (UseSeq == 2) goto ON2; ON1: // first "ON" sequence if (average > 875 && average < 885) digitalWrite (RLED1, HIGH); if (average > 800 && average < 815) digitalWrite (RLED2, HIGH); if (average > 700 && average < 703) digitalWrite (RLED3, HIGH); if (average > 600 && average < 604) digitalWrite (RLED4, HIGH); if (average > 500 && average < 503) digitalWrite (RLED5, HIGH); if (average > 400 && average < 404) digitalWrite (RLED6, HIGH); if (average > 300 && average < 303) digitalWrite (RLED7, HIGH); if (average > 200 && average < 203) digitalWrite (RLED8, HIGH); if (average > 100 && average < 103) digitalWrite (RLED9, HIGH); if (average > 50 && average < 52) digitalWrite (RLED10, HIGH); goto CheckCount; // returns to CheckCount ON2: // second "ON" sequence if (average > 875 && average < 878) digitalWrite (RLED1, HIGH); if (average > 875 && average < 878) digitalWrite (RLED2, HIGH); if (average > 700 && average < 703) digitalWrite (RLED3, HIGH); if (average > 700 && average < 703) digitalWrite (RLED4, HIGH); if (average > 500 && average < 503) digitalWrite (RLED5, HIGH); if (average > 500 && average < 503) digitalWrite (RLED6, HIGH); if (average > 300 && average < 303) digitalWrite (RLED7, HIGH); if (average > 300 && average < 303) digitalWrite (RLED8, HIGH); if (average > 100 && average < 103) digitalWrite (RLED9, HIGH); if (average > 100 && average < 103) digitalWrite (RLED10, HIGH); goto CheckCount; ledsOFF: // OFF sequences for LEDs OFFcount = (OFFcount + 1); // counts number of times "OFF" statements have been read if(OFFcount == 1) goto OFF1; // which "OFF" sequence is run else if (OFFcount == 2) goto OFF3; else if (OFFcount == 3) goto OFF2; else if (OFFcount == 4) goto OFF4; OFF1: // first "OFF" sequence digitalWrite (RLED1, LOW); digitalWrite (RLED2, LOW); digitalWrite (RLED3, LOW); digitalWrite (RLED4, LOW); digitalWrite (RLED5, LOW); count = 0; // resets the value of count under "CheckCount" to zero goto CheckCount; // returns to CheckCount OFF2: // second "OFF" sequence digitalWrite (RLED6, LOW); digitalWrite (RLED7, LOW); digitalWrite (RLED8, LOW); digitalWrite (RLED9, LOW); digitalWrite (RLED10, LOW); count = 0; goto CheckCount; OFF3: // third "OFF" sequence digitalWrite (RLED1, LOW); digitalWrite (RLED2, LOW); digitalWrite (RLED3, LOW); digitalWrite (RLED4, LOW); digitalWrite (RLED5, LOW); digitalWrite (RLED6, LOW); digitalWrite (RLED7, LOW); digitalWrite (RLED8, LOW); digitalWrite (RLED9, LOW); digitalWrite (RLED10, LOW); count = 0; goto CheckCount; OFF4: // fourth "OFF" sequence digitalWrite (RLED10, LOW); digitalWrite (RLED9, LOW); digitalWrite (RLED8, LOW); digitalWrite (RLED7, LOW); digitalWrite (RLED6, LOW); digitalWrite (RLED5, LOW); digitalWrite (RLED4, LOW); digitalWrite (RLED3, LOW); digitalWrite (RLED2, LOW); digitalWrite (RLED1, LOW); OFFcount = 0; // resets OFFcount to zero count = 0; goto CheckCount; }