//-- Atmega 168, 328 - Arduino 1.0 IDE //-- Fred Wolflink 2-23-2012 // // runs DC motor from SN754410 motor control IC and demonstrates // use of functions for Forward, Reverse, Stop and Coast (off) // //--- Set up: > IC pins 4, 5, 12, 13 to ground; 8, 16 to +5v; motor to 3 & 6 // > D3 to IC pin 1 (EN); D4 to pin 2 (1A); D2 to pin 7 (2A) // > switch pulls B0 low when closed // // D3 D4 D2 Motor Ports // EN 1A 2A 754410 pin name // 1 2 7 754410 pin // MOTOR CONTROL LOGIC // 1 0 0 BRAKE // 1 1 0 FORWARD // 1 0 1 REVERSE // 1 1 1 BRAKE // 0 - - OFF - COAST // // -- Wire LED from Digital Pin 7 (pin 13 on the chip) thru a 330 ohm resistor to ground // -- Wire a switch between Digital Pin 5 (pin 11 on the chip) and ground // // 2 options coded here - switch pressed - goes forward speed varies with analog input on A5 // otherwise goes backward for 1 sec, stops for 2 secs, forward 1 sec, coasts 2 secs // #include <ms_arduino.h> // included software libraries to use #include "lcd.h" // define arguments to function #define FORWARD 1 #define REVERSE 0 #define BRAKE 2 #define COAST 3 // variables int buttonState = 0; // create variable for storing "state" of the button int sensorValue = 0; // variable to store the value coming from the sensor // define pins for 754410 int EN = 3; // ENABLE is Digital Pin 4 (IC pin 6) (to motor control IC pin 1) int M1A = 4; // M1A is Digital Pin 3 (IC pin 5) (to motor control IC pin 2) int M2A = 2; // M2A is Digital Pin 2 (IC pin 2) (to motor control IC pin 7) // define other pins int sensorPin = A5; // select the input pin for the light sensor int ledPin = 7; // variable for LED pin int buttonPin = 5; // variable for button pin void setup(){ pinMode(EN, OUTPUT); pinMode(M1A, OUTPUT); pinMode(M2A, OUTPUT); pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH); // pullup_on lcd_init(); // initialize LCD screen } void loop() { buttonState = digitalRead(buttonPin); // "Read" the voltage on the pin (check switch) delay(100); // delay to debounce switch if (buttonState == LOW) { // switch is closed lcd_clear(); lcd_instruction(GOTO_LINE1); lcd_text("Motor Running!"); // put text on the LCD screen delay(10); // read the value from the sensor: sensorValue = analogRead(sensorPin); digitalWrite(M1A, HIGH); // Motor Control pins 1, 0 (FORWARD) digitalWrite(M2A, LOW); sensorValue = sensorValue >> 4; // scale 10 bits to 8 bits for analog out analogWrite(EN, sensorValue); // PWM output to motor digitalWrite(ledPin, HIGH); // turn LED on: } else { lcd_clear(); // switch open, run motor sequence lcd_instruction(GOTO_LINE1); lcd_text("reverse"); motor(REVERSE); delay(1000); lcd_clear(); lcd_instruction(GOTO_LINE1); lcd_text("brake"); motor(BRAKE); delay(2000); lcd_clear(); lcd_instruction(GOTO_LINE1); lcd_text("forward"); motor(FORWARD); delay(1000); lcd_clear(); lcd_instruction(GOTO_LINE1); lcd_text("coast"); motor(COAST); delay(2000); } } void motor(char direction) { if (direction == 1) { // motor FORWARD digitalWrite(EN, HIGH); // ENABLE high digitalWrite(M1A, HIGH); // Motor Control pins 1, 0 digitalWrite(M2A, LOW); } else if (direction == 0) { // motor REVERSE digitalWrite(EN, HIGH); // ENABLE high digitalWrite(M1A, LOW); // Motor Control pins 0, 1 digitalWrite(M2A, HIGH); } else if (direction == 2) { // motor BRAKE digitalWrite(EN, HIGH); // ENABLE high digitalWrite(M1A, HIGH); // Motor Control pins 1, 1 digitalWrite(M2A, HIGH); } else { // motor OFF - COAST digitalWrite(EN, LOW); // ENABLE low } // end IF-ELSE } |