Add new file

parents
// Variáveis para Display:
int sensorPin = A0; // select the input pin for the potentiometer
float sinal = 0; // variable to store the value coming from the sensor
int Led=13;
int threshold=35;
// Variáveis para Moving Average:
const int n=25;
int valores[n];
int index=0;
int total=0;
float media=0;
//Variáveis para Ponte H
int enable = 6;
int out1 = 2;
int out2 = 4;
//Variáveis para Inverter Sentido
int button = 8; // the number of the input pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
void setup() {
// declare OUTPUTS and INPUTS:
pinMode(A0, INPUT);
pinMode(Led,OUTPUT);
pinMode(enable, OUTPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(button,INPUT);
digitalWrite(out1, HIGH);
digitalWrite(out2,LOW);
// begin the serial monitor @ 9600 baud
Serial.begin(9600);
}
void loop() {
// Ler o sinal:
sinal = analogRead(A0)-607;
if (sinal>=0){
sinal = sinal;
} else{
sinal = -sinal;
}
// Fazer a média:
total=total-valores[0]+sinal; //Mais eficiente que calcular o total a cada iteração.
media=total/n;
//Deslocar o vetor
for(index=0;index<=n-2;index++){
valores[index]=valores[index+1];
}
valores[n-1]=sinal;
if(media>threshold){
digitalWrite(Led,HIGH);
analogWrite(enable,150);
} else {
digitalWrite(Led,LOW);
analogWrite(enable,0);
}
// Inverter Sentido
reading = digitalRead(button);
if (reading == HIGH) {
if (state == HIGH){
state = LOW;
} else {
state = HIGH;
}
delay(500);
}
if (state==HIGH){
digitalWrite(out1, HIGH);
digitalWrite(out2,LOW);
} else {
digitalWrite(out1, LOW);
digitalWrite(out2,HIGH);
}
Serial.println(media); // Substituir media por sinal para remover filtro.
Serial.print(" ");
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment