Hello everyone, I am working on a college art project and need to control it with arduino: i do not know how.
Please help as I am struggling with this.
I have 3 toggle switches that are closed in sequence. Here is a table showing the correct sequence. At end line "f " I need to make pin 13 high and will trigger a relay after delay of 10 seconds
sw1 sw2 sw3
end f 1 1 1
e 1 0 0
d 1 1 1
c 0 1 1
b 0 1 0
start a 0 0 0
Because line "d" and "f" duplicate i need to look at line "e" where sw_1 is closed and sw_2 is open as this is not repeated in the table. The "f" line where all switches are closed will trigger the delay. Then I must make this repeat.
The 3 switches are wired with pull down resistors.
Please tell me what I must do to have this work.
Here is my first try at code-
I have 3 toggle switches that are closed in sequence. Here is a table showing the correct sequence. At end line "f " I need to make pin 13 high and will trigger a relay after delay of 10 seconds
Can you explain what you mean by "in sequence"? Do the switches have to be set first in state (a), then in state (b) and so on until state (f), and you want to trigger the relay ONLY if they have gone through all these steps in sequence?
What needs to happen if the user does some steps OK, but then changes the wrong switch for the next step? Does it go back to the start?
Looking at your code, the names used for the variables and the extra (((( ))))) make it a bit hard to read. But it seems to trigger the relay if pin 8 is 1, pin 9 is 0 and pin 7 is 1. There is nothing in there to check that the switches have gone through different steps in sequence (if that is what you wanted).
Anything else you can tell us about the project and how you need it to work would be helpful.
Ray, I am sorry for the code, it is my first try. My project will work only if the delay time is triggered on line "f". It must not trigger on line "d" .
In sequence - all three switches on a line must be correct before the program looks for the next line to be correct
The switches are triggered by sensors- each line will be correct eventually. The program should wait for each line.
I am trying to monitor sw1 also sw2 in line "e" then, if they are correct (1) and (0), enable line "f" to start the delay timer when all switches are on(1).
I realize now that line a and b are not necessary. after the delay it must look through the sequence line c-f again.
so it is just lines c,d,e,f
I do not want to confuse you even more. I must go. I have to be awake in 4 hours.
Than you
Danielle
int status;
void loop(){
status<<0;
status<<digitalRead(8);
status<<digitalRead(9);
status<<digitalRead(7);
}
The last three bits of status would now represent the current switch configuration so if switch 1 was closed and switches 2 and 3 were open, then status=b00000100.
Assuming status is a global variable, the next iteration of loop would move those bits left and add the new switch configuration to the low order bits, so if all 3 switches were closed, then status=b01000111.
Then you can just use an if statement to see if last config was line e and new config is line f.
if (status==b01000111){
//do stuff
}
There is a little more to it as you only want to shift those bits left if the switch config has changed - ie not every iteration of loop.
I suggest that you divide your code into at least two separate functions - both called from loop()
One function will just read the switches and save their values in variables.
The other function will check the variables and do whatever should happen.
If you assign the value 0 for any switch that is off and when the switches are on assign 1 to Switch 1, 2 to switch 2 and 4 to switch 3 and then when you add the three switch values you will always have a number that is unique to the particular switch combination. It's another way of doing what @KrazeyNKrusty is recommending - but may be easier to implement if you are new to coding. For example combination f = 7, e = 1, d = 7, c = 6, b = 2, a = 0
There is what looks a reasonable introduction to state transition diagrams here:
Note that in practice transitions are either inputs changing or time-outs. Certain
states when entered may need to set a timeout variable which is compared with
the current time whilst in that state.
State machines are naturally coded with an int variable to represent the state and
a switch statement using that variable. #defines are often used to give names
the the state values and make the code easy to read:
#define STATE_A 1
#define STATE_B 2
int state = STATE_A ;
void loop ()
{
switch (state)
{
case STATE_A:
if (...) // typical code is testing inputs and doing actions and changing state.
{
do_something () ;
state = STATE_B ;
}
..
break ;
case STATE_B:
..
..
break ;
}
...
}
Also in complicated systems its often the case that you have several state machines
running in parallel, or as subordinate state machines for a particular subtask - so
the strict formalism isn't always followed, but its a powerful structuring tool once
you get used to it.
danielle36:
Hello everyone, I am working on a college art project and need to control it with arduino: i do not know how.
Please help as I am struggling with this.
I have 3 toggle switches that are closed in sequence. Here is a table showing the correct sequence. At end line "f " I need to make pin 13 high and will trigger a relay after delay of 10 seconds
sw1 sw2 sw3
end f 1 1 1
e 1 0 0
d 1 1 1
c 0 1 1
b 0 1 0
start a 0 0 0
Before I even look at code, I need to understand the hardware and process first.
Physically, how are the switches toggled? Is a person flipping the switches manually, or is there something mechanical in the art installation that flips the switches?
If it is a person, what should happen if the person flips the wrong switch to go to the next item in the sequence?
Going through the sequence, between the first 4 items, only one switch is being changed. This is good for the same reason why gray code (yes, follow the link to the wikipedia article on gray code, particularly the motivation section) was developed. But to go from D to E and E to F two switches need to be toggled simultaneously. This is bad. It is incredibly hard (if not impossible) to guarantee that two switches (mechanical, optical, or what ever) will change states simultaneously all the time. If there is no way to change which switches are toggled for each stage, I would see if you can add a button (or another switch) that is pressed (or changes state) when you know the switches for the next step are in the correct position. This additional button or switch would tell the Arduino to read the state of your sequence switches.
In sequence - all three switches on a line must be correct before the program looks for the next line to be correct
The switches are triggered by sensors- each line will be correct eventually. The program should wait for each line.
From the above, I inferred that the OP does not mind if the switches go into incorrect combinations at a particular stage in the process. Eventually the user will put them into the correct combination for the next stage, and the program should change state when that happens.
danielle36:
Robin2. But how will i tell d from f, if they are both 7 ?
To avoid any uncertainty, the confusion you mention has nothing to do with me ascribing 7 to that setting. However you look at it, d and f are the same.
As you have presented the problem it is impossible to tell d from f except by watching the sequence of switch settings - d comes after e. Of course (according to your presentation) we have no means to know what might come before f and if that happened to be an e you would have a problem.
Is there some means to let the Arduino know when the switch sequence is about to start, or when it has completed?
I think we need the OP to confirm if, in a particular state, she is happy to ignore ALL switch combinations except for the one that she is looking for to move the state to the next state.
For example, assume we have ended up at row (d) in her original post, the switch value having just achieved 7.
Part of the state machine code could be (assuming some #defines before):
if (currentState == STATE_D && switchValue == 1)
{
currentState = STATE_E;
}
else
{
// ignore all other switch values, stay in same state and do nothing
}
if (currentState == STATE_E && switchValue == 7)
{
currentState = STATE_F;
}
else
{
// ignore all other switch values, stay in same state and do nothing
}