user86907: Here is my problem, I'm working on an automated garbage bin compressor. When the garbage is near the sensor, the compressor mechanism will trigger. I'm using Arduino Uno as the controller, a 12v AC window motor, an L298N motor driver, a 12v AC battery, an ultrasonic sensor, and 2 led indicators. The flow chart is as follows the sensor will detect if there is garbage in the garbage bin, if there is no garbage then the motor will not spin. If there is garbage then the motor will start spinning for 20-30 seconds and after the motor stops spinning then it will stop for 5 seconds before returning to its original position. There are 2 LED indicators, the green indicator will indicate if the motor doesn't spin and the red LED will indicate if the motor is in use. So far I'm able to program the motor to spin in 1 direction without a command to stop.
const int trigpin = 11;
const int echopin = 10;
int enA = 4;
int SPEED = 250;
int motordirection1 = 3;
int motordirection2 = 2; 
int duration = 0;
int distance = 0;

void setup(){
  pinMode (trigpin, OUTPUT);
  pinMode (echopin, OUTPUT);
  pinMode (enA, OUTPUT);
  pinMode (motordirection1, OUTPUT);
  pinMode (motordirection2, OUTPUT);
  pinMode (trigpin, OUTPUT);
  pinMode (echopin, INPUT);

}

void loop(){
  digitalWrite (trigpin, LOW);
  delayMicroseconds (2);
  digitalWrite (trigpin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigpin, LOW);

  duration = pulseIn (echopin, HIGH);
  distance = duration*0.034/2;

  if (distance < 1){
  digitalWrite (enA, 0);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
  else if (distance > 2 && distance <25){
  digitalWrite (enA, SPEED);
  digitalWrite (motordirection1, HIGH);
  delay (10);
  digitalWrite (motordirection2, LOW);
  delay (50);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
  else if (distance > 15){
  digitalWrite (enA, SPEED);
  digitalWrite (motordirection1, LOW);
  delay (10);
  digitalWrite (motordirection2, HIGH);
  delay (50);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
}

Here is the program that I have. I'm not sure if it's good or not.
EDIT: I made the adjustment in the code and it works but now the problem is the motor only moves in 1 direction when the sensor detects something but after it's out of range the motor doesn't spin back.

user7508: You wrote:

The flow chart is as follows [...]

Rather than a flow chart, you should think of your system as a finite
state machine, and draw its state diagram.
Superficially, a state diagram may look like a flow chart, but it has
some specific properties:

Within the states represented in the diagram, the machine does nothing
but wait for some event to happen.
Events trigger transitions between states.
Whatever actions the machine may perform can only happen on state
transitions.

Here is a state diagram that may represent your system:

In this diagram, the actions are implicit (because I am lazy), but you
may want to write them down explicitly. For example, on the transition
IDLE → MOVING_FORWARD the motor should be started, and on the next
transition (MOVING_FORWARD → WAITING) it should be stopped.
Note also that this only represents my understanding of your
specifications. You will probably have to amend it in order for it to
reflect what you really want to do.
Once you have the state diagram, the next step is... to debug it! It is
essential that you do this debugging before you start to write any
line of code. At this stage I can already spot a couple of issues:

One transition is conditioned on the “compressor” reaching its home
position. This means that you will need some means to detect this
event, like an endstop.
After compressing some garbage... there is still garbage in the bin.
Then, after the transition MOVING_BACK → IDLE, garbage will be
detected, the system will immediately transition to MOVING_FORWARD,
and it will get stuck in an endless loop.

Once you are done debugging the state diagram, and only then, can
you start writing the code. I suggest you follow the steps of Majenko's
tutorial on finite state machines: translating the diagram to
code should be quite straight forward.

86907: <p>Here is my problem, I'm working on an automated garbage bin compressor. When the garbage is near the sensor, the compressor mechanism will trigger. I'm using Arduino Uno as the controller, a 12v AC window motor, an L298N motor driver, a 12v AC battery, an ultrasonic sensor, and 2 led indicators. The flow chart is as follows the sensor will detect if there is garbage in the garbage bin, if there is no garbage then the motor will not spin. If there is garbage then the motor will start spinning for 20-30 seconds and after the motor stops spinning then it will stop for 5 seconds before returning to its original position. There are 2 LED indicators, the green indicator will indicate if the motor doesn't spin and the red LED will indicate if the motor is in use. So far I'm able to program the motor to spin in 1 direction without a command to stop.</p>
<pre><code>const int trigpin = 11;
const int echopin = 10;
int enA = 4;
int SPEED = 250;
int motordirection1 = 3;
int motordirection2 = 2; 
int duration = 0;
int distance = 0;

void setup(){
  pinMode (trigpin, OUTPUT);
  pinMode (echopin, OUTPUT);
  pinMode (enA, OUTPUT);
  pinMode (motordirection1, OUTPUT);
  pinMode (motordirection2, OUTPUT);
  pinMode (trigpin, OUTPUT);
  pinMode (echopin, INPUT);

}

void loop(){
  digitalWrite (trigpin, LOW);
  delayMicroseconds (2);
  digitalWrite (trigpin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigpin, LOW);

  duration = pulseIn (echopin, HIGH);
  distance = duration*0.034/2;

  if (distance &lt; 1){
  digitalWrite (enA, 0);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
  else if (distance &gt; 2 &amp;&amp; distance &lt;25){
  digitalWrite (enA, SPEED);
  digitalWrite (motordirection1, HIGH);
  delay (10);
  digitalWrite (motordirection2, LOW);
  delay (50);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
  else if (distance &gt; 15){
  digitalWrite (enA, SPEED);
  digitalWrite (motordirection1, LOW);
  delay (10);
  digitalWrite (motordirection2, HIGH);
  delay (50);
  digitalWrite (motordirection1, LOW);
  digitalWrite (motordirection2, LOW);
  }
}
</code></pre>
<p>Here is the program that I have. I'm not sure if it's good or not.</p>
<p>EDIT: I made the adjustment in the code and it works but now the problem is the motor only moves in 1 direction when the sensor detects something but after it's out of range the motor doesn't spin back.</p>
 7508: <p>You wrote:</p>
<blockquote>
<p>The flow chart is as follows [...]</p>
</blockquote>
<p>Rather than a flow chart, you should think of your system as a <a href="https://en.wikipedia.org/wiki/Finite-state_machine" rel="nofollow noreferrer">finite
state machine</a>, and draw its <a href="https://en.wikipedia.org/wiki/State_diagram" rel="nofollow noreferrer">state diagram</a>.
Superficially, a state diagram may look like a flow chart, but it has
some specific properties:</p>
<ul>
<li>Within the states represented in the diagram, the machine does nothing
but wait for some event to happen.</li>
<li>Events trigger transitions between states.</li>
<li>Whatever actions the machine may perform can only happen on state
transitions.</li>
</ul>
<p>Here is a state diagram that may represent your system:</p>
<p><a href="https://i.stack.imgur.com/68Ak0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/68Ak0.png" alt="State diagram of an automated garbage bin compressor" /></a></p>
<p>In this diagram, the actions are implicit (because I am lazy), but you
may want to write them down explicitly. For example, on the transition
IDLE → MOVING_FORWARD the motor should be started, and on the next
transition (MOVING_FORWARD → WAITING) it should be stopped.</p>
<p>Note also that this only represents <em>my understanding</em> of your
specifications. You will probably have to amend it in order for it to
reflect what you really want to do.</p>
<p>Once you have the state diagram, the next step is... to debug it! It is
essential that you do this debugging <em>before</em> you start to write any
line of code. At this stage I can already spot a couple of issues:</p>
<ul>
<li>One transition is conditioned on the “compressor” reaching its home
position. This means that you will need some means to detect this
event, like an <a href="https://reprap.org/wiki/Mechanical_Endstop" rel="nofollow noreferrer">endstop</a>.</li>
<li>After compressing some garbage... there is still garbage in the bin.
Then, after the transition MOVING_BACK → IDLE, garbage will be
detected, the system will immediately transition to MOVING_FORWARD,
and it will get stuck in an endless loop.</li>
</ul>
<p>Once you are done debugging the state diagram, <strong>and only then</strong>, can
you start writing the code. I suggest you follow the steps of <a href="https://majenko.co.uk/blog/finite-state-machine" rel="nofollow noreferrer">Majenko's
tutorial on finite state machines</a>: translating the diagram to
code should be quite straight forward.</p>
