Potenciómetro, Médias e LEDs, [arduino]

Arduino Smoothing plus LEDs

Uma pequena experiência com leitura de potenciómetro, smoothing dos valores lidos e  activação de LEDs de acordo com média.

Baseado no tuturial Smoothing (http://arduino.cc/en/Tutorial/Smoothing) do Arduino com adição dos LEDs.

/*
Smoothing plus LEDs

Reads from analog input, calculates running average and activates LEDs

Based on the "Smoothing" example from de Arduino examples
( http://www.arduino.cc/en/Tutorial/Smoothing )

This code is in the public domain.
*/

const int inputPin = A0;
const int RedPin = 13;
const int YellowPin = 12;
const int GreenPin = 8;

const int RedOn = 900;
const int GreenOff = 800;

const int delayValue = 10;
const int numReadings = 30;

int readings[numReadings];
int index = 0;
int total = 0;
int average = 0;
int currentReading = 0;

void setup() {
  Serial.begin(9600);
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;

  pinMode(RedPin, OUTPUT);
  pinMode(YellowPin, OUTPUT);
  pinMode(GreenPin, OUTPUT);
  }

void loop() {
  currentReading = analogRead(inputPin);
  total = total - readings[index];
  readings[index] = currentReading;
  total = total + readings[index];
  index = index + 1;
  if (index>=numReadings)
    index=0;

  average = total / numReadings;

  digitalWrite(RedPin,( average > RedOn ) );
  digitalWrite(GreenPin, ( average < GreenOff ) );
  digitalWrite(
    YellowPin, ((average > GreenOff) && (average < RedOn))
    );

  Serial.println(average);
  delay(delayValue);
  }

Posted

in

,

by

Tags: