Using values from one sketch in another sketch

I am using a total of four programs for a covid screening device. one program that runs a temperature sensor and outputs a one or a zero depending on if the user has a fever or not. the next program runs a pulse oximeter and outputs a zero or a one depending on the users oxygen saturation. the third program is a smell test that is based on a keypad input and similarly outputs either a zero or one. these values are currently integers. the fourth program needs to utilize the previous three values to determine if the user passes or fails the overall evaluation. how do i get the pass fail values from the first three programs into the fourth one. any help would be much appreciated

HI, you didn't say what platform you are using.
If it is Arduino, you can save the values in the EEPROM.
One different EEPROM address for each parameter.

RV minierin

Typically something like this is done by one program with four sections. Look around, you'll see ppl doing many things seemingly at once on just one Arduino board.

So your problem is combining the four programs into one, that is assuming that combined they do not exhaust the resources of your processor.

Post all four of your programs. Search for how to combine Arduino sketches,.

But I am actually replying because you simply must tell us all more about this:

a7

Separate programs suggest separate devices working in different places at the same time. In this case either the digital outputs are connected to input pins of the master controller, or some other protocol (I2C, SPI...) is used for inter-processor communication.

But if those "programs" are only subroutines running interleaved or subsequently on the same controller there is no need for data transmission. The results are deposited in global variables accessible to the master process.

so currently I have 3 separate programs running on three different Arduinos. the temperature and pulse oxygen could probably be performed in one program on one Arduino if necessary. I believe I could make this one large program and use i2c to use a second Arduino to perform the smell test if that would be easiest. welcome to any and all ides and thanks for all the intrest

As @DrDiettrich points out, and now that we know you have multiple processors, it seems like yes/no or one/zero “communication” would simply be hooking digital outputs to digital inputs across boards.

But srsly, WTF is a smell test and how does it result in 1 or 0?

a7

It will actually be easier and more practical to use one "Arduino" board to do all the functions, as long as the functions are not in separate locations (more than say, half a meter apart).

But you need to fully describe the system to get actual recommendations.

so the smell test works by randomly triggering one of three servos dispensing a corresponding scented hand sanitizer. then it performs a multiple choice quiz about what it smells like and if the user answers correctly it outputs a one and if they answer incorrectly it outputs a zero. the reason that I am using more than one Arduino is that I need more pins than are available on a single uno r3. thanks for the interest I am new to Arduino and any information helps.

1 Like

Wow, a clever idea to identify possible loss of sense of smell :slight_smile:

Hello
In this magic project I would start with a basic engineering to identify the amount of I/O pins first.
In the second step I would arrange all neccessary sketches in different tabs inside the IDE. The sketch in tab1 contains a interprocess communition struct, the setup() for all other sketches and finaly a event- and/or time contolled FSM to run the loop() for the sketches located in the other tabs. This procedure requieres a clean programming for the common used infrastructure of the Arduino.
At this project stage I recommend the ussage of an Arduino Mega.

A UNO is a poor choice for a serious project; a Nano is far more practical. The UNO or Mega's only virtue is when you have a "shield" that plugs onto it and performs all the functions you require.

If you need more I/O pins, it is a bad idea to add another Arduino.

You just use one or more (up to 8) port expanders connected to the two I2C pins:


hello all. so I found a elegoo mega2560 R3. it has all the pins I need do y'all see any difficulties I would have in converting to a mega from 3 separate unos.

No, I see no difficulties.

Of course I see no code either, so it's hard to say.

Many ppl come here to combine multiple sketches.

Three naive sketches might be a real challenge, search for "combine arduino sketches" on the internet or amongst theses fora.

Three sketches written by someone who thought they might end up being combined one day would be less of a challenge, much less.

So… like @alto777 said above in Reply #3

"Post all four of your programs. Search for how to combine Arduino sketches."

And we can see how big a problem you have. it is entirely possible and perhaps defensible that it would actually be easier to use multiple processors.

All depends on your budget time and money, your aesthetic sense of what's good and bad and most important what kind on fun you want to have.

I'd go with one processor but that's me.

a7

hello all. i have got everything working on the mega and now all that is left is to get the three codes working together. i currently have a few ideas about how to do this. combine them together into one? or utilize what paul talked about

I would arrange all neccessary sketches in different tabs inside the IDE. The sketch in tab1 contains a interprocess communition struct, the setup() for all other sketches and finaly a event- and/or time contolled FSM to run the loop() for the sketches located in the other tabs.

Or any other ideas that yall have would be much appreciated i will attach all of my code below.
first the pulse oximeter

/*
  Optical SP02 Detection (SPK Algorithm) using the MAX30105 Breakout
  By: Nathan Seidle @ SparkFun Electronics
  Date: October 19th, 2016
  https://github.com/sparkfun/MAX30105_Breakout

  This demo shows heart rate and SPO2 levels.

  It is best to attach the sensor to your finger using a rubber band or other tightening
  device. Humans are generally bad at applying constant pressure to a thing. When you
  press your finger against the sensor it varies enough to cause the blood in your
  finger to flow differently which causes the sensor readings to go wonky.
*/

#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"

MAX30105 particleSensor;

#define MAX_BRIGHTNESS 255

#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
//Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
//To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100];  //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100];  //red LED sensor data
#endif

int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid

int pulseLED = 11; //Must be on PWM pin
int readLED = 13; //Blinks with each data read
int pass;
int count;



void setup()
{
  static int count = 0;
  Serial.begin(115200); // initialize serial communication at 115200 bits per second:

  pinMode(pulseLED, OUTPUT);
  pinMode(readLED, OUTPUT);


  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println(F("MAX30105 was not found. Please check wiring/power."));
    while (1);
  }

  int ledBrightness = 60; //Options: 0=Off to 255=50mA
  int sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
  int ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  int sampleRate = 100 ; //Options: 50, 100, 200, 400, 800, 1600, 3200,
  int pulseWidth = 411; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384

  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings

  bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps
  for (int32_t spo2 = -999; spo2 < 100 && 95 < spo2 ; spo2++); //smooting average for spo2
  //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  //Continuously taking samples from MAX30102. SpO2 are calculated every 1 second


  for (int c = 0; c < 7; c++)
  {
    int count = count + 1;
    Serial.print(count);
    for (int i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }

    //take 25 sets of samples before calculating the heart rate.
    for (int i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data

      //digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read

      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample(); //We're finished with this sample so move to next sample
      if (spo2 >= 90) {
        Serial.print(F(", Pass/Fail="));
        int pass = 1;
        Serial.print(pass);
      }
      else {
        Serial.print(F(", Pass/Fail="));
        int pass = 0;
        Serial.print(pass);

      }

      Serial.print(F(", SPO2="));
      Serial.print(spo2, DEC);

      Serial.print(F(", SPO2Valid="));
      Serial.println(validSPO2, DEC);
    }


    maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  }
}

void loop()
{

}

next the temperature sensor

#include <Adafruit_LiquidCrystal.h>

/* This code works with MLX90614 (GY906) and LCD ic screen
 * It measures both ambient and object temperature in Celsius and display it on the screen
 * Please visit www.surtrtech.com for more details
 */
#include <Adafruit_LiquidCrystal.h>
#include <LiquidCrystal.h>     
#include <Wire.h>
#include <Adafruit_MLX90614.h>

#define BACKLIGHT_PIN 23 // Declaring LCD Pins
#define En_pin 22
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 24
#define D5_pin 25
#define D6_pin 6
#define D7_pin 7
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
//int readings[Range];       // the readings from the analog input
int Index = 0;             // the index of the current reading
int sum = 0;               // the running total
int average = 0;           // the average
int value = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  
  Serial.begin(9600);
  lcd.begin(16,2);            //sets up number of columns, rows:
  lcd.print("Temperature");  // prints to lcd
  mlx.begin();               //Receive data from the sensor

}

void loop() {
 lcd.setCursor(0,1);      //sets cursor on second row column one.... a 1 denotes the second row as counting starts at 0:
 lcd.print(mlx.readObjectTempF() );    // prints objecty temp:
 Serial.println(mlx.readObjectTempF());
 if (mlx.readObjectTempF()> 93.5 && mlx.readObjectTempF() < 99.4){
      Serial.println(1);
      //int subjecttemp = 1;
    }
 else{
      Serial.println(0);
      //int subjecttemp = 0;
    }
 delay(1000);

}

finally the smell test

#include <Servo.h>
#include <Keypad.h>


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad smelltestkeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int ranNum;
Servo servo1;
Servo servo2;
Servo servo3;
int pos = 0;

void setup() {
  
  Serial.begin(9600);
  randomSeed(analogRead(0));

  servo1.attach(10);
  servo2.attach(11);
  servo3.attach(12);
}
void loop() {  

  ranNum=random(1,4);

if (ranNum <= 1) {
Serial.println("scent One is dispensed ");
Serial.println("select 1 if it smells like cherry cough syrup ");
Serial.println("select 2 if it smells like garlic ");
Serial.println("select 3 if it smells like lemon ");
Serial.println("select 4 if it smells like chocolate ");
delay(5000);

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }  

Serial.println("waiting for response ");
int waitforresponse= smelltestkeypad.waitForKey();
Serial.println("response received ");
Serial.println(waitforresponse);
if (waitforresponse == 49){
  Serial.println("correct response");
  int smelltestresponse = 1;
}
else {
  Serial.println("incorrect response");
  int smelltestresponse = 0;
}
}
else if (ranNum >= 3) {
Serial.println("scent Three is dispensed ");
Serial.println("select 1 if it smells like fish ");
Serial.println("select 2 if it smells like mustard ");
Serial.println("select 3 if it smells like mint candy cane ");
Serial.println("select 4 if it smells like coconut ");
delay(5000);

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }  

Serial.println("waiting for response ");
int waitforresponse= smelltestkeypad.waitForKey();
Serial.println("response received ");
Serial.println(waitforresponse);
if (waitforresponse == 51){
  Serial.println("correct response");
  int smelltestresponse = 1;
}
else {
  Serial.println("incorrect response");
  int smelltestresponse = 0;
}

}

else {
Serial.println("scent Two is dispensed ");
Serial.println("select 1 if it smells like lavander ");
Serial.println("select 2 if it smells like vanilla ");
Serial.println("select 3 if it smells like coffee ");
Serial.println("select 4 if it smells like oranges ");
delay(5000);

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }  

Serial.println("waiting for response ");
int waitforresponse= smelltestkeypad.waitForKey();
Serial.println("response received ");
Serial.println(waitforresponse);
if (waitforresponse == 50){
  Serial.println("correct response");
  int smelltestresponse = 1;
}
else {
  Serial.println("incorrect response");
  int smelltestresponse = 0;
}
}

delay(5000);

}

can you please elaborate more on this. i think this could work for my situation but i don’t t fully understand how this would work.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.