Working LED in actual Arduino, but not in simulator

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?

int but1 = 2;
int but2 = 3;
int limitswitch1 = 4;
int limitswitch2 = 5;
int limitswitch3 = 6;
int redled1 = 8;
int blueled1 = 7;
int redled2 = 9;
int blueled2 = 10;
 
void setup() {
  pinMode(but1, INPUT_PULLUP);
  pinMode(but2, INPUT_PULLUP);
  pinMode(limitswitch1, INPUT_PULLUP);
  pinMode(limitswitch2, INPUT_PULLUP);
  pinMode(limitswitch3, INPUT_PULLUP);
  pinMode(redled1, OUTPUT);
  pinMode(blueled1, OUTPUT); 
  pinMode(redled2, OUTPUT);
  pinMode(blueled2, OUTPUT);
}

void loop() {
  if(digitalRead(but1)==LOW && digitalRead(limitswitch1)==HIGH){
    digitalWrite(redled1, HIGH);
    digitalWrite(blueled1, LOW);
    digitalWrite(redled2, LOW);
    digitalWrite(blueled2, LOW);
  }
  if(digitalRead(limitswitch1)==LOW && digitalRead(but1)==HIGH){
    digitalWrite(redled1, LOW);
    digitalWrite(blueled1, LOW);
    digitalWrite(redled2, LOW);
    digitalWrite(blueled2, HIGH);
  }
  if(digitalRead(limitswitch2)==LOW && digitalRead(but1)==HIGH){
    digitalWrite(redled1, LOW);
    digitalWrite(blueled1, LOW);
    digitalWrite(redled2, HIGH);
    digitalWrite(blueled2, LOW);
  }
  if(digitalRead(limitswitch3)==LOW && digitalRead(but1)==HIGH){
    digitalWrite(redled1, LOW);
    digitalWrite(blueled1, HIGH);
    digitalWrite(redled2, LOW);
    digitalWrite(blueled2, LOW);
  }
  if(digitalRead(but2)==LOW && digitalRead(but1)==HIGH){
    digitalWrite(redled1, LOW);
    digitalWrite(blueled1, LOW);
    digitalWrite(redled2, LOW);
    digitalWrite(blueled2, LOW);
  }
}

Seems to me that this is a question for the simulator forum.

Your UNO is unplugged

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).

Further spoiler: most Arduinos have fewer than 256 i/o pins…

a7

Hi, in the picture below you see a led that is on. Why ?
cathode_gnd

Because the cathode is the GND-side and the anode is the positive side.
Turn your leds around.

Do you know Wokwi ?
I made your project in Wokwi: https://wokwi.com/arduino/projects/299577837917045256.
In the middle-upper of the screen is the start button. I hope you like it.

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.

1 Like

Thank you, that helps a lot!

For my project:

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.

Then there should be 4 switches.

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.

You can check all the limits 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.