int sensorFarRight = 52; //Declares all sensor inputs
int sensorRight = 50;
int sensorLeft = 24;
int sensorFarLeft = 22;
int sensorColor = A0; // Analog sensor input for color
int rightCA = 7; //CA Module Outputs
int leftCA = 8;
void setup () {
pinMode (rightCA, OUTPUT);
pinMode (leftCA, OUTPUT);
}
void loop() {
analogWrite (leftCA, 150);
analogWrite (rightCA, 150);
if ((sensorColor > 300) && (sensorColor < 750) && (sensorFarRight == HIGH) && (sensorFarLeft == HIGH) && (sensorRight == LOW) && (sensorLeft == LOW)) /* Color Detection
(.0049)(value) = voltage...currently 3.68 > sensor > 1.47 */
{
digitalWrite (leftCA, HIGH);
digitalWrite (rightCA, LOW);
}
if ((sensorFarRight == HIGH) && (sensorFarLeft == HIGH) && (sensorRight == LOW) && (sensorLeft == LOW)) //Split Detection
{
digitalWrite (leftCA, LOW);
digitalWrite (rightCA, HIGH);
}
if ((sensorFarRight == HIGH) && (sensorFarLeft == HIGH) && (sensorRight == HIGH) && (sensorLeft == HIGH)) //Stop Detection
{
digitalWrite (leftCA, LOW);
digitalWrite (rightCA, LOW);
}
if ((sensorRight == HIGH) && (sensorLeft == HIGH) && (sensorFarLeft == LOW) && (sensorFarRight == HIGH)) // 90 degree right turn
{
digitalWrite (rightCA, LOW);
digitalWrite (leftCA, HIGH);
}
if ((sensorRight == HIGH) && (sensorLeft == HIGH) && (sensorFarLeft == HIGH) && (sensorFarRight == LOW)) // 90 degree left turn
{
digitalWrite (rightCA, HIGH);
digitalWrite (leftCA, LOW);
}
if ((sensorRight == HIGH) && (sensorLeft == HIGH) && (sensorFarLeft == LOW) && (sensorFarRight == LOW)) // #nav Middle sensors both white
{
digitalWrite (rightCA, HIGH);
digitalWrite (leftCA, HIGH);
}
if ((sensorRight == HIGH) && (sensorLeft == LOW) && (sensorFarLeft == LOW)) // #nav Left sensor black, right sensor white
{
digitalWrite (rightCA, LOW);
digitalWrite (leftCA, HIGH);
}
if (( sensorRight == LOW ) && (sensorLeft == HIGH) && (sensorFarRight == LOW)) // #nav Right sensor black, left sensor white
{
digitalWrite (rightCA, HIGH);
digitalWrite (leftCA, LOW);
}
}
Okay so I'm doing a project for my ECE 110 class and we're building an autonomous car capable of following a white piece of tape around a black table. I'm looking to implement the PWM because I want our car to run at a constant (slow) speed, and the only way we have of doing it given to us is a variable resistor on the back and if you turn it down all the way the car doesn't have enough power to even move. I want to turn that all the way up and just change the duty cycle of the PWM to control our speed. I want to know if this implementation of the analogWrite function will work for what I want to do.
Also, any other comments about my code would be greatly appreciated. This is my first time coding ever and I know nothing about declaring functions, calling them, etc so general advice is always appreciated. I love the Arduino community!
-James