/* _______________________________________________________ * C Y T R O N M O T O R C O N T R O L L E R * 3- IR Motor and Servo Controller (Dual Motor) * Created 6/18/2021 * Ron Kessler * UCI Critter Class * Locomotion Controller * * _______________________________________________________ * Updated: * 6/18/2021 Started with #2 Basic IR Motor controller * This will also operate 1 parallax Standard Servo * * Supporting Documentation: * This folder contains the datasheet and overview of the * motor shield and images of my test setup. * * MOTOR DRIVER LIBRARY REQUIRED: * "CytronMotorDriver.h" * 1. Go to Tools | Manage Libraries | Type in "Cytron 3A Motor Drivber Shield". * 2. Install the shield driver and the drivers Library list below it * 3. Restart Arduino IDE. * * DESCRIPTION: * This is a test of the Cytron DC motor controller shield * that is attached to an Arduino UNO. * * Refer to project #2 in this folder for more setup info. * The board is powered by 1500mAh AA.Drone2 Li-Ion battery * The motors move forward/backwards and at different speeds. * The battery powers the UNO and the motor shield without the * need for external wires. * * The battery connections and motor connections are made on the * shield. Take care to wire the motors so they run the direction * you want. Simply reverse the motor wires if needed. * * The motors/servos are controlled via PWM. * * OUR JUMPERS CONNECTIONS SETUP: * The jumpers on the shield are set as follows: * Make sure the jumpers on the board are connected as follows: * Motor 1 PW1 = D3 * Motor 1 DIR = D4 * * Motor 2 PWM = D6 * Motor 2 DIR = D7 * ******************************************** * IR notes * DEMO HOW TO DETECT SONY REMOTE IR KEY CODES AND RUN DC MOTOR FROM YOUR KIT * Codes from 12 bits 1 byte (These coincide with my IR decode report * Channel Up = 144 16 forward * Channel Dn = 145 17 backwards * Volume Up = 146 18 turn right * Volume Dn = 147 19 turn left * Mute 148 20 MUTE used to stop critter * * Servo#1 Keys 2 1 CW Remote keys are 0-based. Button 1 sends a 0 so don't use it! * 3 2 CCW * 5 4 Stop * * * Requires IRremote library (ver 3.3) * Using the IR receiver in your study group kit: In case you need a review of setting up the IR part: 1. Place the IR module in a breadboard with the sensor facing you. 2. Connect a green wire from the leftmost pin to GND on Arduino. 3. Connect a RED wire from the center pin of the sensor to 5V on Arduino. 4. Connect another wire from the right pin to pin 2 of the Arduino. This is the signal pin. 5. Make sure you have the IRRemote library installed on your computer. I use ver 3.3 as of this writing. * ****************S T A R T O F C O D E*********************** */ //---make sure to add the new library #include "CytronMotorDriver.h" //---now add the IR Remote library #include //ver 3.3 As of 6/15/2021 //---now add library for the servos. Included in Arduino IDE #include //---instantiate new servo object Servo myServo; //--- store current servo direction and define pulse width int myDirection = 1; const int CW = 1300; //1.3ms const int CCW = 1700; //1.7ms const int STOP = 1500; //1.5ms //---Create motor objects and configure them. PWM_DIR is defined in the Cytron Library. CytronMD leftMotor(PWM_DIR, 3, 4); // Use PWM and Direction control. PWM 1 = Pin 3, DIR 1 = Pin 4.) CytronMD rightMotor(PWM_DIR, 6, 7); // PWM 2 = Pin 6, DIR 2 = Pin 7. //---define rpm and direction -255 = 100% duty cycle CCW. 255 = 100% CW const int haltMotor = 0; const int fullSpeedCW = 255; const int fullSpeedCCW = -255; const int timeDelay = 500; //.5 seconds between actions //---define our sensor data pin as a constant(const). const byte IR_RECEIVE_PIN = 11; //---define a variable to hold IR code which is detected unsigned int codeReceived = 0; void setup() { //---open serial monitor to check motor rotation. Reverse motor wires before changing code Serial.begin(9600); //---Start our IR receiver on the arduino. We pass in two arguments: pin# and system command // to ignore blinking the onboard LED when a message is received. IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK); myServo.attach(10); // control the servo on pins 9 or 10 only!!! Long Story. } void loop() { //---while the IR sensor is active, listen for a new code. // If one is detected then take the appropriate action while(!(IrReceiver.decode())); //---if data is being received then decode it, print to the monitor, and keep listening for more! if (IrReceiver.decode()) { codeReceived = IrReceiver.decodedIRData.command; //decodedRawData; Serial.println(codeReceived); //for testing switch (codeReceived) { case 1: //#2 button ServoCW(); break; case 2: //#3 button ServoCCW(); break; case 4: //#5 button ServoHalt(); break; case 16: Stop(); Forward(); break; case 17: Stop(); Backwards(); break; case 18: Stop(); TurnRight(); break; case 19: Stop(); TurnLeft(); break; case 20: //mute button stops motors Stop(); break; } } IrReceiver.resume(); // Receive the next value } //---------------Helper Functions---------------- //---move bot motors forward void Forward() { leftMotor.setSpeed(fullSpeedCCW); rightMotor.setSpeed(fullSpeedCW); Serial.println("Forward"); return; } //---move bot motors backwards void Backwards() { leftMotor.setSpeed(fullSpeedCW); rightMotor.setSpeed(fullSpeedCCW); Serial.println("Backwards"); return; } void TurnRight() { leftMotor.setSpeed(haltMotor); rightMotor.setSpeed(fullSpeedCW); Serial.println("Turn Right"); return; } void TurnLeft() { leftMotor.setSpeed(fullSpeedCCW); rightMotor.setSpeed(haltMotor); Serial.println("Turn Left"); return; } void Stop() { leftMotor.setSpeed(haltMotor); rightMotor.setSpeed(haltMotor); Serial.println("Stopped"); delay(timeDelay); return; } void ServoCW() { ServoHalt(); myServo.write(CW); return; } void ServoCCW() { ServoHalt(); myServo.write(CCW); return; } void ServoHalt() { myServo.write(STOP); return; } //------------E N D O F C O D E------------------