>> INICIO     

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  /* MULTITASKING  */  

 

 

 

  CLASE 

 

  COMANDOS  

 

#define

millis() 

static int

void()

 

 

 

  CODIGOS 

  Codigo 20 - a 

 

// INCREMENTO Y DECREMENTO CON VARIABLES INDIVIDUALES

 

int Numero_A =0;

int Numero_B =0;

 

void setup() {

  Serial.begin (9600);

 

}

 

void loop() {

 

  Serial.println (Numero_A);

  Numero_A++;

  delay (1000);

 

  Serial.println (Numero_B);

  Numero_B--;

  delay (1000);

}

 


 

 

  Codigo 20-b 

 

// INCREMENTOS y DECREMENTOS CON VOID INDEPENDIENTES

 

int Numero_A = 0;

int Numero_B = 0;

 

void setup() {

  Serial.begin (9600);

 

}

 

void loop() {

 

  Incremento();

  delay (1000);

  Decremento();

  delay (1000);

 

}

 

void Incremento() {

  Serial.println (Numero_A);

  Numero_A++;

}

 

void Decremento() {

  Serial.println (Numero_B);

  Numero_B--;

}


 

 

 

  Código 20-c 


 

// INCREMENTO y DECREMENTO

// CON VARIABLES ESTÁTICAS.

 

void setup() {

  Serial.begin (9600);

 

}

 

void loop() {

  Incremento();

  Decremento();

}

 

void Incremento() {

  static int Numero = 0;

  Serial.println (Numero);

  Numero++;

  delay (1000);

}

 

void Decremento() {

  static int Numero = 0;

  Serial.println (Numero);

  Numero--;

  delay (1000);

}

 

 

 

  Código 20-d 

 


 

// Capítulo 20

// #define - millis() - Multitasking - Funciones Void

 

#define Led_Verde 2

#define Led_Amarillo 3

#define Led_Rojo 4

#define Switch 8

int Tiempo_Verde = 1000;

int Tiempo_Amarillo = 250;

 

 

void setup() {

  pinMode (Led_Verde, OUTPUT);

  pinMode (Led_Amarillo, OUTPUT);

  pinMode (Led_Rojo, OUTPUT);

  //pinMode (Switch, INPUT); // No es necesario, por defalut todas las puertas son INPUT

  //pinMode (Switch, INPUT_PULLUP); // en caso de no tener resistencia de pull up en switch

}

 

 

void loop() {

  Verde();

  Amarillo();

  Rojo();

}

 

void Verde() {

  static long Ultimo_Cambio = 0; // Static para conservar el valor de la variable dentro de la funcion

  static boolean Estado_Led = LOW; // Almacena el estado del Led. LOW será valido sólo en la 1a iteracion

  long Hora = millis();

  if ((Hora - Ultimo_Cambio) > Tiempo_Verde) {

    Ultimo_Cambio = Hora;

    if (Estado_Led == HIGH) {

      digitalWrite (Led_Verde, LOW);

      Estado_Led = LOW;

    }

    else {

      digitalWrite (Led_Verde, HIGH);

      Estado_Led = HIGH;

    }

  }

}

 

void Amarillo() {

  static long Ultimo_Cambio = 0; // long para que coincida con millis; Static para conservar el valor de la variable dentro de la funcion

  static boolean Estado_Led = LOW; // Almacena el estado actual del Led; el LOW será valido sólo en la 1a iteracion

  long Hora = millis();

  if ((Hora - Ultimo_Cambio) > Tiempo_Amarillo) {

    Ultimo_Cambio = Hora;

    if (Estado_Led == HIGH) {

      digitalWrite (Led_Amarillo, LOW);

      Estado_Led = LOW;

    }

    else {

      digitalWrite (Led_Amarillo, HIGH);

      Estado_Led = HIGH;

    }

  }

 

}

 

void Rojo() {

  if (digitalRead (Switch) == HIGH) {

    digitalWrite (Led_Rojo, LOW);

  }

  else {

    digitalWrite (Led_Rojo, HIGH);

  }

 

}


 

 

 

  Código 20-e - VOLTIMETRO 

 

/*

**** MULTIMETRO CON INDICADOR LED Y LATIDO DE ON****

  1.- Medir Voltaje en A0

  2.- Colocar 5 leds entre D2 y D5

  3.- Colocar un Switch en D8

  4.- Establecer un tiempo de Backlight de 30 Segudos, reseteables con botos SW

  5.- Chequeo Inicial de encendido de todos los Leds

  6.- Indicadores LED:

    Rojo <= 2V

    Amarillo entre 2 y 4V

    Verde > a 4V

  7.- NO USAR DELAY EN EL LOOP

 

    EXITOOOO!!!!!

*/

#include <LiquidCrystal_I2C.h> //Carga Libreria

LiquidCrystal_I2C lcd(0x27, 20, 4); // Inicializa Display

#define Led_Verde 2

#define Led_Amarillo 3

#define Led_Rojo 4

#define Led_Azul 5

#define SW 8

// Variables de tiempo para Backlight

unsigned long BKL_Inicio = 0;

int BKL_Tiempo = 20000; // Tiempo encendido Backlight Display

 

// Variables de tiempo para Latido

int Tiempo; //Para Latido

int Tiempo_anterior; //Para Latido

int Estado; // Para Latido

 

// Variables  para Voltímetro

float Lectura_1 = 0;

float Volt_1;

float Medida_1 = 0;

int Cuentas = 100;

 

 

void setup() {

  //Inicialización Display

  lcd.init();

  lcd.backlight();

  lcd.clear();

  lcd.setCursor(3, 0);

  lcd.print("BRAIN  NASER");

  lcd.setCursor (3, 1);

  lcd.print("VOLTIMETRO DC");

  lcd.setCursor (3, 3);

  lcd.print("Bienvenido...");

 

  // pinMode

  pinMode (SW, INPUT);

  pinMode(A0, INPUT);

  for (int Led = 2; Led <= 5; Led++) {

    pinMode (Led, OUTPUT);

  }

  // Chequeo Inicial de Encendido Leds

  for (int Led = 2; Led <= 5; Led++) {

    digitalWrite (Led, HIGH);

    delay (500);

  }

  delay (2000); // Deja 2 segundos todos los leds encendidos

 

 

  // Apagado de Leds y limpiar pantalla

  lcd.clear();

  for (int Led = 2; Led <= 5; Led++) {

    digitalWrite (Led, LOW); // Apaga todos los leds

  }

  //delay (1000);

}

 

void loop() {

  Latido_ON();

  Switch();

  med_Voltios();

  Medidor_Led();

}

 

 

/////////// LATIDO /////////

void Latido_ON() {

  Tiempo = millis();

  if ((Tiempo - Tiempo_anterior) >= 5 && (Estado == 0)) {

    Estado = !Estado;

    digitalWrite (Led_Azul, 0);

    Tiempo_anterior = millis();

  }

 

  if ((Tiempo - Tiempo_anterior) >= 3000 && Estado == 1) {

    Estado = !Estado;

    digitalWrite (Led_Azul, 1);

    Tiempo_anterior = millis();

  }

}

 

 

/////////// SWITCH /////////

void Switch() {

 

  if (digitalRead (SW) == LOW) {

    BKL_Inicio = millis();

  }

  if ((millis() - BKL_Inicio) <= BKL_Tiempo) {

    lcd.backlight();

  }

  else {

    lcd.noBacklight();

  }

}

 

/////////// VOLTIMETRO /////////

void med_Voltios() {

  Lectura_1 = 0;

  Volt_1 = 0;

  Medida_1 = 0;

 

  for (int B = 1; B <= Cuentas; B++) {

    Lectura_1 = analogRead(A0);

    Volt_1 = Volt_1 + Lectura_1;

  }

 

  Medida_1 = (((Volt_1 / Cuentas) * 5) / 1023);

 

  lcd.setCursor(0, 0);

  lcd.print("Fuente A0 = ");

  lcd.print(Medida_1, 2);

  lcd.print("V");

}

 

/////////// MEDIDOR LED /////////

void Medidor_Led() {

  if (Medida_1 <= 2) {

    digitalWrite (Led_Rojo, HIGH);

    digitalWrite (Led_Amarillo, LOW);

    digitalWrite (Led_Verde, LOW);

  }

  if (Medida_1 > 2 && Medida_1 <= 4) {

    digitalWrite (Led_Rojo, HIGH);

    digitalWrite (Led_Amarillo, HIGH);

    digitalWrite (Led_Verde, LOW);

  }

  if (Medida_1 > 4 && Medida_1 <= 5) {

    digitalWrite (Led_Rojo, HIGH);

    digitalWrite (Led_Amarillo, HIGH);

    digitalWrite (Led_Verde, HIGH);

  }

}