Looking for advice with my code, please.

Hello. First of all i have to apologize about my English not being too good. So the topic subject might be bad.
Anyway i hope, that someone will give me some advice, as i am struggling at the moment. Below is my piece of code. I need to read another two analog inputs and depending on difference between values of those sensors to change how long relays are on. In my case (openR, LOW) is ON. In other words if sensor A has a value 2 and sensor B has a value 5, then i want digitalWrite(openR, LOW) to be LOW for 3seconds, but if A=2 and B=3 then 1 sec.

   if (temp - 1 > foo.c  && limits < 98 ) {  
      digitalWrite(openLed, HIGH);
      digitalWrite(closeR, HIGH);   //Realy close
      delay(200);
      digitalWrite(openR, LOW);   //Relay open
    } else {
      digitalWrite(openLed, LOW);
      digitalWrite(openR, HIGH);
    }

    if (temp + 1 < foo.c && limits > 0) {
      digitalWrite(closeLed, HIGH);
      digitalWrite(openR, HIGH);
      delay(200);
      digitalWrite(closeR, LOW);
    } else {
      digitalWrite(closeLed, LOW);
      digitalWrite(closeR, HIGH);
    }

Again sorry for my English, i tried to explain my pain as good as could. Thank you for reading this.

Nothing wrong with your English.

Please post the full program so we can see things in context. For example I don't see any analogRead() in your snippet.

The use of delay() is generally a bad idea except for a short demo program. Have a look at how millis() is used to manage timing in Several Things at a Time. Also look at how the program is structured. See also Planning and Implementing a Program

It seems to me you need code something like this

if (valA > valB) {
    relayOnTime = 5;
}
else {
  relayOnTime = 1;
}

but maybe I am missing something.

...R

Please put the whole code... We have no clue what foo.c and limits are...

What you want to do is pretty simple in theory so not sure what you coded above

A = sensor A
B = sensor B
Duration = (absolute value of B-A) * 1000 // if difference represents milliseconds
Open relay
Wait for duration
Close relay

This is my full code. It should be messy as i am still working on it. Sensor A is pot for setpoint. int temp is reading from temperature sensor. At the moment, if temperature is higher than setpoint, relay is on till the temerature drops. But i want to make it on only for a while, witch will be calculated (temp - setpoint). Bigger value, longer time on.

Huhh, my brain is boiling, hope you will understand. If not i will try again.

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

#define ONE_WIRE_BUS 45 //Temp sensor pin

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);


#include <EEPROM.h>
#include <EEWrap.h>


#include <RBD_Timer.h>
#include <RBD_Button.h>

struct Foo {

  int16_e c;
};

Foo foo EEMEM;

// input pullup enabled by default
RBD::Button button(22);
RBD::Button button2(23);
int setPoint = 0;
int temp = 20;
const int openLed = 32;
const int closeLed = 33;
const int openR = 36;
const int closeR = 37;
int limits;
unsigned long previousMillis = 0;
unsigned long interval = diff;



int counter = 0;
int reading;
int current_state = LOW;
long time = 0;
int debounce_count = 50;





void setup() {

  pinMode(openLed, OUTPUT);
  pinMode(closeLed, OUTPUT);
  pinMode(openR, OUTPUT);
  pinMode(closeR, OUTPUT);
  digitalWrite(openR, HIGH);
  digitalWrite(closeR, HIGH);
  sensors.begin();
  sensors.setResolution(0, 9);

  Serial.begin(115200);

  lcd.begin(2, 16);
  lcd.clear();
  delay(100);
}

void loop() {

  unsigned long currentMillis = millis();


  if (millis() != time)
  {
    sensors.requestTemperatures();
    reading =  sensors.getTempCByIndex(0);

    if (reading == current_state && counter > 0)
    {
      counter--;
    }
    if (reading != current_state)
    {
      counter++;
    }
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = reading;
      temp = reading;

    }
    time = millis();

    lcd.setCursor(0, 1);
    lcd.print("Tmp: ");
    lcd.setCursor(5, 1);
    lcd.print(temp);
    delay(10);

    lcd.setCursor(9, 0);
    lcd.print("Stp:");
    lcd.print(foo.c);

    int limitPot = analogRead(A9);
    limits = map (limitPot, 60, 970, 100, 0);  // Pot in the gearbox



    int tempReg = analogRead(A8);
    setPoint = map(tempReg, 0, 1023, 15, 30);  //  Pot for adjusting setpoint

    lcd.setCursor (0, 0);
    lcd.print("Set:");
    lcd.setCursor (5, 0);
    lcd.print(setPoint);
    lcd.setCursor (9, 1);
    lcd.print ("Pos:");
    lcd.print (limits);

    if (button.onPressed()) {
      lcd.setCursor(0, 1);
      lcd.print("Saving      ");
      //Serial.println(setPoint);
      foo.c = setPoint;
      delay(3000);
      lcd.clear();
    }

    if (temp - 1 > foo.c  && limits < 98 ) {
      digitalWrite(openLed, HIGH);
      digitalWrite(closeR, HIGH);
      delay(200);
      digitalWrite(openR, LOW);
    } else {
      digitalWrite(openLed, LOW);
      digitalWrite(openR, HIGH);
    }

    if (temp + 1 < foo.c && limits > 0) {
      digitalWrite(closeLed, HIGH);
      digitalWrite(openR, HIGH);
      delay(200);
      digitalWrite(closeR, LOW);
    } else {
      digitalWrite(closeLed, LOW);
      digitalWrite(closeR, HIGH);
    }
  }
}

In the bottom part

     digitalWrite(closeR, HIGH);
     delay(200);
     digitalWrite(openR, LOW);

this is only to make 100% sure that closeR is off, and little delay before another one is on.

You're going to wear out your EEPROM, writing to it every 3 seconds.

I think i`m only writing on key press. And i need that to keep the setpoint when power is off.

Here:

if (button.onPressed()) { lcd.setCursor(0, 1); lcd.print("Saving "); //Serial.println(setPoint); foo.c = setPoint; delay(3000); lcd.clear(); }

Okay, what you should do, is try writing the code you have described, and post the new version.

Problem is, i have no idea, how to do that. That is why i`m here. I feel like millis should be involved, but how?

Some soldering irons using similar thing. The hotter it gets, the less time heater is on. I just cannot think how is that done.

You realize that delay() can take a variable as a parameter, right?

zarsss:
Problem is, i have no idea, how to do that. That is why i`m here. I feel like millis should be involved, but how?

Did you study the links I gave you in Reply #1?

if there is something you did not understand you need to tell me.

…R

I did. I also printed them out, that is one of the best manual, i ever seen for Arduino. What i am asking from you is not a code, but a "logic" what i need to do. It is difficult to explain, as my English is basic. Anyway, i dont want to waste your time, ill try to sort it out myself. Peace.

And thank you for great manuals.

Your English is fine - no one should ever apologize from trying to speak someone's else main language. We can probably understand what you are trying to achieve and if not we can discuss.

Let me give it a try to what I understand you are trying to achieve:

You have a device powered - let's say a soldering iron - which does not have digital temperature control and only thing you can do is turn power on or off. You want to reach a certain temperature and then try to stay at that temperature by driving the power supply.

So You want to read the temperature at the tip of the iron and have a potentiometer which would relate to the target temperature you are trying to reach.

If the the temperature at the tip of the iron is far away from the target temperature set by the potentiometer, you want to power the soldering iron for a long time so that it gets closer to the target temperature, but once you get closer to the target temperature, you want to just give short power supply input so that you don't over heat your iron.

Get i get it?

I have a temperature sensor, pot for setpoint, two relays open and close. In my code, if temperature is higher as setpoint, relay open is doing up till “limits” reach 100. The same if the temperature is lower, relay close is down to zero. I want to archive, if temperature minus setpoint is say two degrees, open relay opens say 10% of limits. If more than two degrees +20% of limits. And then stops, and waits new values from temp sensor. As i have some kind of debounce on the sensor, reading is slowed down, takes up to the minute to get new value. I cannot explain any better.

Soldering iron was like a sample, must be similar logic.

What are limits related To? The temperature or the max time the relay is activated?

Sorry, relays are activating motor with the pot on the shaft.

I'm jumping in a plane for 13h so need to turn off electronics. Back tomorrow -

So you are modifying the set-point by turning a potentiometer through the motor or is it a different potentiometer your are adjusting?

One pot is for setpoint, another is on the motor (limits).

zarsss: One pot is for setpoint, another is on the motor (limits).

You mean feedback?