Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guardar somente soma para calculo da media #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions Vibra__o1.0/Vibra__o1.0.ino
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
float media ;
int total = 0 ;
int microphone = 'A0' ;
int viber = 2 ;
int now ;
int valores[60];
int cont = 0;
float media;
int microphone = A0;
int viber = 2;

// Utilizar esses 2 valores como float para garantir que
// a divisão retorne um float
float soma;
float cont;

#define INTERVALO_LEITURA 200
unsigned long ultimaLeitura;

void setup() {
// put your setup code here, to run once:
pinMode(viber, OUTPUT);
pinMode(microphone, INPUT);
Serial.begin(9600);
}
void updatemedia() {
for (int i = 0; i < 60 ; i++) {
total = total + valores[i];

}
media = total / 60;

void updateMedia(unsigned int leitura) {
soma += leitura;
media = soma / cont;
cont++;
}
void limpaValores() {
valores[60] = 0;

#define TEMPO_VIBRACAO 300
unsigned long inicioVibracao;
void vibrar() {
inicioVibracao = millis();
analogWrite(viber, HIGH);
}

// Verifica se é hora de parar de vibrar
void loopVibrar() {
if (inicioVibracao > 0 && inicioVibracao - millis() > TEMPO_VIBRACAO) {
analogWrite(viber, LOW);
}
inicioVibracao = 0;
}

void loop() {
if (ultimaLeitura - millis() > INTERVALO_LEITURA) {
ultimaLeitura = millis();
unsigned int leitura = analogRead(microphone);

long mil = millis();
if (mil % 1000 == 0 && mil > 0) {
if (cont > 59) {
limpaValores();
}
valores[cont] = analogRead( microphone );
now = analogRead( microphone );
Serial.print(F("Leitura recebida"));
Serial.println(leitura);

cont++;
updateMedia(leitura);
if ( media < leitura ) {
vibrar();
}
}

if ( media < now ) {
analogWrite( viber, HIGH );
}
//Serial.println(total);
Serial.println(media);
Serial.println(now);
// Serial.println(mil);
loopVibrar();
}