Go Down

Topic: Simulating a PLC ladder diagram on arduino (Read 7 times) previous topic - next topic


If you want this result value to persist after your calculation then it needs to be a static variable, or declared outside the function. All the variables declared inside the function are discarded once the function returns.

Again, though, this is a very contrived way to evaluate expressions in an Arduino and I do not think you will be doing your co-workers any favors if you encourage them to design their Arduino sketches as if they were PLC programs. Using 'C' and C++ code you have far more flexibility and can produce far simpler solutions than you would achieve with the PLC approach you're using. Better IMO to describe the behaviour you're trying to achieve and then write it directly in 'C'/C++.

There are hundreds of different programming languages and types of computer and controller and the value comes from knowing what they're capable of and how to use them most effectively - not how to make them all look and behave like the first thing you learned.
I only provide help via the forum - please do not contact me for private consultancy.


sorry for my last code. i have try the code, i can compile well but cant be execute according to the principle that talk before.
this is the one that work after i have spend time making it and testing it to be working well.

this is just an example:
Code: [Select]

trying to set arduino latch funtion

connect pin 2 to a momentary push botton and the other leg to ground
pin2 -----./.----gnd
    ./. is a momentary switch
    repeat connection on pin3
    since most arduino have a LED connected to pin 13, i will use this to varify the code
boolean result = LOW;
void setup()
  pinMode(2,INPUT); //setting pin 2 as input
  digitalWrite(2,HIGH);// activating pin 2 internal pull up
  pinMode(3,INPUT);// setting pin 3 as input
  digitalWrite(3,HIGH);//activating pin 3 internal pull up
  pinMode(13, HIGH);// setting pin 13 as output

void loop()
  int Set = !digitalRead(2);
  int Reset =!digitalRead(3);
  int K=latch(Set,Reset);

int latch(boolean s, boolean r)
  if ( (s== HIGH || result == HIGH) && r==LOW)
    result = HIGH;
  if (r == HIGH)
    result =LOW;
return result;


A keep function (or SetReset, or ResetSet, ...) is a function with a memory... You can't program it with local variables. You either need a global variable or a parameter.
You probably need something like this, but i haven't tested it...

Code: [Select]
#define I1 1
#define I2 2
#define Q3 3

boolean Input1, Input2, Output1, M1;

void setup()
  pinMode(I1, INPUT);
  pinMode(I2, INPUT);
  pinMode(Q3, OUTPUT);
  M1 = 0;

void loop()
  Input1 = digitalRead(I1);
  Input2 = digitalRead(I2);
  Output1 = keep(Input1, Input2, &M1);
  digitalWrite(Output1, Q3);

boolean keep(boolean Set, boolean Reset, boolean *Memory)
  if (Reset == HIGH) *Memory = LOW;
  else if (Set == HIGH) *Memory = HIGH;
  return *Memory;


the only reason for me to use PLC way of programming is that it is something i fully understand

Limiting what you do to what you already know is a bad idea, learn new things.

If you insist on programming the Arduino in Ladder logic then what you should look for is something that pre-processses ladder logic into C code.

Somebody seems to have done this here;

However even if you were going to use something like this I would suggest you need to learn how to program the Arduino fully in C first, otherwise what is happening will just be "magic" and you will not really know what is going on.


Zatalian thanks for the code. it works but theres a slight Typo in the code
int the part
Code: [Select]

digitalWrite(Output1, Q3);

it should have been
Code: [Select]

digitalWrite (Q3, Output1);

there's only one problem with this function of the one i post before,
if let say that i would like to call keep function a couple of time,
any term that use the same term will exhibit the same result.

Go Up