Friday, May 31, 2013

Week 12: Working Arduino

At this stage I now set up a new Arduino circuit and wrote up a new code to control separate 2 DC motors and also to control a full rotation servo.
What I have now is the 2 L293D motor driver to control each motor by the push button. There are two push button to control each motor. While one motor is rotating the other will be disabled and while the other is push will work other wise. The reason to separate the two DC motor is becasue of the power supply. In this circuit the power from 9V battery is supplied separately. (See below Diagram)

There are also another push button to control the direction of the full rotation servo. Once the button is pressed servo will rotate clockwise after a set time it will then rotate counter clockwise for a certain seconds

Video below shows how to servo works with the push button.


Another push button is also installed to control the standard servo this one will act as a switch for electromagnet. At this stage we can not find the way to make a magnet stronger. Therefore we will not be putting it in the prototype.

The following is the code applied in the Arduino Application.

#include <Servo.h> //include servos in the code

Servo servoMain; //identify servo as a main servo
Servo servoSwitch; //identify servo as a switch servo for eletromagnet

int switchPin = 1; //push button pin for motor to go left
int switch2Pin = 2; //push button pin for motor to go right

int servoSwitchPin = 4; //push button pin for electrmagnet switch
int mainServoLongPin = 7; //push button pin for main servo to move down
int mainServoUpPin = 8; //push button pin for main servo to move up

//pin for L293D H-bridge motor drive
//left motor
int motor1Pin1 = 6; // pin 2 on L293D
int motor1Pin2 = 11; // pin 7 on L293D
int enablePin = 5; // pin 1 on L293D

//right motor
int motor2Pin1 = 10; // pin 2 on L293D
int motor2Pin2 = 9; // pin 7 on L293D
int enable2Pin = 3; // pin 1 on L293D

void setup() {
 
   pinMode(switchPin, INPUT); //set left push button as an input
   pinMode(switch2Pin, INPUT); //set right push button as an input
  
   pinMode(servoSwitchPin, INPUT); //set main servo push button as an input
   pinMode(mainServoLongPin, INPUT); //set main servo push button to move down as an input
   pinMode(mainServoUpPin, INPUT); //set main servo push button to move up as an input
 
  //set L293D pins as output pins for left motor
   pinMode(motor1Pin1, OUTPUT);
   pinMode(motor1Pin2, OUTPUT);
   pinMode(enablePin, OUTPUT);
  
   //set L293D pins as output pins for right motor
   pinMode(motor2Pin1, OUTPUT);
   pinMode(motor2Pin2, OUTPUT);
   pinMode(enable2Pin, OUTPUT);

   //set the enable pin from IC to motors as an 'off'/ disable motors
   digitalWrite(enablePin, LOW); //disable left motor
   digitalWrite(enable2Pin, LOW); //disablr right motor
}

void loop() {
 
  //if this button is pressed
  //the switch servo rotate to 0/ 180 position
  if (digitalRead(servoSwitchPin) == HIGH) {
   
  servoSwitch.attach(13);  //activate switch servo
                        
  servoSwitch.write(180); //tell servo to go to 180 position
  delay(3000); //stay for 5 seconds
  }
 
  //if the button is not pressed
  else {
       servoSwitch.write(0); //servo go back to 0 position
  }
 
  //if this button is pressed
  //main servo rotate clockwise
  //goes down and stop
  if (digitalRead(mainServoLongPin) == HIGH) {
 
  servoMain.attach(12); //activate main servo
  servoSwitch.detach();
 
  servoMain.writeMicroseconds(1700); //main servo rotate clockwise
  delay(100);
  }
 
  //if the button is not pressed
  else {
  servoMain.detach(); //deactivate main servo 
  }
 
  //if this button is pressed
  //main servo rotate anti-clockwise
  //goes up and stop
   if (digitalRead(mainServoUpPin) == HIGH) {
 
  servoMain.attach(12); //activate main servo
 
  servoMain.writeMicroseconds(1300); //rotate anti-clockwise
  delay(100);
  }
 
  //if the button is not pressed
  else {
    servoMain.detach(); //deactivate main servo 
  }
 
  //if this button is pressed
  //enable left motor
  //rotate for 2 seconds and stop
   if (digitalRead(switchPin) == HIGH) {
      digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
    
      digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
   
      digitalWrite(enablePin, HIGH); // set pin 1 on L293D high
     
    }

//if this button is not pressed
//disable all the pins
//deactivate motor
    else {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(enablePin, LOW); // set pin 1 on L293D low
    }
   
  //if this button is pressed
  //enable right motor to rotate in opposite direction
  //rotate for 2 seconds and stop
    if (digitalRead(switch2Pin) == HIGH) {
     
       digitalWrite(motor2Pin1, HIGH); // set pin 2 on L293D high
    
       digitalWrite(motor2Pin2, LOW); // set pin 7 on L293D low
    
       digitalWrite(enable2Pin, HIGH);// set pin 1 on L293D high
     
    }
   
  //if this button is not pressed
  //disable all the pins
  //deactivate motor
    else {
        digitalWrite(motor2Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor2Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(enable2Pin, LOW); // set pin 1 on L293D low
    }
}

No comments:

Post a Comment