Program for Feeder application

Hello everyone. I hope I'm going to do everything by the book here.

I would appreciate some help with a code for my vibrating feeder. If I explain my problem:

I have already written the first part of the code (attached below), but I'm having trouble with the second part. The first part of the code works just fine no problem there, but I need new code for the second part. In the first part of the code, the diffuse sensor is switching from high to low all the time (the moving parts in vib. feeder are switching it from high to low) except when the vib. feeder becomes empty.
Then it goes to the second part (the second 'buffer feeder' should fill the first vib. feeder). I would like to write this in a way that the buffer feeder (R0) starts for 2 seconds and stops. But then I would like to check when the R0 is empty and then starts the siren or the light or whatever. My idea is that the program should check with the sensor (a0) if there are parts in the feeder but in a way that it's checking it for let say 5 seconds and within these 5 seconds there should be no part in the vib. feeder (doesn't get high on a0 not even once). When this happens it should start something (siren, light,...). Also within these 5 seconds, the buffer feeder should run all the time.
Would be someone so kind and help me with that dilemma? :slight_smile:

I hope I haven't written this confusing. I'm not a programmer but I would like to do and test some basic stuff on my own.

I almost forgot. I am using Controllino PLC Maxi but it's based on Arduino so programming is the same.

Thank you.

void setup()
{
  pinMode(R9, OUTPUT); //vib. feeder
  pinMode(R0, OUTPUT); //buffer feeder
  pinMode(a0, INPUT); //sensor
}

void loop()
// FIRST PART
{
  digitalWrite(R9, HIGH); //vib. feeder
  for (i = 1; i <= 10; i += 1) {
    delay(5000); 
    digitalWrite(R0, LOW); 
    if (digitalRead(a0) == HIGH) { //sensor
      i = 0;
    }
  }
//SECOND PART:
  digitalWrite(R0, HIGH);
  delay(2000);
  digitalWrite(R0, LOW);

Rather than this style

void setup()
{
  pinMode(R9, OUTPUT); //vib. feeder

give meaningful names to your variables like this and you don't need comments and the code will be easier for the brain to follow.

byte vibFeederPin = R9;

void setup()
{
  pinMode(vibFeederPin, OUTPUT);

and

void loop()
// FIRST PART
{
  digitalWrite(vibFeederPin, HIGH);

It also means that you only need to change one line of code if you want to use a different I/O pin for the vibFeeder.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

You should also read about "state machines". You probably should have some variables that keep track of the states of the different parts of the project - for example whether something is operating or whether something is empty.

...R

Try writing it out in steps, like this pseudo-code:

bool VSNBE // vib feeder should not be empty flag


void setup()
{
  ...
  ...
  VSNBE = true // assume some in vib
}


void loop()
{
  run vib
  if (vib empty)
  {
    if (VSNBE) // vib is empty but it shouldn't be we've either just started or
               // just tried to refill it.
    {
      sound alarm
      wait for operator to press restart
    }
    else
    {
      try to reload from buffer
      VSNBE = true // assume some went into vib
    }
  }
  else
  {
    VSNBE = false
  }
}

then run through it in your head, maybe scribbling down the state of the vib and the buffer at each step. (I think the above works, but I won't guarantee it!).

Robin2:
Rather than this style

void setup()

{
 pinMode(R9, OUTPUT); //vib. feeder



give meaningful names to your variables like this and you don't need comments and the code will be easier for the brain to follow.


byte vibFeederPin = R9;

void setup()
{
 pinMode(vibFeederPin, OUTPUT);




and


void loop()
// FIRST PART
 digitalWrite(vibFeeder, HIGH);

I agree with all the above, but a couple of preprocessor definitions make it even more self-documenting, e.g.

#define ON HIGH
#define OFF LOW

void loop()
// FIRST PART
{
  digitalWrite(vibFeeder, ON);

Which works as long as all the outputs are sourcing (i.e. high to turn on). If not, you have to comment them all anyway.

I'm new in programming and I'm not familiar with millis() so, first I've to do some reading and then I'll try 'spell' something out :slight_smile:
Thank you for your suggestions and help.