1 / 14

Le cció n 4: Enunciados Condicionales Soluciones de Programación RoboLab a NQC

Le cció n 4: Enunciados Condicionales Soluciones de Programación RoboLab a NQC. Ejercicio 1. Al partir el programa , se ejecuta un zumbido si se presiona el sensor de contacto. Sino, si el sensor de contacto no es presionado, se ejecuta un tono descendente . Ejercicio 1: Solución RoboLab.

phyliss
Télécharger la présentation

Le cció n 4: Enunciados Condicionales Soluciones de Programación RoboLab a NQC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lección 4:Enunciados CondicionalesSoluciones de ProgramaciónRoboLab a NQC

  2. Ejercicio 1 Al partir el programa, se ejecuta un zumbido si se presiona el sensor de contacto. Sino, si el sensor de contacto no es presionado, se ejecuta un tono descendente.

  3. Ejercicio 1: Solución RoboLab if (SENSOR_1 == 0) { PlaySound(SOUND_DOWN); } task main() { } else { PlaySound(SOUND_LOW_BEEP); } SetSensor(SENSOR_1, SENSOR_TOUCH);

  4. Ejercicio 1: Solución NQC En el puerto 1 hay un sensor de contacto. task main() { SetSensor(SENSOR_1, SENSOR_TOUCH); if (SENSOR_1 == 0) { PlaySound(SOUND_DOWN); } else { PlaySound(SOUND_LOW_BEEP); } } 1 significa “presionado.” 0 significa “liberado.” No olvide el doble signo igual! “SOUND_LOW_BEEP” Es un zumbido!

  5. Ejercicio 2 Si el sensor de luz está sobre papel blanco, enciende sólo el motor A. Si el sensor de luz está sobre un papel negro enciende el motor C. Hace esto para siempre.

  6. Ejercicio 2: Solución RoboLab If (LIGHT > THRESHOLD) { Off(OUT_C); OnFwd(OUT_A); } task main() { while (true) { } } else { Off(OUT_A); OnFwd(OUT_C); } SetSensor(LIGHT, SENSOR_LIGHT);

  7. Ejercicio 2: Solución NQC use macros para facilitar la lectura del código #define LIGHT SENSOR_1 #define THRESHOLD 45 task main() { SetSensor(LIGHT, SENSOR_LIGHT); while (true) { if (LIGHT > THRESHOLD) { Off(OUT_C); OnFwd(OUT_A); } else { Off(OUT_A); OnFwd(OUT_C); } } } while (true) crea un lazo infinito

  8. Ejercicio 3 Comience encendiendo el motor A para que avance. Si el sensor de rotación ha completado 3 vueltas, apague A y salga del programa. De lo contrario, ejecute un sonido, espere un segundo, y vuelva a verificar el sensor de rotación.

  9. Ejercicio 3: Solución RoboLab else { Off(OUT_A); break; } ClearSensor(ROT); while (true) { task main() { } OnFwd(OUT_A); if ( abs(ROT) <= 48) { PlaySound(SOUND_FAST_UP); Wait(100); } } SetSensor(ROT, SENSOR_ROTATION);

  10. Ejercicio 3: Solución NQC Si usa el valor absoluto no importa el sentido de la rotación. #define ROT SENSOR_1 task main() { SetSensor(ROT, SENSOR_ROTATION); ClearSensor(ROT); OnFwd(OUT_A); while (true) { if ( abs(ROT) <= 48) { PlaySound(SOUND_FAST_UP); Wait(100); } else { Off(OUT_A); break; } } } El enunciado “break” rompe el lazo while.

  11. Ejercicio 3: Solución NQC #define ROT SENSOR_1 task main() { SetSensor(ROT, SENSOR_ROTATION); ClearSensor(ROT); OnFwd(OUT_A); while ( abs(ROT) <= 48) { PlaySound(SOUND_FAST_UP); Wait(100); } Off(OUT_A); } Simplifique el código usando un lazo “while” con una condición Mientras la condición sea cierta, el lazowhilecontinuará ejecutándose

  12. Ejercicio 4 Si el sensor de luz está sobre papel blanco, si el sensor de contacto es presionado, encienda el motor A en reversa;en otro caso, encienda el motor A para que avance. Sino, si el sensor de luz está sobre el papel negro, si el sensor de contacto es presionado, encienda el motor C en reversa;en otro caso, encienda el motor C para que avance. Para todas las condiciones, el motor funciona por 4 segundos y después se apaga.

  13. Ejercicio 4: Solución RoboLab if (TOUCH == 0) { OnFwd(OUT_A); } else { OnRev(OUT_A); } task main() { Wait(400); Off(OUT_A); Off(OUT_C); if (LIGHT > 45) { } SetSensor(TOUCH, SENSOR_TOUCH); SetSensor(LIGHT, SENSOR_LIGHT); if (TOUCH == 0) { OnFwd(OUT_C); } else { OnRev(OUT_C); } } } else {

  14. #define TOUCH SENSOR_1 #define LIGHT SENSOR_2 task main() { SetSensor(TOUCH, SENSOR_TOUCH); SetSensor(LIGHT, SENSOR_LIGHT); if (LIGHT > 45) { if (TOUCH == 0) { OnFwd(OUT_A); } else { OnRev(OUT_A); } } else { if (TOUCH == 0) { OnFwd(OUT_C); } else { OnRev(OUT_C); } } Wait(400); Off(OUT_A); Off(OUT_C); } Ejercicio 4: Solución NQC

More Related