Commit 9f021612 authored by Tim Soderstrom's avatar Tim Soderstrom

Added switches

parent 0e30fff3
......@@ -28,6 +28,8 @@
// Slowest Motor Will Turn
// #define MOTOR_MIN_RPM 24
#define MOTOR_MIN_PCT 50
#define FAST 100
#define SLOW 50
// How long to free spin motor when changing direction
#define COAST_MOTOR_MILLIS 1000
......@@ -42,22 +44,20 @@
/*******
* Pins
*******/
/*
const byte motorSpeed = 5;
const byte motorX = 6;
const byte motorY = 7;
*/
/* For ATTiny85 */
const byte motorSpeed = A1;
const byte motorX = A2;
const byte motorY = A3;
#define MOTOR_SPEED 0
#define MOTOR_X A2
#define MOTOR_Y A3
#define RUN_SWITCH A1
#define SPEED_SWITCH 1
/************
* Variables
************/
unsigned long previousMotorMillis = 0;
unsigned long previousCoastMillis = 0;
int motorSpeedCurrent = 0;
bool motorDirection = FORWARD;
// If we need to coast
bool motorCoast = false;
......@@ -70,14 +70,16 @@ bool motorCoasting = false;
void setup()
{
pinMode(motorSpeed, OUTPUT);
pinMode(motorX, OUTPUT);
pinMode(motorY, OUTPUT);
pinMode(MOTOR_SPEED, OUTPUT);
pinMode(MOTOR_X, OUTPUT);
pinMode(MOTOR_Y, OUTPUT);
pinMode(RUN_SWITCH, INPUT);
pinMode(SPEED_SWITCH, INPUT);
/* Start forward at start speed */
analogWrite(motorSpeed, toSpeed(MOTOR_START_PCT));
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
analogWrite(MOTOR_SPEED, toSpeed(MOTOR_START_PCT));
digitalWrite(MOTOR_X, HIGH);
digitalWrite(MOTOR_Y, LOW);
}
void loop()
......@@ -88,16 +90,23 @@ void loop()
void runMotor()
{
// byte rpm = MOTOR_RPM;
byte rpm = MOTOR_START_PCT;
//byte rpm = MOTOR_START_PCT;
byte rpm = 100;
byte directionInterval = DEFAULT_DIRECTION_INTERVAL;
bool runMotor = true;
while(true)
{
if(digitalRead(SPEED_SWITCH))
rpm = FAST;
else
rpm = SLOW;
runMotor = digitalRead(RUN_SWITCH);
if(runMotor)
controlMotor(rpm, directionInterval);
else
controlMotor(0, 0);
}
}
......@@ -108,7 +117,7 @@ void controlMotor(byte speed, byte directionInterval)
// Coast on direction change
if(motorCoast)
{
analogWrite(motorSpeed, 0);
analogWrite(MOTOR_SPEED, 0);
motorCoasting = true;
if (millis() - previousCoastMillis >= COAST_MOTOR_MILLIS && motorCoast)
{
......@@ -117,7 +126,7 @@ void controlMotor(byte speed, byte directionInterval)
}
// If we're not coasting, make sure we set the speed to whatever is being requested
else if (!motorCoast)
analogWrite(motorSpeed, toSpeed(speed));
analogWrite(MOTOR_SPEED, toSpeed(speed));
if(millis() - previousMotorMillis >= SECONDS_MS * directionInterval)
{
// Flag we need to coast the motor
......@@ -135,7 +144,7 @@ void controlMotor(byte speed, byte directionInterval)
// Now Change Direction
else if(!motorCoasting)
{
analogWrite(motorSpeed, toSpeed(speed));
analogWrite(MOTOR_SPEED, toSpeed(speed));
changeDirection();
motorCoast = false;
previousMotorMillis = millis();
......@@ -145,9 +154,9 @@ void controlMotor(byte speed, byte directionInterval)
/* If directionInterval is zero, never change speed */
else
{
analogWrite(motorSpeed, toSpeed(speed));
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
analogWrite(MOTOR_SPEED, toSpeed(speed));
digitalWrite(MOTOR_X, HIGH);
digitalWrite(MOTOR_Y, LOW);
motorDirection = FORWARD;
}
}
......@@ -163,14 +172,14 @@ void changeDirection()
{
if(motorDirection == FORWARD)
{
digitalWrite(motorX, LOW);
digitalWrite(motorY, HIGH);
digitalWrite(MOTOR_X, LOW);
digitalWrite(MOTOR_Y, HIGH);
motorDirection = REVERSE;
}
else
{
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
digitalWrite(MOTOR_X, HIGH);
digitalWrite(MOTOR_Y, LOW);
motorDirection = FORWARD;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment