Motor controller program help

Hello all, I myself am fairly new to Arduino. I have already built several example projects
and am keen to expand on several project ideas I have.

The project I am working on currently, is a motor controller system

This will simulate the drive controls used in conveyor systems and other industrial applications.

The system will have 3 push buttons that activate the following:

Button 1-Motor start
Button2-Motor stop
Button3-Emergency stop

The system will also have 3 LEDs that indicate the following:

Green LED-Motor running
Orange LED-Motor stopped
Red LED-Emergency stop activated

The system runs as follows:

Press the start button, motor runs and green LED turns on.
Press the stop button, the motor stops and the red LED turns on.
Press the emergency stop button, the motor stops and both the orange and red LEDs turn on.

I have already written most of the code, however I am having a few issues.

I am unsure of the coding needed to start, stop, and E stop the motor using single momentary button pushes.
I have been experimenting with the "Switch code" to activate either a green or red LED but I haven't had much luck.

I have a Freetronics 2 ch motor shield that I will be using to control the motor. The example coding however, only seems to show how to make the motor run using commands from the serial monitor.

Below is the coding I have done so far.

//Arduino motor drive system
//By Paul Coller
/*The purpose of this sketch is to simulate the control system of a conveyor
drive system.
It comprises 3 inputs (motor start, motor stop, emergency stop) and
4 outputs (motor drive, motor running green LED, motor stopped orange LED,
emergency stop activated red LED)
When Button 1 is pressed, the motor starts and the green LED lights up.
When Button 2 is pressed, the motor stops and the orange LED lights up.
When Button 3 is pressed, the motor(if running) stops and the red LED lights up.

*/

int LED_PIN1 = 8;
int LED_PIN2 = 9;
int LED_PIN3 = 10;
int BUTTON_PIN1 = 11;
int BUTTON_PIN2 = 12;
int BUTTON_PIN3 = 13;
int motorPin = 7;
int val = 0;

void setup() {
pinMode(motorPin, OUTPUT);//Motor power
pinMode(LED_PIN1, OUTPUT);//Motor running
pinMode(LED_PIN2, OUTPUT);//Motor stopped
pinMode(LED_PIN3, OUTPUT);//Emergency stop activated

pinMode(BUTTON_PIN1, INPUT);//Start motor
pinMode(BUTTON_PIN2, INPUT);//Stop motor
pinMode(BUTTON_PIN3, INPUT);//Emergency Stop
}

void loop()

{
val = digitalRead(BUTTON_PIN1);
val = digitalRead(BUTTON_PIN2);
val = digitalRead(BUTTON_PIN3);
if (val == HIGH) {
digitalWrite(motorPin, HIGH);
digitalWrite(LED_PIN1, HIGH);
digitalWrite(LED_PIN2, LOW);
digitalWrite(LED_PIN3, LOW);
} else if (val == HIGH) {
digitalWrite(motorPin, LOW);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, HIGH);
digitalWrite(LED_PIN3, LOW);
} else (val == HIGH) {
digitalWrite(motorPin, LOW);
digitalWrite(LED_PIN1, LOW);
digitalWrite(LED_PIN2, HIGH);
digitalWrite(LED_PIN3, HIGH);
}
}

I hope I have explained things clearly

Thanks in advance

Paul

Hi,
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit?

Also your emergency stop should be a hardware not a software implementation, that is it should disconnect all power supplies to the output devices via a switch that is not under software control.

Tom..... :slight_smile:

I think you might just want two more variables to read the switches: at the moment, this:

  val = digitalRead(BUTTON_PIN1);
  val = digitalRead(BUTTON_PIN2);
  val = digitalRead(BUTTON_PIN3);

... means that any button press will set val as high, so this:

  if (val == HIGH) {
    digitalWrite(motorPin, HIGH);
    digitalWrite(LED_PIN1, HIGH);
    digitalWrite(LED_PIN2, LOW);
    digitalWrite(LED_PIN3, LOW);

... will always be the part that runs.

So perhaps val1, val2 and val3 will do the trick.

edit.... that said, if you haven't used pulldown resistors, there is no guarantee a pin is low, and might read high even if it's not pressed. Even easier, is to use the built in pull UP resistors, saves a bit of circuit building. But then your logic will be reversed, the pressed button will be low and you'll need to make this:

  if (val == HIGH) {

.... look like this:

  if (val == LOW) {

I would do as JimboZA suggests and wire your buttons from the Arduino pins to GND. Then,

//Arduino motor drive system
//By Paul Coller
/*The purpose of this sketch is to simulate the control system of a conveyor
  drive system.
  It comprises 3 inputs (motor start, motor stop, emergency stop) and
  4 outputs (motor drive, motor running green LED, motor stopped orange LED,
  emergency stop activated red LED)
  When Button 1 is pressed, the motor starts and the green LED lights up.
  When Button 2 is pressed, the motor stops and the orange LED lights up.
  When Button 3 is pressed, the motor(if running) stops and the red LED lights up.

*/

#define ACTIVATED LOW

byte StartLED = 8;
byte StopLED = 9;
byte EmergencyLED = 10;
byte StartButton = 11;
byte StopButton = 12;
byte EmergencyButton = 13;
byte motorPin = 7;
byte val = 0;

void setup() {
pinMode(motorPin, OUTPUT);//Motor power
pinMode(StartLED, OUTPUT);//Motor running
pinMode(StopLED, OUTPUT);//Motor stopped
pinMode(EmergencyLED, OUTPUT);//Emergency stop activated

pinMode(StartButton, INPUT_PULLUP);//Start motor
pinMode(StopButton, INPUT_PULLUP);//Stop motor
pinMode(EmergencyButton, INPUT_PULLUP);//Emergency Stop
}

void loop()

{


  if (digitalRead(StartButton) == ACTIVATED) {
    digitalWrite(motorPin, HIGH);
    digitalWrite(StartLED, HIGH);
    digitalWrite(StopLED, LOW);
    digitalWrite(EmergencyLED, LOW);
  } else if (digitalRead(StopButton) == ACTIVATED) {
    digitalWrite(motorPin, LOW);
    digitalWrite(StartLED, LOW);
    digitalWrite(StopLED, HIGH);
    digitalWrite(EmergencyLED, LOW);
  } else if (digitalRead(EmergencyButton) == ACTIVATED) {
    digitalWrite(motorPin, LOW);
    digitalWrite(StartLED, LOW);
    digitalWrite(StopLED, HIGH);
    digitalWrite(EmergencyLED, HIGH);
  }
}

tylernt:
I would do as ZJimboZA suggests

JimboZA:

tylernt:
I would do as ZJimboZA suggests

I really like the letter 'Z'?

If I lived 600km North of where I do, "Zimbo" would be a good name....