Code issue, relay hammer when thermal set point reached

Hi

I have some code that I have copy and pasted from several locations,
the code works for me except the relay control hammers at about 2-3hz

the code below is related to sensors and relays. just a snippet of it

any idea how to prevent the relay from clicking multiple times when it hits 40 degrees?

please also find attached my code in txt doc

#include "OneWire.h"
//#include "Streaming.h"

const int DS18S20a_Pin = 2; //DS18S20 Signal pin on digital 2
#define MIN_TEMP 10
#define MAX_TEMP 45
#define RELAY1  4 // Relay on digital pin 4
OneWire ds(DS18S20a_Pin);

const int DS18S20b_Pin = 7;
#define MIN_TEMP2 10
#define MAX_TEMP2 45
//edit
#define RELAY2  8
OneWire dsb(DS18S20b_Pin);

const int DS18S20c_Pin = 12;
#define MIN_TEMP3 10
#define MAX_TEMP3 45
//edit
#define RELAY3  13
OneWire dsc(DS18S20c_Pin); 

----------------

digitalWrite(4,HIGH);
} else {
digitalWrite(4,LOW);
}
if (temperature2 >30) {
digitalWrite(8,HIGH);
} else {
digitalWrite(8,LOW);
}
if (temperature3 >30) {
digitalWrite(13,HIGH);
} else {
digitalWrite(13,LOW);
}
}

I was told to add that “else if” with 36 degree low off but doesn’t work…

#include "OneWire.h"
//#include "Streaming.h"

const int DS18S20a_Pin = 2; //DS18S20 Signal pin on digital 2
#define MIN_TEMP 10
#define MAX_TEMP 45
#define RELAY1  4 // Relay on digital pin 4
OneWire ds(DS18S20a_Pin);

const int DS18S20b_Pin = 7;
#define MIN_TEMP2 10
#define MAX_TEMP2 45
//edit
#define RELAY2  8
OneWire dsb(DS18S20b_Pin);

const int DS18S20c_Pin = 12;
#define MIN_TEMP3 10
#define MAX_TEMP3 45
//edit
#define RELAY3  13
OneWire dsc(DS18S20c_Pin); 

----------------

if (temperature < 40) {
digitalWrite(4,HIGH);
} else if (temperature > 36) {
digitalWrite(4,LOW);
}

if (temperature2 > 40) {
digitalWrite(8,HIGH);
} else if (temperature < 36) {
digitalWrite(8,LOW);
}
if (temperature3 > 40) {
digitalWrite(13,HIGH);
} else if (temperature < 36) {
digitalWrite(13,LOW);
}
}

please help,

my arduino boards are locked away in my project with only a usb wire sticking out for recodes…
the output from the arduino controls a opto-relay board,
the relay hammer happens about 6 to 10 times…

i was told a delay might work, but scared it will delay the other segments of my code!!!
or if delay goes inside brackets will it delay part of the code only?

perhaps can I set a minimum HIGH time? say 300 seconds, this would prevent hammer

many thanks

code.txt (11.2 KB)

Do you have the < & > the wrong way round in your snippet?

Should it be…

if (temperature > 40) {
  digitalWrite(4,HIGH);
  } else if (temperature < 36) {
  digitalWrite(4,LOW);
}

You may need to swap the HIGH/LOW values depending on the need to set the pin high on > 40 or low on > 40

if (temperature < 40) {  // sends signal if temp is less than 40
digitalWrite(4,HIGH);  // temp will ALWAYS bee less than 40
} else if (temperature > 36) {    // alters relay if temp is above 36 causes bouncing
digitalWrite(4,LOW);
}

if (temperature2 > 40) {
digitalWrite(8,HIGH);
} else if (temperature < 36) {
digitalWrite(8,LOW);
}
if (temperature3 > 40) {
digitalWrite(13,HIGH);
} else if (temperature < 36) {
digitalWrite(13,LOW);
}
}

you are causing a problem

if (temperature < 40 && temperature > 36) {

would offer you a dead zone.
what you are doing is turning the relay on, then off on the next line of code.

is would be better to just set flags in your logic

// ++++ LOGIC SECTION ++++

if (temperature < 40) {  // sends signal if temp is less than 40
condition_1 = HIGH: //  sets flag
} else if (temperature > 36) {    // sets flag is temp is above 36
condition_1=LOW;
}
if (temperature2 > 40) {  // sends signal if temp is greater than 40
   condition_2 = HIGH;
} else if (temperature < 36) {  // sends signal if temp is less than 36
  condition_2=LOW);
}
if (temperature3 > 40) {
digitalWrite(13,HIGH);
} else if (temperature < 36) {
digitalWrite(13,LOW);
}  


//  ++++ OUTPUT SECTION ++++
digitalWrite(4, condition_1);
digitalWrite(8, condition_2);
digitalWrite(13, condition_3);

if you are controlling a thing, it might help if you explain what you are trying to do.

your comment about adding a time factor might be needed as you have no hysteresis (look it up)

most control would be
if above below 38 turn on the heater
if above 40 turn it off.

that way it will not turn on and off instantly.
relay control will never give you that 'close to setpoint' ability.

there are lots of ways to add a delay
since you are controlling 3 totally different things, you might have 3 totally different processes.
then each could be separate.
if you were controlling one process, but 3 states, then adding a delay could encompass any change for some time-out period. but, if you offer a description of what you are controlling and how fast the temperatures can change.

AND describe your intention of how you thing the control is to work, there might be better answers.

Thanks for all the comments…

My Computer is on the wall, it has a red/blue lighting beneath each component and a thermal sensor

The arduino currently will use the onewire sensor info and display blue to red depending the temperature.
The arduino controls 3x sensors, 3x light strip, 3x relays
I use 2 arduino allowing a total of 6x total.

The relay output is connected to resistors which add resistance to a power supply to increase voltage
Each relay that gets triggered makes fans spin faster.
I actually desoldered a variable resistor from the psu and 2 flying leads replaced the vr, now the relay outputs control the power supply…

I notice when my switch gets triggered the relay hammers a little bit and not fast hammering basically on off on off for about half a minute changing every 1-2 seconds
It’s random really, sometimes on off on off a few times

I would be happy to have some code added which says… Let’s leave the fans on for 5minutes minimum once a trigger is on.
This should also allow proper cool down for the system haha

Thanks for help

"
I would be happy to have some code added which says... Let's leave the fans on for 5minutes minimum once a trigger is on.
This should also allow proper cool down for the system haha"

You can do that fairly easily (you need to blink without delay tutorial too). What you do is set a counter to some non-zero count down value (whatever your time desired is). You have a timer running on interrupts that decrements that counter for each tick. Your code for the relay does not change state until the counter gets down to zero (and the decrementer in the ISR decrements it down to zero). That way, when you change state, it will stay in that state until the timer gets to zero. I did that basic thing with my old hot tub controller - hysteresis to keep it from going on and off (or trying to) too rapidly, then a countdown timer that kept it in whichever state it was for a minimum of 5 minutes so the pump/heater were not cycling too often. Worked well until I lost a bearing/seal in the pump and got water in the motor etc eating the motor bearings. Sigh.

Good morning everyone,

I had a look at that tutorial,

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Am I to assume that I would not need the 'else if' segment on my code, I can go back to the original just using 'else'

And the code in the tutorial this part below is what controls the timing?
I would need to set interval at 300000 (300 seconds)

I would need to use the same methology
And use something like this

I just don't understand how this sets a 5 minute minimum on?

unsigned long previousMillis = 0;        
   // will store last time LED was updated
const long interval = 300000;           
   // interval at which to blink
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
previousMillis = currentMillis;
if (temperature2 > 40) {
digitalWrite(8,HIGH);
} else {
digitalWrite(8,LOW);

Am I to assume that I would not need the 'else if' segment

I don't see an "else if" segment, so I can't comment on your assumption.