miércoles, 30 de mayo de 2018

Luz con pulsador

En esta práctica, hemos creado un programa que enciende una luz led cuando se pulsa un botón, hemos utilizado un par de variables (led y pulsador) para el programa. Mientras se mantenga pulsado el pulsador, la luz quedará encendida. En teoría, habría que utilizar dos protecciones en la placa base, pero no nos funciona el pulsador si añadimos una. De forma que hemos  utilizado GND para dar energía al pulsador y al led, pero parte de la energía del led la hemos extraído por el pin de los 5V.
Al final de la entrada está el código copiado.



int pulsador = 1;
int led = 5;

void setup() {

pinMode (pulsador, INPUT);
pinMode (led, OUTPUT);
digitalWrite (led,LOW);
}

void loop() {
if (digitalRead (pulsador) == LOW){
  digitalWrite (led, HIGH);  
}
else{ 
  digitalWrite (led, LOW); 
}
}

martes, 15 de mayo de 2018

Simulación de dos semáforos, peatones y vehículos.

En esta práctica, hemos avanzado nuestra simulación de un semáforo, esta vez, añadimos el semáforo de los peatones, tuvimos que escribir varios "DigitalWrite" sin "delays" de por medio para que funcionase. El código está copiado al final.




/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  for (int i=3; i<8; i++)
  pinMode(i, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  
  digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(7, HIGH);    // turn the LED off by making the voltage LOW
  delay(5000);              // wait for a second
  digitalWrite(3, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
  delay(75);              // wait for a second

  digitalWrite(5, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(6, HIGH);    // turn the LED off by making the voltage LOW
  delay(2000);              // wait for a second
  digitalWrite(5, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(6, LOW);    // turn the LED off by making the voltage LOW
  delay(0);              // wait for a second
     
  digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(6, HIGH);    // turn the LED off by making the voltage LOW
  delay(3000);              // wait for a second
  digitalWrite(4, LOW);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(6, LOW);    // turn the LED off by making the voltage LOW
  delay(75);              // wait for a second  


miércoles, 2 de mayo de 2018

Coche Fantástico! A toda velocidad!

En la tercera practica, hemos hecho el efecto del coche fantastico, las luces van de un lado a otro parpadeando. Tuvimos que utilizar los pines 13, 12, 11, 10 y 9 junto al GND para que las luces encendieran correctamente. Al final del documento, esta el codigo escrito.



En este caso, hemos utilizado la forma lógicamente correcta de solucionar este ejercicio, sin embargo, hay otras formas, óptimas para llegar al mismo resultado con un menor número de órdenes. En este caso, utilizamos un bucle for para que la misma orden se repita varias veces en diferentes pines bajo la variable "i". Este proceso de bucle, se encuentra explicado al final de la entrada.


void setup() {

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop() {
  digitalWrite(13, HIGH);   
  delay(50);            
  digitalWrite(13, LOW);   
  delay(50);          

  digitalWrite(12, HIGH);  
  delay(50);            
  digitalWrite(12, LOW);  
  delay(50);       

  digitalWrite(11, HIGH);  
  delay(50);             
  digitalWrite(11, LOW);  
  delay(50);            

    digitalWrite(10, HIGH); 
  delay(50); 
  digitalWrite(10, LOW); 
  delay(50);      

    digitalWrite(9, HIGH);
  delay(50);          
  digitalWrite(9, LOW); 
  delay(50);           

    digitalWrite(10, HIGH); 
  delay(50);          
  digitalWrite(10, LOW);  
  delay(50);            

    digitalWrite(11, HIGH);
  delay(50);          
  digitalWrite(11, LOW);   
  delay(50);          

    digitalWrite(12, HIGH);  
  delay(50);         
  digitalWrite(12, LOW);  
  delay(50);         
}

--------------------------------------------------------------


/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */
void setup() {

  pinMode(13, OUTPUT); //Elegimos los pines que vamos a utilizar bajo la variable "i"
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
}


void loop() {                        //Abrimos el bucle principal del programa con una llave "{"

  for (int i=13; i>8; i--)       //Utilizamos el bucle for para darle el valor 13 a la variable "i",  después,                                               concretamos que el último número que se debe usar en el bucle ha de ser                                             superior a 8, o sea el 9. Al final, utilizamos la orden "i--" para ordenar que                                               el número de la variable "i" baje cada vez que el bucle se reinicie.
 {                                             //Además, abrimos el bucle con "{".
  digitalWrite(i, HIGH);  // En lugar de escribir un número en el pin, ponemos la variable "i".
  delay(100);       
  digitalWrite(i, LOW);
  delay(100);       

  }

   for (int i=10; i<12; i++)       //Utilizamos el bucle for para darle el valor 13 a la variable "i",  después,                                               concretamos que el último número que se debe usar en el bucle ha de ser                                             superior a 8, o sea el 9. Al final, utilizamos la orden "i--" para ordenar que                                               el número de la variable "i" baje cada vez que el bucle se reinicie.
 {                                             //Además, abrimos el bucle con "{".
  digitalWrite(i, HIGH);  // En lugar de escribir un número en el pin, ponemos la variable "i".
  delay(100);       
  digitalWrite(i, LOW);
  delay(100); 
 }}  

Pulsador Retrasado

En esta práctica, hemos modificado el programa del pulsador. Desde un  principio, hay un led rojo que está encendido permanentemente, al pu...