Re: Problem with this sketch

Hi everyone, I’m a new member and I need the help of someone who know better than me arduino. I’ve write this sketch but There’s a problem pin 8 (uzd_pin) and pin 4 (doz_pin) do not switching off completely when temp1 is >25 and flame are low.what is wrong. Help me please somebody.

multitask_bandymas3.ino (4.99 KB)

Why do you post in this thread? Get your own one and attach the code in the post using code tags (read how to use the forum)

 if(temp1<=22 && flame,HIGH)

What is this rubbish supposed to do? Have you ever seen an if statement with commas in it?

Hello everyone. why are you so angry people? I'm new to this area. I just asked for advice, not give me my own wealth. but one bump is that I'm using the forum incorrectly, and the other trying to make me idiot, I think that not one of yours was born by knowing everything. Thanks for the help :confused:

@bugis33, do not hijack. Thread split.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thaks.. Tom... :)

hi .someone can help with this mes. i am beginner and i have got some problems with this code and i cannot find what is wrong, pin 4 and pin 8, they doz not switching off wen temp1 rich 25 and flame is low.

#include <Fsm.h>
#define servo_pin 12
#define PWM_pin 2
#define pad_pin 3
#define doz_pin 4
#define siur1_pin 5
#define siur2_pin 6
#define pam_pin 7
#define uzd_pin 8
float temp1;
#define temp1_pin A0
 float temp2;
#define temp2_pin A1
 float temp3;
#define temp3_pin A2
int led_pin = 13 ;                                               
int flame_sensor_pin = 10;                           
int flame_pin = HIGH ;                                  

void on_doz_on_enter() {
    Serial.println("dozavimas ijugtas");
    digitalWrite(doz_pin, HIGH);
}

void on_doz_off_enter() {
    Serial.println("dozavimas isjungtas");
    digitalWrite(doz_pin, LOW);
}
void on_uzd_on_enter() {
    Serial.println("uzdegimas ijungtas");
    digitalWrite(uzd_pin, HIGH);
}

void on_uzd_off_enter() {
    Serial.println("uzdegimas isjungtas");
    digitalWrite(uzd_pin, LOW);
}
State state_doz_on(&on_doz_on_enter, NULL);
State state_doz_off(&on_doz_off_enter, NULL);
State state_uzd_on(&on_uzd_on_enter, NULL);
State state_uzd_off(&on_uzd_off_enter, NULL);

Fsm fsm_doz(&state_doz_off);
Fsm fsm_uzd(&state_uzd_off);
Fsm fsm1_doz(&state_doz_off);
Fsm fsm1_uzd(&state_uzd_off);
Fsm fsm2_doz(&state_doz_off);


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

    pinMode(doz_pin, OUTPUT);
    pinMode(uzd_pin, OUTPUT);
    pinMode(PWM_pin, OUTPUT);
    pinMode(pad_pin, OUTPUT);
    pinMode(pam_pin, OUTPUT);
    pinMode(siur1_pin, OUTPUT);
    pinMode(siur2_pin, OUTPUT);
    pinMode(temp1_pin,INPUT);
    pinMode(temp2_pin,INPUT);
    pinMode(temp3_pin,INPUT);
   pinMode(flame_pin,INPUT);
    pinMode(led_pin,OUTPUT );
   
    fsm_doz.add_timed_transition(&state_doz_off, &state_doz_on, 5000, NULL);
    fsm_doz.add_timed_transition(&state_doz_on, &state_doz_off, 10000, NULL);
    fsm_uzd.add_timed_transition(&state_uzd_off, &state_uzd_on, 10000, NULL);
    fsm_uzd.add_timed_transition(&state_uzd_on, &state_uzd_off,7000, NULL);
    fsm1_doz.add_timed_transition(&state_doz_off, &state_doz_on, 3000, NULL);
    fsm1_doz.add_timed_transition(&state_doz_on, &state_doz_off, 7000, NULL);
    fsm1_uzd.add_timed_transition(&state_uzd_off, &state_uzd_on,0,NULL);
    
}


void loop() {
  
 flame_pin = digitalRead ( flame_sensor_pin ) ;

if (flame_pin == HIGH)
{
Serial.println("nera liepsnos");
digitalWrite(led_pin,LOW);
}
else {
digitalWrite(led_pin,HIGH);
Serial.println("LIEPSNA");
}
{
delay (100);

}
   temp1 = analogRead (temp1_pin);
  temp1 = temp1 * 0.3725;
 Serial.println("KATILO TEMPRATURA = ");
 Serial.print(temp1);
 Serial.println("*C");
   temp2 = analogRead(temp2_pin);
   temp2 = temp2 * 0.3725;
 Serial.println("RADIATORIU TEMPRATURA  ");
 Serial.print(temp2);
 Serial.println("*C");
   temp3 = analogRead (temp3_pin);
   temp3 = temp3 * 0.3725;
 Serial.println("BOILERIO TEMPRATURA  ");
 Serial.print(temp3);
 Serial.println("*C");
 
 if(temp1<=20 && flame_pin,HIGH)
{
 fsm_doz.check_timer();
 fsm_uzd.check_timer();
}
else if(temp1<=20&& flame_pin,LOW)
{
 fsm1_doz.check_timer();
 fsm1_uzd.check_timer();
}
  if (temp1 < 22)
  {
  digitalWrite (PWM_pin,150); 
  Serial.println ("ventiliatorius ijungtas");
  digitalWrite (pad_pin,HIGH);
  Serial.println ("padavimas ijungtas");
  }
  else if (temp1 > 22)
  {
  digitalWrite (PWM_pin,0);
  Serial.println ("ventiliatorius isjungtas");
  digitalWrite (pad_pin,LOW); 
  Serial.println ("padavimas isjungtas");
  digitalWrite (doz_pin,LOW);
 digitalWrite (uzd_pin,LOW);
// on_doz_off_enter();
// on_uzd_off_enter();
  }

  if (temp1> 20)
  { 
    digitalWrite(siur1_pin,HIGH);
   Serial.println("radiatoriu siurblys ijungtas");
  }
  
 else
{ 
    digitalWrite (siur1_pin,LOW);
   Serial.println("radiatoriu siurblys isjungtas");
}
  if (temp3 >20&& temp1>20)
   { 
    digitalWrite(siur2_pin,LOW);
   Serial.println("boilerio siurblys isjungtas");
    }
    else if (temp3 <20&& temp1>20)
{
     digitalWrite(siur2_pin,HIGH);
   Serial.println("boilerio siurblys ijungtas");

}
  if (temp2<=20)
  {
   digitalWrite (pam_pin,HIGH);
   Serial.println ("pamaisymas mazeja");
   }
  else 
 {
  digitalWrite (pam_pin,LOW);
  Serial.println ("pamaisymas dideja");
}
{
    delay(1000);
}}
{
delay (100);

}

{Why} {are} {there} {useless} {curly} {braces} {here} {?}

Why
is
your
code
so
piss-poorly
indented?

Use Tools + Auto Format BEFORE posting code again.

 if(temp1<=20 && flame_pin,HIGH)

Why have you not explained what this is supposed to do? It does NOT, but without knowing what you think it should do, we can’t help you fix the code.

  if (temp1 < 22)
  {
  digitalWrite (PWM_pin,150);
  Serial.println ("ventiliatorius ijungtas");
  digitalWrite (pad_pin,HIGH);
  Serial.println ("padavimas ijungtas");
  }
  else if (temp1 > 22)

Why is there no code to deal with temp1 equal to 22?

With your code in the IDE, press ctrl-T to use the autoformat tool that will try to indent the code for better reading and not be like a trick maze for the eyes and mind to solve while trying to see what is wrong with the logic itself. It should help you see easier.

I don't know why you use fsm.h. I can only guess it is for timers? But I don't know what it does and don't want to dig to find out.

I see the condition if temp1 > 22 that turns pins 4 and 8 off but I'm not entirely sure what else must be true or false for that condition to even run. It looks like it should at a quick glance but if it's not then something is stopping it or something else is turning those pins back on perhaps very quickly.

Sometimes drawing diagrams on paper can help organize your view. Drawing a map of what you actually have may show you where the missing or extra or wrong part(s) is.

I don't know why you use fsm.h. I can only guess it is for timers?

FSM = Finite State Machine

Why there is a class for something so simple amazes me.

@bugis33, do not cross-post. Threads merged.

PaulS: FSM = Finite State Machine

Why there is a class for something so simple amazes me.

It seems a bit like having a for-next library. No, I'm not going to look because I might find one for those too.

I saw errors in the code and I could guess about what it's supposed to control but the logic is not well organized. Ordinarily my motto is "the code is the comments" but in this case it tells me about as much as a Trump speech.

The use made of that li9brary seems limited to timers though I dunno if they're repeaters or one-shots and frankly I'm not gonna dig into that library to find out.

Thank you for responding to my request for help. I want to control the 8 channel relay block with this code using 3 thermocouples and a flame sensor. It should be noted that when the temp1 does not reach the specified temperature and the flame sensor is inactive, pin_4 should be operated at intervals of 5 and 10 seconds and pin_8 at intervals of 7 and 10 seconds, and when the flame sensor is activated and the temperature is still lower than the specified pin_8, it should be LOW and pin_4 should be in range 3 and 7 seconds. when it reaches the highest specified temperature pin_4 and pin_8 to be LOW, but every 20 seconds they get “HIGH” for about a quarter of a second
I use the FSM for the timer.
those curly brackets are just a mistake. I’ve made some changes to my colleague comments, but no one changes.

#include <Fsm.h>
#define servo_pin 12
#define PWM_pin 2
#define pad_pin 4
#define doz_pin 3
#define siur1_pin 5
#define siur2_pin 6
#define pam_pin 8
#define uzd_pin 7
float temp1;
#define temp1_pin A0
float temp2;
#define temp2_pin A1
float temp3;
#define temp3_pin A2
#define led_pin 13
#define flame_sensor_pin 10
int flame_pin = HIGH;

void on_doz_on_enter() {
  Serial.println("dozavimas ijugtas");
  digitalWrite(doz_pin, HIGH);
}

void on_doz_off_enter() {
  Serial.println("dozavimas isjungtas");
  digitalWrite(doz_pin, LOW);
}
void on_uzd_on_enter() {
  Serial.println("uzdegimas ijungtas");
  digitalWrite(uzd_pin, HIGH);
}

void on_uzd_off_enter() {
  Serial.println("uzdegimas isjungtas");
  digitalWrite(uzd_pin, LOW);
}
State state_doz_on(&on_doz_on_enter, NULL);
State state_doz_off(&on_doz_off_enter, NULL);
State state_uzd_on(&on_uzd_on_enter, NULL);
State state_uzd_off(&on_uzd_off_enter, NULL);

Fsm fsm_doz(&state_doz_off);
Fsm fsm_uzd(&state_uzd_off);
Fsm fsm1_doz(&state_doz_off);
Fsm fsm1_uzd(&state_uzd_off);
Fsm fsm2_doz(&state_doz_off);


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

  pinMode(doz_pin, OUTPUT);
  pinMode(uzd_pin, OUTPUT);
  pinMode(PWM_pin, OUTPUT);
  pinMode(pad_pin, OUTPUT);
  pinMode(pam_pin, OUTPUT);
  pinMode(siur1_pin, OUTPUT);
  pinMode(siur2_pin, OUTPUT);
  pinMode(temp1_pin, INPUT);
  pinMode(temp2_pin, INPUT);
  pinMode(temp3_pin, INPUT);
  pinMode(flame_pin, INPUT);
  pinMode(led_pin, OUTPUT );

  fsm_doz.add_timed_transition(&state_doz_off, &state_doz_on, 5000, NULL);
  fsm_doz.add_timed_transition(&state_doz_on, &state_doz_off, 10000, NULL);
  fsm_uzd.add_timed_transition(&state_uzd_off, &state_uzd_on, 10000, NULL);
  fsm_uzd.add_timed_transition(&state_uzd_on, &state_uzd_off, 7000, NULL);
  fsm1_doz.add_timed_transition(&state_doz_off, &state_doz_on, 3000, NULL);
  fsm1_doz.add_timed_transition(&state_doz_on, &state_doz_off, 7000, NULL);
  fsm1_uzd.add_timed_transition(&state_uzd_off, &state_uzd_on, 0, NULL);

}


void loop() {

  flame_pin = digitalRead ( flame_sensor_pin ) ;

  if (flame_pin == HIGH)
  {
    Serial.println("nera liepsnos");
    digitalWrite(led_pin, LOW);
  }
  else {
    digitalWrite(led_pin, HIGH);
    Serial.println("LIEPSNA");
    digitalWrite (uzd_pin, LOW);

  }


  temp1 = analogRead (temp1_pin);
  temp1 = temp1 * 0.3725;
  Serial.println("KATILO TEMPRATURA = ");
  Serial.print(temp1);
  Serial.println("*C");
  temp2 = analogRead(temp2_pin);
  temp2 = temp2 * 0.3725;
  Serial.println("RADIATORIU TEMPRATURA  ");
  Serial.print(temp2);
  Serial.println("*C");
  temp3 = analogRead (temp3_pin);
  temp3 = temp3 * 0.3725;
  Serial.println("BOILERIO TEMPRATURA  ");
  Serial.print(temp3);
  Serial.println("*C");

  if (temp1 <= 20 && flame_pin, HIGH)
  {
    fsm_doz.check_timer();
    fsm_uzd.check_timer();
  }
  else if (temp1 <= 20 && flame_pin, LOW)
  {
    fsm1_doz.check_timer();
    fsm1_uzd.check_timer();
  }
  if (temp1 <= 22)
  {
    digitalWrite (PWM_pin, 150);
    Serial.println ("ventiliatorius ijungtas");
    digitalWrite (pad_pin, HIGH);
    Serial.println ("padavimas ijungtas");
  }
  else if (temp1 >= 22)
  {
    digitalWrite (PWM_pin, 0);
    Serial.println ("ventiliatorius isjungtas");
    digitalWrite (pad_pin, LOW);
    Serial.println ("padavimas isjungtas");
    digitalWrite (doz_pin, LOW);
    digitalWrite (uzd_pin, LOW);

  }

  if (temp1 > 20)
  {
    digitalWrite(siur1_pin, HIGH);
    Serial.println("radiatoriu siurblys ijungtas");
  }

  else
  {
    digitalWrite (siur1_pin, LOW);
    Serial.println("radiatoriu siurblys isjungtas");
  }
  if (temp3 > 20 && temp1 > 20)
  {
    digitalWrite(siur2_pin, LOW);
    Serial.println("boilerio siurblys isjungtas");
  }
  else if (temp3 < 20 && temp1 > 20)
  {
    digitalWrite(siur2_pin, HIGH);
    Serial.println("boilerio siurblys ijungtas");

  }
  if (temp2 <= 20)
  {
    digitalWrite (pam_pin, HIGH);
    Serial.println ("pamaisymas mazeja");
  }
  else
  {
    digitalWrite (pam_pin, LOW);
    Serial.println ("pamaisymas dideja");
  }
    delay(1000);
}

Hi, What model Arduino controller are you using. Check to see if you can use pin 2 for PWM, if you are using a NO, pin2 is not a PWM pin.

Thanks.. Tom... :)

if you are using a NO, pin2 is not a PWM pin

It's not a PWM pin on the YES, either. Or the UNO. 8)

bugis33:
Thank you for responding to my request for help. I want to control the 8 channel relay block with this code using 3 thermocouples and a flame sensor. It should be noted that when the temp1 does not reach the specified temperature and the flame sensor is inactive, pin_4 should be operated at intervals of 5 and 10 seconds and pin_8 at intervals of 7 and 10 seconds, and when the flame sensor is activated and the temperature is still lower than the specified pin_8, it should be LOW and pin_4 should be in range 3 and 7 seconds. when it reaches the highest specified temperature pin_4 and pin_8 to be LOW, but every 20 seconds they get “HIGH” for about a quarter of a second
I use the FSM for the timer.
those curly brackets are just a mistake. I’ve made some changes to my colleague comments, but no one changes.

This

  if (temp1 <= 20 && flame_pin, HIGH)
  {
    fsm_doz.check_timer();
    fsm_uzd.check_timer();
  }
  else if (temp1 <= 20 && flame_pin, LOW)
  {
    fsm1_doz.check_timer();
    fsm1_uzd.check_timer();
  }

should be

  if (temp1 <= 20 && flame_pin == HIGH)
  {
    fsm_doz.check_timer();
    fsm_uzd.check_timer();
  }
  else if (temp1 <= 20 && flame_pin == LOW)
  {
    fsm1_doz.check_timer();
    fsm1_uzd.check_timer();
  }

I don’t know about the fsm code, all it does, but it seems there’s either too much or not enough code since in loop() it only does time check calls with no off/off/reset that I would expect if the timers do not run over and over which they should not. Perhaps they only run once and the code to start them up is missing, I dunno.

You have fsm and fsm1 with added functions to turn pins on and off as well as loop() code managing the same pins which may be a source of error.
You also have many serial prints to help track what is happening. I would print millis() at the start of all of those reports to know the when as well as the what of each. You might even print more info to track the code execution all the way through and print critical values, we call these ‘debug prints’ to help find bugs.

I write my own timers, it makes life easier. I write my own state machines too. These tutorials teach those with timers in the first and state machine in the second (but not at the top, scroll down or edit->find the word machine).

  1. Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
  2. Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

One state machine could handle this whole job though you’d have to define the states and actions to do it.

Serial.begin() should be a higher value than 9600. It should be fast as it can (or at least 115200) to empty the print queue quicker, do not forget to change the baud rate of serial monitor to match.

This...should be...

I'd disagree with that. It's not a good idea to use _pin in the name of a variable that holds a state.

Hi. Į am using Mega 2560

Hello everyone. I’m using the Mega 2560. Today I’ve made the changes that GoForSmoke recommends and a few more changes to my mind, and everything seems to work. Changes made to my anchor, so I deleted a few blank spaces and added extra brackets.

if (temp1 <= 20 && (]flame_pin == HIGH))
  {
    fsm_doz.check_timer();
    fsm_uzd.check_timer();
  }
  else if (temp1 <= 20 && (flame_pin == LOW))

Just have a few more questions:
how to make the temperature readable eg. 20.5 but not 20.50 because when the temperature reaches the specified relay it starts to switch it on and it switches off as long as the temperature does not exceed the set limit.
The second question is about the Nextion screen, I can not link it in any way. In what way I try, I get only meaningless characters, not the proper command signal to serial port THANKS for everyone

PaulS: I'd disagree with that. It's not a good idea to use _pin in the name of a variable that holds a state.

Yeah but if I get started it ends with how not to block and do you really want to see all that again?