/* Smoothing Reads repeatedly from an analog input, calculating a running average and printing it to the computer. Keeps ten readings in an array and continually averages them. The circuit: * Analog sensor (potentiometer will do) attached to analog input 0 Created 22 April 2007 By David A. Mellis http://www.arduino.cc/en/Tutorial/Smoothing */ // Define the number of samples to keep track of. The higher the number, // the more the readings will be smoothed, but the slower the output will // respond to the input. Using a constant rather than a normal variable lets // use this value to determine the size of the readings array. const int numReadings = 20; //Código en español -> pot. para el ratio const int numeroLecturas = 10; int readings[numReadings]; // the readings from the analog input int index = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int lecturas[numeroLecturas]; // the readings from the analog input int i = 0; // the index of the current reading int totalLecturas = 0; // the running total int media = 0; // the average int zero = 520.2; float ratio = 21.34; float cm = 0; int inputPin = 0; /* const int atrasPin = 8; const int avanzarPin = 9; int buttonStateAtras = 0; int buttonStateAvanzar = 0; int buttonAtrasCounter = 0; int buttonAvanzarCounter = 0; int lastAtrasState = 0; int lastAvanzarState = 0; */ #include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // initialize serial communication with computer: Serial.begin(9600); // initialize all the readings to 0: for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; for (int estaLectura = 0; estaLectura < numeroLecturas; estaLectura++) lecturas[estaLectura] = 0; /* pinMode(atrasPin, INPUT); pinMode(avanzarPin, INPUT); */ lcd.begin(16, 2); } void loop() { // subtract the last reading: total= total - readings[index]; // read from the sensor: readings[index] = analogRead(inputPin); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings) // ...wrap around to the beginning: index = 0; // calculate the average: average = total / numReadings; //Botones de ajuste de ratio /* buttonStateAtras = digitalRead(atrasPin); buttonStateAvanzar = digitalRead(avanzarPin); if (buttonStateAtras != lastAtrasState) { if (buttonStateAtras == HIGH) { buttonAtrasCounter++; ratio = ratio - 0.1; } } lastAtrasState = buttonStateAtras; if (buttonStateAvanzar != lastAvanzarState) { if (buttonStateAvanzar == HIGH) { buttonAvanzarCounter++; ratio = ratio + 0.1; } } lastAvanzarState = buttonStateAvanzar; */ //Suaviza los cm /* totalLecturas = totalLecturas - lecturas[i]; lecturas[i] = cm; totalLecturas = totalLecturas + lecturas[i]; i = i + 1; if (i >= numeroLecturas) i = 0; media = totalLecturas / numeroLecturas; cm = media; */ cm = average - zero; cm = cm / ratio; Serial.println(average); // send it to the computer (as ASCII digits) /* Serial.print("Lectura = "); Serial.print(average, DEC); Serial.print(", ratio = "); Serial.print(ratio, DEC); Serial.print(", medida = "); Serial.print(cm, DEC); Serial.println(" cm"); */ lcd.setCursor(0, 0); lcd.print(cm, DEC); lcd.println(" cm"); lcd.setCursor(0,1); lcd.print("ratio = "); lcd.println(ratio, DEC); }