LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reset shift register back to 0 when a condition is true

I have a code that follows the next logic (initially shift register=0):

 

If sensor value > threshold, send email and shift register value+1

if sensor value > threshold, and shift register > 0, wait certain amount of time and then send email

 

I´m doing this so that I cand send an email every x seconds so that I don´t recieve emails every ms since the code is inside a while loop. Also because I wanted the first email to be sent immediatley the first time the value exceeds the threshold, but wait a certain amount of time before sending the other emails if the condition continues to be true. The problem is that if the value of the sensor drops, and it goes up again above the treshold, the shift register value will continue to be 1, so the program will wait x seconds before sending the email instead of sending it right away.

 

What I want to do is add a condition in which if value < threshold, shift register=0, so when it goes up again It works as if it were the first time. I have tried doing this by adding another case, but when I connect this new case to the shift register I get an error in the wiring. The same thing happens if I do it without the case structure.

 

Is there any other way to reset the SR value back to 0? All the answers I have found for similar problems include a case structure but that isn´t working.

0 Kudos
Message 1 of 7
(1,472 Views)

If you get an error after some wiring, attach that VI so we can see what you did wrong!

 

You already know how to increment (or not) using a case structure, you can do exactly the same to set the value back to zero.

 

I would strongly recommend to arrange the diagram more logically so you can follow the wire. Can you explain the feedback node? What that inserted automatically or did you place it? What do you have all these datatype mismatches? Do you really need to read the ini file with every iteration? How often does is change and what changes it?

Message 2 of 7
(1,450 Views)

I attached the way that I was trying to do it based on an example that I saw online. The feedback node is to obtain the current value of the shift register, so if it´s equal to 0 the email is sent right away, if it is greater than 0 wait some time before sending email. The icon was placed automatically when I wired the functions. You made me realize that I don´t need to read the file every iteration, thank you hahah. It´s only supposed to change if the user wants to change the information for the threshold and the email adress. I´ll try to organize it more when I have some time. Could you explain what you mean by datatype mismatches?

0 Kudos
Message 3 of 7
(1,439 Views)

@NataliaB14 wrote:

I attached the way that I was trying to do it based on an example that I saw online.


Link?

 

 

0 Kudos
Message 4 of 7
(1,424 Views)

I think the problem that you are having is you started by thinking about how you are going to accomplish what you want instead of thinking about what you want to do.  Who said anything about Shift Registers?  [That's a "how", as in "how do I do this", detail].

 

When I look at your code, I see a giant While loop with all kinds of things taking place every time the loop runs, so the loop does basically the same thing over and over.  I'm sure that's not what you want the program to do.  I also see various things all "happening at the same time" (because they are all in the same While Loop with no clear "time ordering" (Do this first, then do that, then do this other thing)).

 

You will have a much easier time of it if you redesign your program to be a State Machine.   You (mentally) divide your program into a set of "States", one of which is "Initialize", one of which might be "Check if Send E-mail", one of which might be "Read Instrument", one might be "Clean up and Exit". 

 

Under what conditions do you want to "Send E-mail"?  Better question -- under what conditions do you not want to send E-mail?  Sounds like the answer is "Everything is OK" or "I already sent one less than X hours ago".  Those are two separate conditions -- one is governed by a Boolean ("Everything OK"), the other by a Time (when was the last E-mail sent).   Let's put both the Boolean and a number representing the time of the last sent E-mail in separate Shift Registers.  Somewhere along the line, you need to initialize the Shift Registers -- in a State Machine, you do that in the "Initialize" State -- we'll initialize "Everything OK" to True, and "Last E-mail Set" to 0.

 

As  State Machine runs, at some point it might figure out that things are not OK-. You need two States, "Do Main Operation" and "Check Everything OK", where "Do" always does "Check" next, and "Check" usually does "Do" next.  "Do Main Operation" does its thing, and reads some instrument to decide if things are OK.  If so, it leaves "Everything OK" alone, otherwise it sets it to "False" and calls the next State, "Check Everything OK". 

 

"Check Everything OK" looks at "Everything OK".  If it is True, it is done, so it sets "Do Main Operation" as the next State.  Otherwise, it looks at how much time has elapsed since it sent the last message (conveniently saved on a Shift Register).  If it is "too soon to send another message", it does nothing, and goes back to "Do Main Operation", otherwise it (a) sends an E-Mail, and (b) saves the Current Time in the "Time of Last Message" Shift Register.

 

I suspect this might be more than you can handle, for which I apologize.  I wrote a little discussion of State Machines and what I called the "Boss-Worker Design Pattern" here on the Forums a few days ago, submitted by "taylorblack" on 12 Apr 2023.  That has a small example I wrote that has a little State Machine that you can Start and Stop (which was his question), but you can easily adapt this model to not respond to Front Panel Controls (which is what taylor wanted) but to respond to "Everything OK" saved in the Servant Loop.

 

Anyway, I've been typing for a while, so I'm going to go get some sleep.  Learn about how to create State Machines in LabVIEW (While Loop with a Case Statement "fed" by the States you want to run).  I apologize for wearing my Professor Hat so much.

 

Bob Schor

Message 6 of 7
(1,420 Views)
Spoiler
"...,so the program will wait x seconds before sending the email instead of sending it right away."

The reason it waits before sending an email is because all the conditions need to be met first (dataflow). It would wait for whatever time you set before moving forward. 

I am not sure how large your project will get, but by looking at it I can tell that you are still in the initial phase of the project. So maybe  it's time to implement state machine (only if it's necessary, maybe it's not needed for your project) . You can use my code I attached to your previous post and make it better 😃. I'm using a state machine there. 

State machine is awesome 🙃! It really helps stepping through. It's easy to debug, follow.

I'm far from being an expert like most guys in here, but I'm just giving a small advice based on what I've learned so far🤗. I'm still learning too😊

 

Message 7 of 7
(1,367 Views)