Help combining digital and analog code in arduino programming,

So I’m making a device that does 3 things,

  1. Measures frequency of an audio signal through a microphone
  2. Checks whether there is a weight placed on a pressure pad (my pressure sensor works through the resistance of the material changing when a weight is placed on it)
  3. Measure ambient room temperature

I have coded each individual section and tested them individually to find them work great, here are the sketches.

Frequency sketch :

// runs every time thru the loop
  setInputState();

  // count every transision HIGH<->LOW
  if (inputState != lastInputState) {
    count++;  
    lastInputState = inputState;
  }

  if (millis() - previousCountMillis >= countMillis) {
    previousCountMillis += countMillis;

    // show Hz on Serial too if available
    Serial.print(count); 
    Serial.println(" Hz");
    ledtest = count;
    // reset to zero for the next half second's sample
    count = 0L;
  }
  if (ledtest >= 400 && ledtest <= 850){
    digitalWrite(A2, HIGH);
  }
  else if (ledtest < 400 || ledtest > 850){
    digitalWrite(A2, LOW);
  }
}

Here is the pressure sensor sketch

int Vin = 5;
float Vout = 0;
float raw1 = 0;

int R1 = 770;

int R2old1 = 0;

int R2new1 = 0;

int R2avg1 = 0;

int pressurelog = 0;

void setup() {
  pinMode(A0, INPUT);             //Receive pressure voltage
  pinMode(A1, OUTPUT);            //Red led, PRESSURE PAD
}

void loop() {
  raw1 = analogRead(A0);
  Vout = raw1*5/1024;
  R2old1 = Vout*R1/(Vin-Vout);

  raw1 = analogRead(A0);
  Vout = raw1*5/1024;
  R2new1 = Vout*R1/(Vin-Vout);

  R2avg1 = (R2new1+R2old1)/2;

  Serial.print("Resistance: ");
  Serial.println(R2avg1);

  if (R2avg1 >= 1000){
    digitalWrite(A1, LOW);
    pressurelog = 0;
  }
  else if (R2avg1 <= 1000){
    digitalWrite(A1, HIGH);
    pressurelog = 1;
  }  
}

Here is the temperature code

float raw = 0;
float tempC = 0;
float tempF = 0;

void setup() {
  pinMode(A5, INPUT);
  pinMode(A1, OUTPUT);
  Serial.begin(9600);
}

void loop() {
 raw = analogRead(A5) * 0.004882814;
 tempC = (raw - 0.5) * 100.0;
 tempF = (180.0/100.0)*tempC + 32.0;

 Serial.print("temp: ");
 Serial.println(tempF);
 delay(1000);
}

I coupled the “void loop” of all three and defined everything and ran the code to give a peculiar output. It does the temp and pressure reporting just fine but seems to constantly skip the frequency measurement. It seems to like doing the pressure and temperature measurement like 50 times then do an audio measurement which doesn’t make sense as it should do it sequentially.

I TRIED:

  1. Adding a delay between the frequency measurement and the other two. This didn’t work as now the audio measurement work, as it relies on the millis () function
  2. Adding the 3 components in 3 different functions and calling them sequentially. This results in the exact same stupid output.

Does anyone have an idea or solution?

Combined code

int Vin = 5;
float Vout = 0;
float raw1 = 0;

int R1 = 770;

int R2old1 = 0;

int R2new1 = 0;

int R2avg1 = 0;

int pressurelog = 0;



const byte inputPin = 2;

boolean inputState = false;
boolean lastInputState = false;
long count = 0L;
int ledtest = 0L;

unsigned long previousCountMillis = millis();
const long countMillis = 500L;

void setInputState() {
  inputState = digitalRead(inputPin);
}

int crylog = 0;



float raw = 0;
float tempC = 0;
float tempF = 0;

int templog = 0;

void setup() {
  pinMode(inputPin, INPUT);       //Receive microphone data
  pinMode(A0, INPUT);             //Receive pressure voltage
  pinMode(A1, OUTPUT);            //Red led, PRESSURE PAD
  pinMode(A2, OUTPUT);            //Blue led, FREQUENCY/CRYING
  pinMode(A3, OUTPUT);            //Green led, TEMPERATURE
  pinMode(A4, OUTPUT);            //Red led, MAX WARNING
  Serial.begin(115200);
}

void loop() {
  Serial.println("HI");
  Cryometer();
  delay(2);
  Serial.println("YO");
  Pressure();
  Temperature();

  // WARNING SETUP
  if (pressurelog == 1 && templog == 1){
    digitalWrite(A4, HIGH);
  }
  else {
    digitalWrite(A4, LOW);
  }
}

void Pressure() {
  raw1 = analogRead(A0);
  Vout = raw1*5/1024;
  R2old1 = Vout*R1/(Vin-Vout);

  raw1 = analogRead(A0);
  Vout = raw1*5/1024;
  R2new1 = Vout*R1/(Vin-Vout);

  R2avg1 = (R2new1+R2old1)/2;

  Serial.print("Resistance: ");
  Serial.println(R2avg1);

  if (R2avg1 >= 1000){
    digitalWrite(A1, LOW);
    pressurelog = 0;
  }
  else if (R2avg1 <= 1000){
    digitalWrite(A1, HIGH);
    pressurelog = 1;
  }  
}

void Cryometer(){
  // runs every time thru the loop
  setInputState();

  // count every transision HIGH<->LOW
  if (inputState != lastInputState) {
    count++;  
    lastInputState = inputState;
  }

  if (millis() - previousCountMillis >= countMillis) {
    previousCountMillis += countMillis;

    // show Hz on Serial too if available
    Serial.print("Frequency: ");
    Serial.print(count); 
    Serial.println(" Hz");
    ledtest = count;
    // reset to zero for the next half second's sample
    count = 0L;
  }
  if (ledtest >= 400 && ledtest <= 850){
    digitalWrite(A2, HIGH);
    crylog = 1;
  }
  else if (ledtest < 400 || ledtest > 850){
    digitalWrite(A2, LOW);
    crylog = 0;
  }  
}

void Temperature() {
   raw = analogRead(A5) * 0.004882814;
   tempC = (raw - 0.5) * 100.0;
   tempF = (180.0/100.0)*tempC + 32.0;

   Serial.print("Temperature (F): ");
   Serial.println(tempF);
   Serial.println();

  if (tempF >= 75 && ledtest <= 50){
    digitalWrite(A3, HIGH);
    templog = 1;
  }
  else if (ledtest < 50 || ledtest > 75){
    digitalWrite(A3, LOW);
    templog = 0;
  }
}
delay(2);

Don't do that! Also don't print so much to Serial. Even at 115200 baud, your are only sending a few characters per millisecond so your whole loop will get stuck waiting for the serial printing to be transmitted.

You see this section...?

  if (millis() - previousCountMillis >= countMillis) {
    previousCountMillis += countMillis;

This is what stops the counter from printing all the time. It only prints twice per second. You need to add the same kind of counter to each of the other functions that prints something.

It is usually better to combine similar operations into a single function. Do all the inputs in one function (maybe it calls other functions) and do all the printing in another. That way you can collect lots of readings very fast and accurate and each printout has a consistent set of readings all taken at one time.

void loop() {
  readInputs();
  doCalculations();
  doPrintout();
}

Each one of these functions may have its own timer. For example, you probably don't need to analogRead() quite as fast as you're reading input pulses. You definitely don't need to print as often. The "previousMillis" variable can be local to the function, just with the static modifier so it stores the value permanently.