Hello guys, I tried to simulate something like the linear actuator but using LED to represent. I did the code and wire everything up in person and it worked, but somehow it doesn't work in the simulator. Is there anything wrong with the diagram? Also, what can I improve with the code?
learn about different variable types.
see if any of your port pin assignement is really two bytes large and chose the right variable type instead.
make variables const if they shouldn't change during runtime (spoiler: pin assignments don't change so much, make them const).
The code can be improved if I know what the sketch should do. The way it works does not seem to be something that was the goal of the project.
There are few minor things to say about the sketch. We prefer a 'const byte' or 'const int' and the word "pin" in the variable. But you don't have to worry about that, they are really minor things.
int but1 = 2; // not preferred
const int button1Pin = 2; // okay
const byte button1Pin = 2; // okay
Some prefer a 'byte' and others prefer a 'int' for a pin number. In my opinion they are both okay.
There are 2 linear actuators,4 limit switches, and 2 buttons. #1 and #2 limit switch are for A actuator, #3 and #4 switches are for B actuator.
Step1: press first button > A actuator will retract and hit switch #1 and stop > once it hits switch #1 it will activate B actuator to extract and hit switch #3 and stop.
Step2: press second button > B actuator will retract and hit switch #4 and stop > once it hits switch #4 it will activate A actuator to extract and hit switch #2 and stop.
But based on the my code, it wouldn't happen. I am trying to see what can I do to make one actuator move while two switches are pressed at the same time.
What do you mean with two switches pressed at the same time ? Do you mean the two buttons ?
You try to solve the problem with if-statements. However, you need a certain sequence of code.
There is something for that: a Finite State Machine. That is a fancy word for something simple: https://majenko.co.uk/blog/finite-state-machine.
It is small chunks of code, and a 'state' selects which chunk of code should run this time.
You can make a sketch without the Finite State Machine, but when you look at your code afterwards, you only have to reorganize the code to make it a Finite State Machine.
What I meant by two switches are pressed at the same time is that when actuator 1 hit the first limit switch, then the actuator 2 will starting moving and hit another limit switch. At the moment when 2nd actuator hit the switch, those two limit switches are pressed at the same time.
// pseudo code, don't use it for real
void loop()
{
if (actuator1 == going_forward)
{
check limit_switch_2
}
if (actuator1 == going_backward)
{
check limit_switch_1
}
if (actuator2 == going_forward)
{
check limit_switch_4
}
if (actuator2 == going_backward)
{
check limit_switch_3
}
}
To do that, you have to know if the actuator is moving and in which direction.
That means you need global variables that know the state of each actuator.
You can not solve this with if-statements.
Because the Arduino loop() runs over and over again, you have to remember things with global variables.
In some cases, remembering a few things turns out to remember just one thing: the 'state' of a Finite State Machine.
Can you add a timeout to your sketch, that if it takes too long for an actuator to reach the end, that it will stop and an alarm goes off ? With a Finite State Machine you can add whatever you want.