Programming Question

I have started on arduino from total scratch after programming PLC’s and am learning as I am going along. I have a project that I want to utilize 2 sensors that are controlling 3 relays.

Sensor 1 will control Feed 1 and Auger 1. Sensor 2 will control Feed 2 and Auger 1.

This is what I have successfully programmed so far:

When Sensor 1 is HIGH, Feed 1 will be HIGH and Auger 1 will be LOW. When Sensor 1 goes LOW, Feed 1 goes LOW after delay and Auger 1 goes HIGH after delay. When Sensor 1 comes back HIGH, Feed 1 goes HIGH without delay and Auger 1 goes LOW with delay.

This is the next step in the program:

When Sensor 2 is HIGH, Feed 2 will be HIGH and Auger 1 will be LOW. When Sensor 2 goes LOW, Feed 2 goes LOW after delay and Auger 1 goes HIGH after delay. When Sensor 2 comes back HIGH, Feed 2 goes HIGH without delay and Auger 1 goes LOW with delay.

Victory_.ino (1.05 KB)

Your topic says "Programming Question". But your post does not ask a question. Also, you did not post in the programming questions section of the forum and you did not post your code correctly. I just wanted to let you know these things.

Ok I'm a Newbie big time! My question is how to write the code for the next step and make it run with the first step.

Please read and follow the directions in the "How to use this forum" post.

You will see delay() used a lot in Arduino examples. It's simple and it gets the basic point of the example working but it doesn't show you how to write more advanced programs.

Have you heard of a "state machine"? That concept will get you on your way to making very complex Arduino programs. Re-write your list of actions as states.

Your code looks like this after using the </> code tag.

byte sensorOne = 2;
byte sensorTwo = 3;
byte feedOne = 8;
byte feedTwo = 9;
byte augerOne = 16;

unsigned long feedEmptyMillis; // when feed level dropped below sensor
unsigned long turnOnDelay = 3000; // wait to turn on auger
unsigned long turnOffDelay = 10000; // turn off auger after this time

void setup() {
  // put your setup code here, to run once:

  pinMode (sensorOne, INPUT);
  pinMode (sensorTwo, INPUT);
  pinMode (feedOne, OUTPUT);
  pinMode (feedTwo, OUTPUT);
  pinMode (augerOne, OUTPUT);
}

void loop() {

  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();

  boolean prox1 = digitalRead (sensorOne);
  boolean prox2 = digitalRead (sensorTwo);

  if (prox1 == LOW) {
    digitalWrite (feedOne, LOW);
    digitalWrite(augerOne, HIGH);
  }

  if (prox1 == HIGH) 
    feedEmptyMillis = currentMillis;
 
   if (currentMillis - feedEmptyMillis <= turnOnDelay) {
 digitalWrite (augerOne, LOW);
 } 
   if (currentMillis - feedEmptyMillis <= turnOffDelay) {
 digitalWrite (feedOne, HIGH);
  }
}

It doesn’t change your code in this case, but try to be consistent with code blocks and {} framing!

Bookmark this page: Arduino Reference - Arduino Reference

When you have the Arduino IDE open, open your browser to that page as a quick reference to commands in the code that you may not know exactly. Don't guess with code, know. None of it is there for decoration, it all runs as compiled.

In the IDE, use the AutoFormat tool often when writing. It will let you know a whole range of errors you can fix right then +and+ it will make your indents nice and clean.

Your IDE is packed with examples. There are the Tutorial Examples and the Library Examples. Just stay away from Tutorial Examples section 4! It teaches BAD HABITS for ARDUINO that are maybe good ones on PC's, the use of capital-S Strings that can/will play havoc with your limited heap space (global variables) in RAM. An Uno has 2K RAM for everything, it's best to use C strings that are simply arrays of char variables to hold text values in. You can address/access/change any element directly and that's all that changes.

There are some differences between standard C/C++ and Arduino C/C++.

Standard C/C++ has an int main() function that returns an error code.

Arduino has that but you don't see it. What you see is

void setup() // runs once
{
{

void loop() // runs over and over
{
}

which is perfect for a microcontroller running automation or a one-time demonstration run in setup() only.

If you only do a little in any pass through loop(), what's inside loop() will run > 100KHz.

I set up a 32 bit variable as a counter and a 16 variable as a time mark. In void loop() that adds 1 to the counter and then checks to see if 1 second has passed within 4 microseconds and if it has, the counter value is printed and then zeroed then the time mark is set ahead 1 second and then loop() runs again, etc. That's how I know it runs at over 111KHz, doing very little.

When I add a blinking led that also has a time mark, the speed drops to to 81KHz. With button as well, 67KHz but that includes debouncing the button.

But YOU only want to do simple things so you may get > 100KHz responses.

Hi,
Can you post a table of all you possible input combinations and what you want outputs to do for each case?

What mode Arduino are you using?

There is a function called switch.. case that will help you.

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Thanks.. Tom..... :slight_smile:

Hi,
Does the code you posted really work?

if (prox1 == HIGH)
feedEmptyMillis = currentMillis;

if (currentMillis - feedEmptyMillis <= turnOnDelay) {
digitalWrite (augerOne, LOW);
}
if (currentMillis - feedEmptyMillis <= turnOffDelay) {
digitalWrite (feedOne, HIGH);
}
}

How is that supposed to work?

To clarify, what happens if both sensors are HIGH?
What are they sensing?
You have two systems trying to control one auger.

Can you post a diagram of the sensor, feeder and auger layout,and what position the feeders and auger are in for HIGH and LOW outputs?

So we can see what you are trying to achieve.
A picture is worth a thousand words.

Tom… :slight_smile:

cordellmusser:
I have started on arduino from total scratch after programming PLC's and am learning as I am going along.

Idle curiosity, which PLCs?

Thanks everyone for all your input. I sure appreciate this responsive and high quality forum that has great talented people writing responses! I have attached a diagram of sorts to try to help visualize what my project consists of. I couldn’t figure out how to view my attachment before I posted it so I hope it is clear enough. All I have accomplished so far in the code is to utilize one sensor that operates one auger and feeder. My question is how to incorporate sensor 2 and make it operate the feeder and auger like the diagram demonstrates. With PLC programming this is a small task but I am looking for more cost effective components. I am currently working with UNO and MEGA boards.

Hi,
Ops diagram, thankyou.
a578a4fd43454c607ce0b3ad52cff5789704adb9.jpg
Tom… :slight_smile:

cordellmusser:
All I have accomplished so far in the code is to utilize one sensor that operates one auger and feeder. My question is how to incorporate sensor 2 and make it operate the feeder and auger like the diagram demonstrates. With PLC programming this is a small task but I am looking for more cost effective components. I am currently working with UNO and MEGA boards.

I have void loop() running fast and 16 buttons. The code to do one button is in loop. I make it so it does 1 button out of an array, the button will be the one that a variable will have the number for. Then I have the index change with every run through loop() to serve 1 button as before but a different button each time. When loop() runs > 50KHz, each button gets attention > 3x a millisecond.

Your answer is time and speed. Fast code can deal with one thing at a time so quickly that none stumbles.