lunes, 8 de abril de 2019

Condicionales

Esta imagen pertenece al juego de Pokemon, que pasa en esta imagen?


Aquí se escoge el pokemon inicial.

PALABRA CLAVE: ESCOGER

Qué pasa en esta imagen? que podría estar pensado esta persona?


Tal vez piensa que:
si es mas grande la manzana verde que la roja.
si es más barata.
si es más dulce.
si es pesada
si esta buena o mala.
si  ....

PALABRA CLAVE: SI


Diferencia entre Sí y Si



SÍ = AFIRMACIÓN

SI =  CONDICIÓN




Vamos a trabajar con los botones de nuestra Circuit PlayGround.
Ubicamos los botones izquierdo (left) y el derecho (right)


SI EL BOTÓN ESTA SIN PRESIONAR SU VALOR ES 0
SI EL BOTÓN ESTA PRESIONADO SU VALOR  1


Vamos a escribir un código en el cual, SI se presiona el botón izquierdo (valor = 1), encienda el neopixel 0 y si está suelto o sin presionar (valor = 0) se apaga.




#include <Adafruit_CircuitPlayground.h>
//incio del Programa
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

//ciclo del Programa
void loop() {
      CircuitPlayground.setPixelColor( 0, 0, 0, 0 );
      //apaga los neopixeles
      if (CircuitPlayground.leftButton()== 1) { 
        //si se preciona el botón izquierdo
        CircuitPlayground.setPixelColor( 0, 255, 255, 255 );
        //enciende los neopixeles
      }
   } 



Reto 1:

Haga que si se presiona el botón izquierdo se enciendan todos los neopixeles de diferentes colores y si esta sin presionar se apagan.


#include <Adafruit_CircuitPlayground.h>
//incio del Programa
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

//ciclo del Programa
void loop() {
      //apaga los neopixeles
      CircuitPlayground.setPixelColor( 0, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 1, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 2, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 3, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 4, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 5, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 6, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 7, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 8, 0, 0, 0 );
      CircuitPlayground.setPixelColor( 9, 0, 0, 0 );
   
      if (CircuitPlayground.leftButton()== 1) { 
        //si se preciona el botón izquierdo
     
        //enciende los neopixeles
        CircuitPlayground.setPixelColor( 0, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 1, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 2, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 3, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 4, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 5, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 6, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 7, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 8, 255, 255, 255 );
        CircuitPlayground.setPixelColor( 9, 255, 255, 255 );
     
      }
   } 



Reto 2:

haga un semáforo peatonal. todos los neopixeles estan en verde si el botón no es presionado, si el botón se presiona, los neopixeles verdes se encienden y apagan (si es posible dos veces), luego se encienden en color amarillo por  5 segundos, luego cambian a rojo  y suena una alarma por 5 segundos.



#include <Adafruit_CircuitPlayground.h>
//incio del Programa
void setup() {
  Serial.begin(9600);
  CircuitPlayground.begin();
}

//ciclo del Programa
void loop() {
      // neopixeles en verde
      CircuitPlayground.setPixelColor( 0, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 1, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 2, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 3, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 4, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 5, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 6, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 7, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 8, 0, 255, 0 );
      CircuitPlayground.setPixelColor( 9, 0, 255, 0 );
   
      if (CircuitPlayground.leftButton()== 1) { 
        //si se preciona el botón izquierdo

        // neopixeles en verde
        CircuitPlayground.setPixelColor( 0, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 1, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 2, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 3, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 4, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 5, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 6, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 7, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 8, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 9, 0, 255, 0 );
        delay (1000);
        CircuitPlayground.setPixelColor( 0, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 1, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 2, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 3, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 4, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 5, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 6, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 7, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 8, 0, 0, 0 );
        CircuitPlayground.setPixelColor( 9, 0, 0, 0 );
        delay (1000);
        CircuitPlayground.setPixelColor( 0, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 1, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 2, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 3, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 4, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 5, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 6, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 7, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 8, 0, 255, 0 );
        CircuitPlayground.setPixelColor( 9, 0, 255, 0 );
        delay (1000);
        // neopixeles en verde
        CircuitPlayground.setPixelColor( 0, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 1, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 2, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 3, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 4, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 5, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 6, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 7, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 8, 255, 255, 0 );
        CircuitPlayground.setPixelColor( 9, 255, 255, 0 );
        delay (5000);
        CircuitPlayground.setPixelColor( 0, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 1, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 2, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 3, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 4, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 5, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 6, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 7, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 8, 255, 0, 0 );
        CircuitPlayground.setPixelColor( 9, 255, 0, 0 );

        CircuitPlayground.playTone( 262, 1000 );
        delay (5000);
      }
   }