This is the beginning to controlling the 4 motors of the ROV. I wrote this code so that the speed of a motor, using port M1 on the Adafruit MotorShield, is controlled via the position of the potentiometer. Its very rough but its gets the job done!
Team-Subzero
============
/*
William Kammerer
11/21/14
Adafruit Motorshield V2 Team SubZero
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
//Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//Try to designate the address of the shield as 0x1 or 0x61
//Adafruit_MotorShield AFMS = Adafruit_MotorShield (0x1)
//Adafruit_MotorShield AFMS = Adafruit_MotorShield (0x61)
//Select which port M1, M2, M3 or M4
//In this case we are using port M1
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
//To select another motor on a port you can use
//Adafruit_DCMotor *Motor# = AFMS.getMotor(2);
void setup()
{
//Set up Serial library at 9600 bits per second
Serial.begin(9600);
//Print message to serial monitor
Serial.println("SubZero Motor Controller");
//Create AFMS with a default fequency 1.6KHz
//Also can be created using a different frequency i.e. 1.0KHz
//AFMS.begin(1000)
AFMS.begin();
//Turn motor on
Motor1->run(FORWARD);
Motor1->run(RELEASE);
}
void loop()
{
int sensorValue = analogRead(A0);
float dot = sensorValue * (255.0 / 1023.0);
delay(50);
Serial.println(dot);
//Set the speed of the motor, from the position of the pot
Motor1->setSpeed(dot);
Motor1->run(FORWARD);
}
-WK
No comments:
Post a Comment