Diferencia entre revisiones de «While statement conditional»

De ArduWiki
Saltar a: navegación, buscar
(Vea también)
(Código)
Línea 5: Línea 5:
 
== Código ==
 
== Código ==
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 +
const byte sensor = A0;
 +
const byte pulsador = 2;
 +
const byte led = 9;
 +
 +
//Valores sensor
 +
int min = 1023;
 +
int max = 0;
 +
int actual = 0;
 +
 +
void setup() {
 +
  pinMode(LED_BUILTIN, OUTPUT);
 +
  pinMode(led, OUTPUT);
 +
  pinMode(pulsador, INPUT_PULLUP);
 +
}
 +
 +
void calibra() {
 +
  // turn on the indicator LED to indicate that calibration is happening:
 +
  digitalWrite(LED_BUILTIN, HIGH);
 +
  actual = analogRead(sensor);  //Lee sensor:
 +
  //Guarda maximo
 +
  if (actual > max) {
 +
      max = actual;
 +
  }
 +
  //Guarda minimo
 +
  if (actual < min) {
 +
      min = actual;
 +
  }
 +
}
 +
 +
void loop() {
 +
  //Mientras esta pulsado calibramos sensor
 +
  while (digitalRead(pulsador) == LOW) {
 +
      calibra();
 +
  }
 +
  digitalWrite(LED_BUILTIN, LOW);  //Fin de calibracion
 +
 +
  sensorValue = analogRead(sensorPin); //Lee sensor
 +
 +
  //Aplica calibracion de sensor
 +
  sensorValue = map(actual, min, max, 0, 255);
 +
 +
  // in case the sensor value is outside the range seen during calibration
 +
  sensorValue = constrain(sensorValue, 0, 255);
 +
 +
  analogWrite(led, sensorValue);  //PWM al LED externo
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revisión del 19:39 26 jun 2018

Placa aplicable

Todas.

Código

const byte sensor = A0;
const byte pulsador = 2;
const byte led = 9;

//Valores sensor
int min = 1023;
int max = 0;
int actual = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(pulsador, INPUT_PULLUP);
}

void calibra() {
   // turn on the indicator LED to indicate that calibration is happening:
   digitalWrite(LED_BUILTIN, HIGH);
   actual = analogRead(sensor);  //Lee sensor:
   //Guarda maximo
   if (actual > max) {
      max = actual;
   } 
   //Guarda minimo
   if (actual < min) {
      min = actual;
   }
}

void loop() {
   //Mientras esta pulsado calibramos sensor
   while (digitalRead(pulsador) == LOW) {
      calibra();
   }
   digitalWrite(LED_BUILTIN, LOW);  //Fin de calibracion

   sensorValue = analogRead(sensorPin); //Lee sensor

   //Aplica calibracion de sensor
   sensorValue = map(actual, min, max, 0, 255);

   // in case the sensor value is outside the range seen during calibration
   sensorValue = constrain(sensorValue, 0, 255);

   analogWrite(led, sensorValue);  //PWM al LED externo
}

Comentarios

Vea también

Referencias