Automation Line Concept

So I’m doing this project where I am building an automation line which consists of a conveyor belt driven by a stepper motor. The motor will start and stop so that a batteries will be brought in front of a sensor one at a time, the sensor is an analog sensor attached to a solenoid which closes on the battery. Depending on whether or not the battery has the correct voltage it will be rejected in the next stage by another solenoid or left to stand on the conveyor until it reaches the end and drops off. So to summarise.

  1. Push Start Button
  2. Stepper Motor Rotates required degrees to bring first battery to sensor and stops
  3. Solenoid with sensor closes
  4. Analog reading taken
  5. Motor rotates required steps to bring the next battery to sensor, and the battery that has just been tested to rejection solenoid
  6. If the tested battery is below the required threshold rejection solenoid closes then opens, once open the sensor solenoid closes and checks the other battery. IF the battery that had been tested has correct voltage then the rejection solenoid does not close and instead the sensor solenoid closes for the battery after it.
  7. Stop button stops the system

NB the first rotation is not the same angle as the rotation which will move the batteries to the sensor one at a time, since a buffer will be feeding batteries onto a toothed belt to hold them one after the other.

I tested the components individually and they all worked, therefore the circuitry and components are fine. I wrote a program and compiled it without problems but when it came to the motor just started rotating without annything else happening. This is the code.

// Constants which will not change
// Set Pins
const int stopButtonPin = 39;
const int startButtonPin = 40;

int SenseSolPin = 12;
int SolPin = 11;
int DirPin = 2;
int StepPin = 1;
int SensePin = A0;
int LED = 13;

// Set Compare variables
const int ThresholdLevel = 600; // Threshold limit

//
//

boolean startupSequenceCompleted = false;
boolean oneBatteryChecked = false;

int powerButtonState = 0; // this stores the status of POWER button
int emergencyButtonState = 0; // this stores the stauts of the EMERGENCY STOP BUTTON

int valueRead = 0;

int checkBattery()
{
  int sensorReading = 0;
  
  if ( emergencyButtonState == LOW )
  {
    // Place Code here to check Battery 
    digitalWrite(SenseSolPin, HIGH);
    delay(1000);                           // Close Solenoid of Sensors
    sensorReading = analogRead(SensePin);  // Take Reading and store in variable "sensorReading"
    digitalWrite(SenseSolPin,LOW);  // Open Solenoid of Sensors
    delay(1000);
  }
  
  // This line returns the value out of this method
  return sensorReading;
}

void DiscardKeepBattery(int batteryLevel)
{
  if ( emergencyButtonState == LOW )
  {
    // This code checks if the battery level is what we want if not it is discards
    if ( batteryLevel < ThresholdLevel )
    {
      digitalWrite(SolPin, HIGH);// Battery does not have required charge hence discard                      
      delay(1000);          // Trigger Solenoid to discard battery 
  
      digitalWrite(SolPin, LOW);
      delay(1000); 
    }
  }
}

void setup() {
  // Set up pins
  pinMode(SensePin, INPUT);
  pinMode(startButtonPin, INPUT);
  pinMode(stopButtonPin, INPUT);
  pinMode(SenseSolPin, OUTPUT);
  pinMode(SolPin, OUTPUT);
  pinMode(DirPin, OUTPUT);
  pinMode(StepPin, OUTPUT);
  pinMode(LED, OUTPUT);
  analogReference(INTERNAL2V56);
 
  
  digitalWrite(LED, HIGH);// Switch on power LED
  
  // Setting up Interrupt System
  // numbers 0 (on digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).
  attachInterrupt(0, EMERGENCYBUTTON, CHANGE);
}

void EMERGENCYBUTTON()
{
  emergencyButtonState = digitalRead(stopButtonPin);
}

void loop() {
 // read the state of the pushbutton value:
 powerButtonState = digitalRead(startButtonPin);
 
 if ( ( startupSequenceCompleted == false ) && ( powerButtonState == HIGH ) )
 {
   // Feed batteries until one battery is in the sensor grid
   
   int i;

  digitalWrite(DirPin, LOW);     // Set the direction.
  delay(100);

  for (i = 0; i<4000; i++)       // Iterate for 4000 microsteps.
  {
digitalWrite(StepPin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(StepPin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(200);      // This delay time is close to top speed for this
  }    
   
   startupSequenceCompleted = true;
 }
 
 if ( ( startupSequenceCompleted == true ) && ( powerButtonState == HIGH ) && ( emergencyButtonState == LOW ) )
 {
   // Main Loop
   
   // Check if current battery should be kept or not
   if ( oneBatteryChecked == true )
   {
     // Run Method to keep/discard battery
     DiscardKeepBattery(valueRead);
   }
   
   // Read level of battery at sensor stage
   valueRead = checkBattery();
   oneBatteryChecked = true;
   
   if ( emergencyButtonState == LOW )
   {
     // Rotate Conveyor Belts
     int i;

  digitalWrite(DirPin, LOW);     // Set the direction.
  delay(100);

  for (i = 0; i<2000; i++)       // Iterate for 4000 microsteps.
  {
digitalWrite(StepPin, LOW);  // This LOW to HIGH change is what creates the
digitalWrite(StepPin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(200);      // This delay time is close to top speed for this
  }    
     
   }
 } 
}

I used some parts from tutorials for the Stepper motor easydriver from sparkfun.

Any help will be much appreciated

1) I would not control the "emergency button" function in Software. I would make the emergency button cut the power to the motors.

2) I would put "startup sequence" code into "setup"

3)

I wrote a program and compiled it without problems but when it came to the motor just started rotating without annything else happening.

Would you be slightly more specific? Under which conditions did it start to rotate? Did you already put print statements into your code to see if it does what you expect it to do? Are you really sure that you have properly decoupled power supplies?

There is a Stepper library that makes handling stepper motors easier.

I would not control the "emergency button" function in Software. I would make the emergency button cut the power to the motors.

As in hardware and not software?

Under which conditions did it start to rotate? Did you already put print statements into your code to see if it does what you expect it to do? Are you really sure that you have properly decoupled power supplies?

It started rotating without me pressing the start button, it was starting at stopping but not depending on the booleans being completed :~ I did not put in any print statements no because to be honest I'm not quite sure how the feedback from serial print can be interpreted, this is my first electronics project. What do you mean by decoupled power supplies? :sweat_smile: I did put the solenoids and motor on their own external circuit since they require 12v, and I also tied all the ground together, both of the external power supply and the arduino

I did not put in any print statements no because to be honest I'm not quite sure how the feedback from serial print can be interpreted, this is my first electronics project.

So, if you printed a message like "Emergency stop switch pressed" when you detected that the emergency stop switch was pressed, you would not know how to interpret that message? Hard to fathom.

The presence of that message in the serial monitor when you did not press the emergency stop switch would tell you something. The absence of the message when you pressed the switch would tell you something.

For someone with no electronics experience, and, apparently no programming experience, you have far too much code already.

Start over with very simple sketches until you can assure yourself that ALL of the hardware is behaving correctly - switches are wired and read correctly, the stepper starts AND stops when it is supposed to, etc.

Only when all the hardware works, can you begin to put all the code together to make something useful.

A builder does not start putting the final trim paint on a building before checking that the foundation is square. But, that is what you are doing.

I have tested the Motor alone with a simple program and it worked. Therefore the simple code of the motor + the circuitry is correct, the solenoid, buttons and sensor are tested and functioning as well so it is not the hardware, the components all worked with their sketches. Its the program design that is giving me trouble. The hardware is fine for sure.

Im not quite sure what you are referring to when you say printing, do you mean printing to the Serial Monitor?

Im not quite sure what you are referring to when you say printing, do you mean printing to the Serial Monitor?

Where else? You didn't get the advanced Arduino model with the printer attached, did you?

Nope no printer peripherals! So should I add a serial print line in my sketch and see the response of the program on a serial monitor? If so how can I tell the difference between say, one solenoid going high and another solenoid going high?

So should I add a serial print line in my sketch and see the response of the program on a serial monitor?

How many times do you need to be told yes? Yes! Yes! Yes! Yes! Yes! Yes! Yes! Yes! Yes!

If so how can I tell the difference between say, one solenoid going high and another solenoid going high?

Gee, that's a tough one.

Serial.print("One solenoid went high!\n");
Serial.print("Another solenoid went high!\n");

All you need to do is get smart about WHERE you put the Serial.print() statements and what you put IN the Serial.print() statements.