I'm using two sensors, which is Galvanic Skin Response sensor and Heartrate sensor. The problem is that when I combined both of the codes, the only output that is showing up is from Heartrate only which is at A0. If I run them separately both codes works perfectly fine. My background in coding is basic and both codes came from the internet.

These are the codes:

    #define BLYNK_PRINT Serial
    #include <ESP8266_Lib.h>
    #include <BlynkSimpleShieldEsp8266.h>
    #include <SoftwareSerial.h>
    SoftwareSerial EspSerial(2, 3); // RX, TX

    #define samp_siz 4
    #define rise_threshold 4

    // Your ESP8266 baud rate:
    #define ESP8266_BAUD 9600

    char auth[] = "ba4e5bd1bd9549669a02ac378db9faf6";
    char ssid[] = "vivo";
    char pass[] = "takoyaki";

    /*char auth[] = "b3f01a592b78471f9f720b09d2ff8047";
    char ssid[] = "vivo";
    char pass[] = "takoyaki";
    ESP8266 wifi(&EspSerial);


    BlynkTimer timer;

    // Pulse Monitor Test Script

    void setup() {

      // Set ESP8266 baud rate

      Blynk.begin(auth, wifi, ssid, pass);

      pinMode(A0, INPUT);
      pinMode(A1, INPUT);

      //timer.setInterval(1000L, myTimerEvent);     //send the arduino's                         uptime
      timer.setInterval(350L, sensor);            //send gsr data every         0.35s

    /*void myTimerEvent()
      // You can send any value at any time.
      // Please don't send more that 10 values per second.
      Blynk.virtualWrite(V4, millis() / 1000);
    void sensor(){

      const int GSR=A1;
      int inputvalue=0;
      int gsr_average=0;
      long total=0;
      for(int j=0;j<10;j++)                       //Average the 10                 measurements to remove the glitch
          total += inputvalue;
       gsr_average = total/10;
       Blynk.virtualWrite(V6, gsr_average);

    void heartrate ()
        int sensorPin =A0;
        float reads[samp_siz], sum;
        long int now, ptr;
        float last, reader, start;
        float first, second, third, before, print_value;
        bool rising;
        int rise_count;
        int heartrate;
        int n;
        long int last_beat;

        for (int i = 0; i < samp_siz; i++)
          reads[i] = 0;
        sum = 0;
        ptr = 0;

          // calculate an average of the sensor
          // during a 20 ms period (this will eliminate
          // the 50 Hz noise caused by electric light
          n = 0;
          start = millis();
          reader = 0.;
            reader += analogRead (sensorPin);
            now = millis();
          while (now < start + 20);  
          reader /= n;  // we got an average

          // Add the newest measurement to an array
          // and subtract the oldest measurement from the array
          // to maintain a sum of last measurements
          sum -= reads[ptr];
          sum += reader;
          reads[ptr] = reader;
          last = sum / samp_siz;
          // now last holds the average of the values in the array

          // check for a rising curve (= a heart beat)
          if (last > before)
            if (!rising && rise_count > rise_threshold)
              // Ok, we have detected a rising curve, which implies a         heartbeat.
              // Record the time since last beat, keep track of the two previous
      // times (first, second, third) to get a weighed average.
      // The rising flag prevents us from detecting the same rise more than once.
              rising = true;
              first = millis() - last_beat;
              last_beat = millis();

              // Calculate the weighed average of heartbeat rate
              // according to the three last beats
              print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 *         third);

              heartrate = print_value;

              Blynk.virtualWrite(V7, heartrate);

              third = second;
              second = first;

            // Ok, the curve is falling
            rising = false;
            rise_count = 0;
          before = last;

          ptr %= samp_siz;


    void loop()
  • If you need the codes without those blynk's command just tell me in the comment section – tako Nov 15 '18 at 18:43
  • the function heartrate() has an infinite loop, so the Uno is stuck there – chrisl Nov 15 '18 at 18:45
  • so i cannot use the code as a function ? how do i need to edit it ? – tako Nov 15 '18 at 18:53
  • 2
    The while(1) loop is the problem. Rewrite the function in a way, that it takes the reading, does somethings with it and then exits to the main loop to do something else – chrisl Nov 15 '18 at 18:56
  • Since the code keeps record of the last measurements, you will have to make these variables static or global, so that they keep their value between different executions of the function. And btw: the start variable has to be an unsigned long, since that is the type, that the millis() function returns. The same goes for variable now. – chrisl Nov 15 '18 at 19:07

I have not tested the following code, so it will be up to you to do this and handle small errors. This is more about the coarse way to change the program. And you will have to understand, what the code really does, to be able to change it the way you need it.

First you will have to make some of the variables in the heartrate() function to global variables (by writing them near the top of the sketch outside of any function), so that they keep their values between the different executions of the function. I also changed some variable types, which match the type of the assigned values.

float reads[samp_siz], sum;
unsigned long now, start, ptr;
float last, before, reader;
unsigned long first, second, third;
bool rising;
int rise_count;
float print_value, heartrate;
unsigned long last_beat;

The sensor pin should be #defined at the top of the program (by convention). Then you should put this code:

for (int i = 0; i < samp_siz; i++)
    reads[i] = 0;
sum = 0;
ptr = 0;

into the setup() function.

Now we can look at the while(1) loop. On Arduino a value of 0 is treated as the boolean false and every other value as true. So this is the same as writing while(true). The while() structure loops as long, as the expression inside the brackets evaluates to true. Since this is always the case here, it will loop forever. At this point you can just erase this line (and the corresponding closing bracket }).

After these changes the function will only take 1 reading (averaged over 20ms), then check, if a heartbeat was detected, and finally exiting to the loop() function. Since not every reading is a heartbeat, you will get more output from the sensor() function (which outputs on every execution) than from the heartbeat() function. And now, due to your functions being non-blocking, you will get very fast output. You might want to slow that down, by using a delay() or (better) the millis() function (like in the BlinkWithoutDelay example of the Arduino IDE).

Note, that this code exhibits some bad coding styles. You can get much of good code on the web, but also much of bad code. I see wrong types for variables, the usage of totally unnecessary variables and usage of the millis() function, which doesn't handle the rollover. This doesn't mean, that the code does not work. But you can get weird and difficult to debug errors through them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.