20 de junho de 2017

Pulse Sensor Arduino

 I was trying to find a pulse sensor code that worked well on my Arduino UNO. I found some complex codes that are difficult to understand and to customize. Some of them were not precise on measuring BPMs. Then I found a way to simplify those codes and I came up with the code I'm about to describe here.

Basically, the signal that comes from the sensor varies from 0-1024 and it's very hard to sense a beat since the signal varies a lot. So, I reduced the scale from 0-1024 to a 0-1 scale by dividing by 512. I noticed that everytime a pulse was sensed it was higher than 512 on the first scale, which will give me 1 on the second scale.
Because there was a lot of false reads, or noises, the sensor would hit the number 1(2nd scale) once in a while. Then created a counter that reduce that noice when counting beats. Everytime this counter is above or equals 4, it'd be probably a real beat.

Sorry, english is not my first language but I hope it helped you.
If you have any questions, feel free to ask me leaving a comment bellow.

//  Variables
int PulseSensor = 0;                 //Pulse sensor is connected to the '0' analog pin
int Signal;                          //Signal Received from the sensor
int base = 0;                        //Base to the measurement
int beat = 0;                        //Number of beats
unsigned long time = 0;              //Current time in milliseconds
int bpm = 0;                         //BPM
int SignalPrev = 0;                  //Armazena o sinal coletado anterior ao atual
int counter = 4;                     //Number of variations that will be despised



void setup() {
   Serial.begin(9600);               // Set's up Serial Communication at certain speed.  
   pinMode(13, OUTPUT);
}

void loop() {

  Signal = analogRead(PulseSensor);                             // Reads the sensor
  Signal = Signal/512;                                          // Reduce the 1024 value scale to 0-1 scale
        
   if(Signal > base && SignalPrev ==0 && counter>=4){           //If There's a pulse and the previous signal is 0
        digitalWrite(13, HIGH);
        counter = 0;                                            // and the number of the signs despised is 4, then
        beat++;                                                
        SignalPrev = Signal;
        
     if (checkTime(time)){                                      //True if 1 minute has passed
        bpm = beat;       
        }  
        Serial.println(bpm);                                    //Prints the BPM value
        beat = 0;                                               // Resets the values
        bpm = 0;
        
     }
   
   }else {                                                                          //Senão, despreze o valor lido e adicione 1 ao
     SignalPrev = Signal;                                                       //número de valores desprezados
     counter++;
     digitalWrite(13, LOW);
   }
delay(100);                                                                         //Atrase a leitura do sinal em 100ms
}

//check if 1 minute has passed
   boolean checkTime (unsigned long temp){
     unsigned long temp2= millis();


      if((temp2-time)>=(60000)){
        time = millis();   
        return true;
       }
  return false;
  }

16 de junho de 2017

Guerra

Pensei que a luta maior fosse comigo mesmo
e que a inocência era um atributo louvável.
Mas não nesse mundo, não com as pessoas que aqui habitam.
Por causa de um ou outro ego pestilento e punhetável.

Pensei que a guerra fosse só no interior,
mas com esse jogo de imitação que é a vida social
nada há de novo e nem de aproveitável.
Nada de bom em ser um sujeito normal.

Tudo isso de requerer aceitação mexe com a mente.
Por que esperar que outros concordem com minhas ideias
se eu e o outro somos pessoas diferentes?
E nessa de encaixar-se com o que já existe
limitam-se os potenciais criativo e evolutivo de cada um inerentes.