/**
* MINI PROJECT
* - by Iris Howley -
*
* Runs two motors connected through an h-bridge.
* Motors start \'off\', but when the lights go on, then the
* motors go \'on\'. When the lights turn off, then the motors turn
* \'off.\'
**/
int hbridgeL1 = 6; //declares the first pin on the left side of the hbridge for the motor
int hbridgeL2 = 7; //declares the second pin on the left side of the hbridge for the motor
int motorLpmw = 9; // this is the pmw that will set how much power the motor is getting (speed)
int hbridgeR1 = 4;
int hbridgeR2 = 3;
int motorRpmw = 5;
int LEFT_POWER = 255;
int RIGHT_POWER = 255
;
int lightSensorPin = A0;
int lowLightValue = 1000; // stores the current lowest light sense value
int highLightValue = -1; // stores the highest light sense value
int RECORDED_LOW_LIGHT = 58; // the recorded lowest light sense value
int RECORDED_HIGH_LIGHT = 746; // the highest recorded light sense value
int status = 0; // default, motors = off
int currentLight = 50; // the amount of light we\'re current experiencing
int lightThreshold = 50; // the min amount light has to change for us to consider the lights changing
int movingForward = -1; // everytime the lights turn on/off, we\'re going to switch out direction
// there may be better ways to handle this.
unsigned long lastMillisec = 0; // To keep track of time, we need a start time. 0.
int directionChangeCycle = 4000; // Every 3 seconds, we want the direction of the spools to change
void setup() {
Serial.begin(9600);
// Declaring as outputs
pinMode(hbridgeL1, OUTPUT);
pinMode(hbridgeL2, OUTPUT);
//pinMode(motorLpmw, OUTPUT);
pinMode(hbridgeR1, OUTPUT);
pinMode(hbridgeR2, OUTPUT);
//pinMode(motorRpmw, OUTPUT);
}
void loop() {
run();
//rightMotorOn();
//leftMotorOn();
//
calibrate();
//testReverse(hbridgeR1, hbridgeR2, motorRpmw);
//moveForward(hbridgeL1, hbridgeL2, motorLpmw);
}
/**
* Runs the left and right motors. Motors start \'off\', but when the lights turn on
* the motors turn on. Lights turn off, motors turn off.
**/
void run() {
int lightSensorValue = analogRead(lightSensorPin);
Serial.print(currentLight); // debug printing
Serial.print(" -> "); // debug printing
Serial.println(lightSensorValue); // debug printing
if (status && ((currentLight-lightSensorValue) > lightThreshold)) { // we\'re on, and the lights turned off
status = 0; // change the \'status\' of the power, so we know whether to stay on/off
Serial.println("\\tTURN OFF");
}
else if (!status && ((currentLight-lightSensorValue) < -lightThreshold)) { // we\'re off and the lights turned on
status = 1;
Serial.println("\\tTURN ON");
}
currentLight = lightSensorValue;
if (status) { // continue on
if (cycleCheck(&lastMillisec, directionChangeCycle)) {
movingForward = movingForward*(-1); // switch direction!
}
if (movingForward > 0) { // if we\'re moving forward, then move forward!
rightMotorForward();
leftMotorBackward();
}
else { // otherwise, move backward.
rightMotorBackward();
leftMotorForward();
}
//TODO: put delays in here!!!
}
else { // stay stopped
analogWrite(motorLpmw, 0); // turn off left motor
digitalWrite(hbridgeL1, LOW);
digitalWrite(hbridgeL2, LOW); // I\'m messing something up. Quick fix.
analogWrite(motorRpmw, 0); // turn off right motor
}
// Fix the light value if it\'s not the recorded low/high
/**
* if (lightSensorValue < RECORDED_LOW_LIGHT) {
* lightSensorValue = RECORDED_LOW_LIGHT;
* }
* if (lightSensorValue > RECORDED_HIGH_LIGHT) {
* lightSensorValue = RECORDED_HIGH_LIGHT;
* }
**/
}
/**
* Returns true if enough time has gone by (as defined by \'cycle\').
**/
boolean cycleCheck(unsigned long *lastMillis, unsigned int cycle) {
unsigned long currentMillis = millis();
if(currentMillis - *lastMillis >= cycle)
{
*lastMillis = currentMillis;
return true;
}
else
return false;
}
/**
* Move the left motor forward.
**/
void leftMotorForward() {
// Keeps the left motor on
analogWrite(motorLpmw, LEFT_POWER);
digitalWrite(hbridgeL1, LOW); //turns the motors on
digitalWrite(hbridgeL2, HIGH);
}
/**
* Move the rightft motor forward.
**/
void rightMotorForward() {
// Keeps the right motor on
analogWrite(motorRpmw, RIGHT_POWER);
digitalWrite(hbridgeR1, LOW); //turns the motors on
digitalWrite(hbridgeR2, HIGH);
}
/**
* Move the left motor "backward".
**/
void leftMotorBackward() {
// Keeps the left motor on
analogWrite(motorLpmw, LEFT_POWER);
digitalWrite(hbridgeL1, HIGH); //turns the motors on
digitalWrite(hbridgeL2, LOW);
}
/**
* Move the rightft motor "backward".
**/
void rightMotorBackward() {
// Keeps the right motor on
analogWrite(motorRpmw, RIGHT_POWER);
digitalWrite(hbridgeR1, HIGH); //turns the motors on
digitalWrite(hbridgeR2, LOW);
}
/**
* Senses light through analog input, then outputs
* current light sensor value, as well as the current
* high and low sensor values. Use to calibrate the high/low
* values of the photo sensor.
**/
void calibrate() {
int lightSensorValue = analogRead(lightSensorPin);
if (lightSensorValue > highLightValue) {
highLightValue = lightSensorValue;
}
else if (lightSensorValue < lowLightValue) {
lowLightValue = lightSensorValue;
}
// Print values
Serial.print(lowLightValue);
Serial.print("\\t<\\tcur:");
Serial.print(lightSensorValue);
Serial.print("\\t<\\t");
Serial.println(highLightValue);
}
/**
* Tests if the hbridge/motor circuit works.
* Spins a single motor in one direction for 1000ms, and then in the other direction.
* @param pin1 the first digital pin coming from the h-bridge
* @param pin2 the second digital pin coming from the h-bridge
* @param pmwpin the pwm pin determining how much power to give the motor (0-255)
**/
void testReverse (int pin1, int pin2, int pmwpin) {
analogWrite(pmwpin, RIGHT_POWER); // this is the analog speed value for the arduino (0-255)
digitalWrite(pin1, HIGH); //turns the motors on - forwards
digitalWrite(pin2, LOW);
delay(1000); // wait
digitalWrite(pin1, LOW); //makes motor go backwards
digitalWrite(pin2, HIGH);
delay(1000); // wait
}
/**
* Tests if the hbridge/motor circuit works.
* Spins a single motor in one direction.
* @param pin1 the first digital pin coming from the h-bridge
* @param pin2 the second digital pin coming from the h-bridge
* @param pmwpin the pwm pin determining how much power to give the motor (0-255)
**/
void moveForward (int pin1, int pin2, int pmwpin) {
digitalWrite(pmwpin, RIGHT_POWER); // this is the analog speed value for the arduino (0-255)
digitalWrite(pin1, LOW); //turns the motors on - forwards
digitalWrite(pin2, HIGH);
}