0

I am trying to combine codes for my 3 sensors: GSR, Pulse sensor, and ECG but I keep getting errors. I have a poor background in coding. I have the 3 codes separately and I'm not the one who wrote them. Here are the new Sketch after Combining the loops and the setups.I didn't get any error after uploading and verifying but I am not getting values in my Serial Monitor

Combined sketch:

<!-- language-all: lang-cpp -->

#include <Wire.h>
#include "rgb_lcd.h"
#define ANALOG_IN 2   // analog input pin to display
#define OUTPUT_PIN 10 // test signal output pin
#define PULSE_MS 250  // pulse width in ms

int PulseSensorPurplePin = 1;        // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
int LED13 = 13;   //  The on-board Arduion LED
int Signal;                // holds the incoming raw data. Signal value can range from 0-1024
int Threshold = 550;            // Determine which Signal to "count as a beat", and which to ingore. 

const int BUZZER=3;
    const int GSR=A0;
    int threshold=0;
    int sensorValue;
    rgb_lcd lcd;

long ms;        // timer in ms
int out = HIGH;     // output level


void setup() {
   pinMode(LED13,OUTPUT);         // pin that will blink to your heartbeat!
   Serial.begin(9600);         // Set's up Serial Communication at certain speed. 

   long sum=0;
      //VERIFY VALUE
      lcd.begin(16, 2);
      Serial.begin(9600);
      pinMode(BUZZER,OUTPUT);
      digitalWrite(BUZZER,LOW);
      delay(1000);
//VERIFY VALUE
      for(int i=0;i<500;i++)
      {
      sensorValue=analogRead(GSR);
      sum += sensorValue;
      delay(5);
      }
      threshold = sum/500;
       Serial.print("threshold =");
       Serial.println(threshold);

       pinMode(OUTPUT_PIN, OUTPUT);    // set output pin mode
       Serial.begin(19200);        // initiate serial communication
       digitalWrite(OUTPUT_PIN, out);  // set initial digital output
       ms = millis();          // query current time (in ms)

}

void loop() {
  Signal = analogRead(PulseSensorPurplePin);  // Read the PulseSensor's value. 
                                              // Assign this value to the "Signal" variable.

   Serial.println(Signal);                    // Send the Signal value to Serial Plotter.


   if(Signal > Threshold){                          // If the signal is above "550", then "turn-on" Arduino's on-Board LED.  
     digitalWrite(LED13,HIGH);          
   } else {
     digitalWrite(LED13,LOW);                //  Else, the sigal must be below "550", so "turn-off" this LED.
   }


delay(100);

   int temp;
      sensorValue=analogRead(GSR);
      Serial.print("sensorValue=");
      Serial.println(sensorValue);
      lcd.setCursor(0, 0);
      lcd.print("GSR Value: ");
      lcd.print(sensorValue);
      temp = threshold - sensorValue;
      if(abs(temp)>50)
      {
        sensorValue=analogRead(GSR);
        temp = threshold - sensorValue;
        if(abs(temp)>50){
       digitalWrite(BUZZER,HIGH);
        Serial.println("YES!");
        delay(3000);
        digitalWrite(BUZZER,LOW);
        delay(1000);}
      }


      int val = analogRead(ANALOG_IN);    // read voltage at input pin
  Serial.write( 0xff );         // write value to serial port
  Serial.write( (val >> 8) & 0xff );
  Serial.write( val & 0xff );

  if (millis() >= ms+PULSE_MS) {    // check if pulse width has elapsed
    ms = millis();            // query current time (in ms)
    out = !out;             // invert output value
    digitalWrite(OUTPUT_PIN, out);    // update digital output
  }
}
3
  • 2
    you can have only one setup() and one loop() subroutine ... you will have to merge the three subroutines into one for each of those .... first, you need to figure out what the code does – jsotola Jan 16 '18 at 7:15
  • 2
    -1 for messing up code formatting after it was corrected. i will remove the downvote if you correct the formatting – jsotola Jan 16 '18 at 16:48
  • @jsotola - isn't that blackmail :) – Code Gorilla Jan 18 '18 at 14:29
0

In general:

  • There can be only one setup, in most cases you can put the code from all three setup's together, but checkout the code does not interfere, i.e. does not use the same pins. In that case, change the pin numbers if possible (and change the circuit accordingly).

  • For the loops it can be more tricky:

    • Check how often each loop should be called. If one loop costs e.g. 500 ms that means the other loops will be called less often, which can affect the precision or even good readout of the sensor.
    • If all loops are relatively small and the duration do not matter, you can just call them after each other (rename the loops and call them one by one).
    • If there is a time constraint, than split the lengthy loop in a small part, and call it whenever needed. There is no clear rule for this.

What you can try is:

  • Check for reach loop how long it takes
  • Than for each loop add a delay with same time as the other loops, thus:
    • Test the duration (average) of loop 1, than of loop 2, than of loop 3. Assume loop1 takes 100, loop2 200 and loop3 300 ms.
    • Test loop1 without calling loop2 and loop3, but just add the delay (200+300=500 ms).
    • Check if it works, if so, it means you can call the other loops without problems.
    • Do the same for loop2 (with 100+300ms delay) and loop3 (100+200ms).
    • If all loops work correctly, than you can call the loop1, loop2, loop3 in sequence.
    • If not, you have to call the loop(s) that do not work either more often or in a different way.
2
  • 1
    Yep all that and use functions to break down you code into manageable chunks. If its longer than 10 lines, or you do it more than once maybe it ought to be a separate function – Code Gorilla Jan 18 '18 at 14:30
  • 1
    @CodeGorilla good addition ... it also will make readability, efficiency, maintainability and testing easier to have small functions. – Michel Keijzers Jan 18 '18 at 14:52

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