PLC pulse command equivalence

theautomationguy:
@ michinyon I struggled to get the concept of PLC's ladder logic in the beginning too, I am willing to learn that is why I am here. I have bought a few beginners books & have no trouble copying other peoples code & making adjustments to them, just trying to get to the next base and this forum is very helpful for that.

@ GoForSmoke Correct I don't know "C" Your sketch looks good and you are leading down the right path. I am using Freetronics Eleven (Uno clone, I believe) I have tried your sketch & made the changes but I am only guessing at some things which isn't good enough to make things work.

This is what I have done so far
but I have to head off to work but will spend more time on it when I get home.

const int photoPin = 2;

const byte buzzerPin = 12;
byte state = 0;
unsigned long buzzerStart = 0;
unsigned long waitFiveMinsStart = 0;
unsigned long buzzerPeriod = 1000UL;
unsigned long fiveMinutePeriod = 300000UL;

void setup()
{
  Serial.begin(115200);
  pinMode(buzzerPin, OUTPUT);
  pinMode(photoPin, INPUT);
}

void loop()
{

switch (state)
{
 
  //case 0:                              //do nothing until the beam is broken
    if (photoPin, HIGH);   //adjust value or change to digitalRead if appropriate
    {
      state = 1;
      digitalWrite(buzzerPin, HIGH);   //turn on the buzzer
      buzzerStart = millis(); 
    }
   break;

//case 1:                              //buzzer sounds for one second
    if (millis() - buzzerStart >= buzzerPeriod)
    {
      state = 2;
      digitalWrite(buzzerPin, LOW);    //turn off the buzzer
      waitFiveMinsStart = millis();
    }
   // break;

//case 2:                              //do nothing until five minutes has elapsed
    if (millis() -  waitFiveMinsStart >= fiveMinutePeriod)
    {
      state = 0;
    }
   // break; 
  }




I assume I don't need the serial.
State = 2 I am confused with this, 0 = LOW = Off, 1 = HIGH = On, is my understanding or is this referring to the case: which I put // in front of?

// is comment, what in BASIC is REM or REMARK. Everything after is ignored by the compiler.

So yes, you commented out every case line in that code and the break statements (that cause execution to go straight to the end of the switch-case or loop the break is in).

You have interleaved the sensor and buzzer code into the state code. Some day you might not while for now it looks like it should work (once debugged) especially as how "button" is really a beam-interrupt not likely to bounce.
The day you might separate each section is the day after you've had to tear out input or action code and interleave something different in, or add yet another feature, one too many times. Till then, at least interleaving looks tighter.