I'm trying to write an always block that will open a valve and then keep it open for a few seconds and then close it if needed. The valve opens when the input is 1 and it closes when it's zero. How can I make the code wait for a few seconds after the valve opens so that it doesn't close right after it opens?
Here is my code:
input in, alteraclock;
output reg out;
reg clock;
clock myclock(.clkin(alteraclock), .clkout(clock));
always@(in)
begin
if(in==1)
begin
out=1; //open valve
end
else
begin
out=0; //close valve
end
end
PS: I'm using an Altera DE2-115, if that helps at all.
Best Answer
Use a state machine and a large counter. In one state, wait for the input to change. When the input changes, set the counter to a large number, update the output, and switch to the delay state. In the delay state, decrement the counter. When it reaches zero, switch back to the wait for input state.
Edit: I'm not going to write your code for you, but here is a template for a state machine that you can play with:
Here is a way to do this without an explicit state machine: