First project help please

I am pretty new to Arduino. For my first project I would like to control a motor with 2 switches. Imagine a motor with paddles that push marbles into a funnel attached to the top of a tube. I want the motor to run until the tube is full, then stay off until the level falls to a certain level. Similar to an automatic water tank fill.

Tube full = top and bottom switches both pushed = motor off
Tube partial full = top switch not pushed, bottom switch pushed = motor off
Tube empty= neither switch pushed = motor on

I know this is possible, but I’m not sure exactly how to do it. I’ve written a sketch(my first attempt), but I’m fairly certain it is completely messed up and won’t work. It compiles just fine and everything, but it seems over-complicated. Is anyone willing to look it over for me? I attached the .ino file just in case.

Thanks in advance for looking it over!

Case_Feeder.ino (4.8 KB)

Tube partial full = top switch not pushed, bottom switch pushed = motor off

That will turn off the motor as soon as the bottom switch is activated. Leave that rule out if you want the tube to fill before stopping the motor.

So what you want to do is:

if (not bottomSwitch) // Level has gotten low. motorOn(true); // Start filling

if (topSwitch) // Level has gotten high motorOn(false); // Stop filling

Is that enough to get you started on the programming?

//Variables will change:
int SW1state = digitalRead(SW1);         // Intitial top switch position
int SW2state = digitalRead(SW2);         // Intitial bottom switch position
int LED1state = digitalRead(LED1);       // Current LED1 state
int LED2state = digitalRead(LED2);       // Current LED2 state
int MOTOR1state = digitalRead(MOTOR1);   // Current MOTOR state

Do the initialisation of the variables in "setup() after you've set the pinMode for the pin.

Edit: scrub that - I'd assumed these were meant as the previous states of the pins, but it seems you're overwriting them in "loop()" anyway, so there's little point in making them globals.

  if (MOTOR1state == LOW) {                          // Turn LED2 ON
    digitalWrite(LED2, HIGH);
  } else {                                         // Turn LED2 OFF
    digitalWrite(LED2, LOW);

aka digitalWrite(LED2, ! MOTOR1state);

I think you are over-complicating things. You don't need to de-bounce if you aren't counting switch closures. What is it you want the LEDs to do?

// Reloader Case Feeder Sketch
// Automates Feed Motor 
// Reads the state of 2 switches to control the feeder motor

// Constants won't change. 
// Set pin numbers:
const int SW1 = 3;                       // The pin for top switch.  HIGH when a case is present.
const int SW2 = 4;                       // The pin for bottom switch. HIGH when a case is present.
const int LED1 = 5;                      // The pin for LED1
const int LED2 = 6;                      // The pin for LED2
const int MOTOR1 = 7;                // The pin for the motor control. HIGH=run, LOW=stop

void setup() {
  Serial.begin(9600);                    // Start serial connection
  pinMode(SW1, INPUT_PULLUP);            // Sets SW1 as input
  pinMode(SW2, INPUT_PULLUP);            // Sets SW2 as input
  pinMode(LED1, OUTPUT);                 // Sets LED1 as output
  pinMode(LED2, OUTPUT);                 // Sets LED2 as output
  pinMode(MOTOR1, OUTPUT);               // Sets MOTOR1 as output

  // set initial state of motor
  digitalWrite(MOTOR1, LOW);  // Motor off

void loop() {
  if (!digitalRead(SW2)) {  // Level has gotten low.
    digitalWrite(MOTOR1, HIGH);  // Start filling

  if (digitalRead(SW1)) { // Level has gotten high
    digitalWrite(MOTOR1, LOW);  // Stop filling

First off, thanks for the responses. I'll try to answer All the questions.

To johnwasser -

I saw that after I got the breadboard layout finished. It seemed to work decent until I saw what you pointed out, that as soon as the bottom switch is re-activated it shuts the motor down. I'll use your advice and keep experimenting.


I see what you mean. This is my first attempt at a project from scratch, so please excuse the sloppy code.

To johnwasser's 2nd post -

The LEDs are simply a way to help me visualize the state of the motor. I don't have the motor attached yet. It's not really pushing marbles, that was just my example I thought up to simplify my description. The end sketch will read the switch positions and simply turn the motor on and off as needed. The LEDs will be taken off once I know the motor is running properly.

I'm nervous about disclosing my project because it will most likely offend people, but here it goes:

I reload pistol ammo, and I want to properly automate the case feeder, which has a motor that turns a plate in a hopper. The plate grabs the cases and drops them down a tube, all facing the same direction. The factory one runs the motor until the tube is full. It only has one switch at the top, so every time a case is pulled out the bottom of the tube, the motor runs until one case drops into the top of the tube and it shuts off again. It makes more sense to have the motor stay off until the tube is almost empty, then run longer and fill it up before shutting back off.

I hope this helps you all understand what I'm doing better, and thanks again for your responses!

My Sketch is working great now. One more quick question. The top switch will constantly be switching states as pieces fall down the tube. What would be the most effective way of ignoring the top switch changes as the pieces fall past it? I need it to ignore the switch when it quickly goes from open to closed back to open as a piece passes it, and only shut the motor off when the switch remains closed for 'x' amount of time. To John, that was the reasoning for the debounce on the switches, although I probably did it wrong.

  if (digitalRead(SW1)) { // Level is high?
    // Wait long enough for a case to pass but not long enough 
    // for a second case to have arrived.  Tune manually
    if (digitalRead(SW1)) { // Level IS high
      digitalWrite(MOTOR1, LOW);  // Stop filling

Thanks again John for your help. I inserted the code you posted and got mixed results. It could be because I have been changing things a little bit based on other comments I have received. Anyway, after inserting your code, it would work sporadically. One time it would work flawlessly up to 10 times, and other times it would shut the motor off on the first quick push of the switch. I experimented with the delay time, but even as low as 100 all the up to a full second, the results never varied, it just stayed random. Attached is the latest version of the sketch. I would very much appreciate it if you could give it one more look and see where I messed up. Thanks again for your kindness towards a complete newbie!

When you see your code in the sketch, I’m using the pullups on the 2 pins for the switches, so the HIGH/LOW is inverted. Switch UP=HIGH, Switch DOWN=LOW.

Case_Feeder_Improved.ino (2.74 KB)