DallasTemperature or OneWire Library slowing down the clock of NANO

Just trying to make the code as simple as possible. Tell me if i should take a different approach. If yes what should it be. Do i need to use timer interrupts?

Do you know what a millis-timer is ?
Start here: https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay.
Then you can request the temperatures from the DS18B20 for example once per 5 seconds.


bonyl
10h
#include <OneWire.h>
#include <DallasTemperature.h>

/***************** Temperature Initialization ************************************************************/
#define ONE_WIRE_BUS A4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

/***************** Datatype Initialization ************************************************************/

unsigned long currentMillis = 0;
unsigned long a;
unsigned long b;

bool RF = 1;
bool LW = 0;
bool LF = 1;
bool RW = 0;
bool MTR = 0;

int SR = 0;

int val = 0;
int val1 = 0;

void setup()
{
  sensors.begin();

  /******************* PinMode and Read/Write *******************************************************/

  pinMode(7, OUTPUT);            // Motor Output
  pinMode(6, OUTPUT);            // RF Valve
  pinMode(3, OUTPUT);            // LW Valve
  pinMode(2, OUTPUT);            // LF Valve
  pinMode(5, OUTPUT);            // RW Valve

  delay(3000);                    // Initial Delay to open waste valves
}

void loop()
{
  /************************* Read Values **************************************************************/

  val = analogRead(A7);                                            // O2
  val1 = analogRead(A5);                                           // Flow (LPM)

  /*********************** Display Values *************************************************************/

  float oxy = val * (100.0 / 511.0);                       // Convert to percentage

  float Flow = val1 * (10.0 / 511.0);                      // Convert to Flow/Minute
  
  unsigned long currentMillis = millis();

  motor1();
  Seq ();
  Valves();
  Temp();

}
void motor1()
{
  digitalWrite(7, HIGH);
}

void Seq ()
{
  if (SR == 0) {
    previousmillis = currentmillis;
    if (currentMillis-previousmillis >= 2000) {
      digitalWrite(6, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(2, LOW);
      digitalWrite(5, LOW);
      SR = 1;
    }
  }

  else if (SR == 1) {
    previousmillis = currentmillis;
    if (currentMillis-previousmillis >= 2000) {
      digitalWrite(6, LOW);
      digitalWrite(3, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(5, HIGH);
      SR = 0;
    }
  }
}
  
void Temp() {
  sensors.requestTemperatures();            
  //Serial.println(sensors.getTempCByIndex(0));
}

Okay I understood what you guys were trying to say. I was calling millis() too many times. Now I call it only once per scan cycle and store it in a variable, then use that variable. I wanna know if using timer interrups a must for this. Or is everything correct in my code now.

#include <OneWire.h>
#include <DallasTemperature.h>

/***************** Temperature Initialization ************************************************************/
#define ONE_WIRE_BUS A4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

/***************** Datatype Initialization ************************************************************/

unsigned long currentMillis;
unsigned long previousmillis;
unsigned long a;
unsigned long b;

bool MTR = 0;

int SR = 0;

int val = 0;
int val1 = 0;

void setup()
{
  Serial.begin(9600);
  sensors.begin();

  /******************* PinMode and Read/Write *******************************************************/

  pinMode(7, OUTPUT);            // Motor Output
  pinMode(6, OUTPUT);            // RF Valve
  pinMode(3, OUTPUT);            // LW Valve
  pinMode(2, OUTPUT);            // LF Valve
  pinMode(5, OUTPUT);            // RW Valve

  digitalWrite(3, HIGH);
  digitalWrite(5, HIGH);
  delay(3000);                    // Initial Delay to open waste valves
  previousmillis = 0;
}

void loop()
{
  /************************* Read Values **************************************************************/

  val = analogRead(A7);                                            // O2
  val1 = analogRead(A5);                                           // Flow (LPM)

  /*********************** Display Values *************************************************************/

  float oxy = val * (100.0 / 511.0);                       // Convert to percentage
  Serial.println(oxy);
  float Flow = val1 * (10.0 / 511.0);                      // Convert to Flow/Minute
  Serial.println(Flow);

  currentMillis = millis();
  motor1();
  Seq ();
  Temp();

}
void motor1()
{
  digitalWrite(7, HIGH);
}

void Seq ()
{
  if (SR == 0 && currentMillis - previousmillis >= 4000) {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(5, LOW);
    previousmillis = currentMillis;
    SR = 1;
  }

  else if (SR == 1 && currentMillis - previousmillis >= 4000) {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(5, HIGH);
    previousmillis = currentMillis;
    SR = 0;
  }
}

void Temp() {
  sensors.requestTemperatures();
  //Serial.println(sensors.getTempCByIndex(0));
}

Final code, what i was able to write.

Why use a function motor1() if all it does is set pin 7 high on each loop iteration? Couldn't you just set that pin high in setup() and be done with it? In fact, if it's high all the time anyway, why control it from the Arduino in the first place?

Why are int val and int val1 global, and in fact why use them at all?
float oxy = (float)analogRead(A7) * (100.0 / 511.0);
Already does it, right?

In Seq, why have a and b for storing millis() if they're never used parallel? You could limit the function to having 1 if-statement for the 2 second timing.

Perhaps you have simplified to the point of this program not being functional anymore to begin with, but I'm kind of confused by the measurement of flow, oxygen and temperature if they're not actually used to control anything.

Really? It still looks like rubbish but, if it works, it works. Just don't show it to your mother.

I just fail to understand why it is all so complicated. I thought it might be because your subroutines vary in time. Apparently not, but who would actually know? I submit that, typically, the time taken for a loop to go round should be determined by the code in the loop. That's what loops do, anybody can understand what's going on and, ergo, if all the subroutines called were actually devoid of code, the loop would still keep time. There is no time control in your loop whatsoever, which just makes the whole thing look like shit. Maybe I am missing the point, and loop time is actually immaterial, but the mere presence of DS18B20s suggests it is very material. I recognise that there is probably some sort of time control in void Seq, and I guess that is all that is needed, since you maintain it works.

Most likely not. A purist might use a millis timer in the loop, but I suspect that, if you simply put a delay(2000) in the loop and got rid of everything to do with millis, it would probably work. All you seem to need to address is the flag state of SR.

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