Main Code, V1
/*
* QT
* Description:
* Author: Nicholas Stone
* Date: 1/29/2017
* Version: 1
*/
const int motor1Pin = D3; // H-bridge leg 1
const int motor2Pin = D4; // H-bridge leg 2
const int enablePin = D5; // H-bridge enable pin
const int phototransPin = A0;
int photoSampleRate = 100;
int light = 0;
int dcMotorStatus = 0;
const int photoLightThres = 2000;
const int bodyLoopCounter = 10;
void setup() {
Serial.begin(9600);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
//pinMode(phototransPin,INPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}
//If switch is high, turn the motor one way by taking one H-bridge pin high and the other low.
//If the switch is low, reverse the direction by reversing the states of the two H-bridge pins.
void loop() {
int i = 0;
light = analogRead(phototransPin);
while(light<photoLightThres) {
if(dcMotorStatus==0) {
digitalWrite(enablePin, HIGH);
dcMotorStatus = 1;
}
while(i<bodyLoopCounter && light<photoLightThres) {
if(i==0){
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
i++;
light = analogRead(phototransPin);
delay(photoSampleRate);
Serial.print("CW Counter Value: ");
Serial.println(i);
}
while(i<2*bodyLoopCounter && light<photoLightThres) {
if(i==bodyLoopCounter){
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
i++;
light = analogRead(phototransPin);
delay(photoSampleRate);
Serial.print("CCW Counter Value: ");
Serial.println(i);
}
if(i==2*bodyLoopCounter && light<photoLightThres){
i = 0;
}
}
if(dcMotorStatus==1)
{
dcMotorStatus = 0;
digitalWrite(enablePin, LOW);
}
Serial.print("Light Value: ");
Serial.println(analogRead(phototransPin));
delay(photoSampleRate);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .