Basic PWM Question and General Code Advice

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

It should work.

Sure, i dont know what var Resistor you have there and so on, bu basically...

You chose 7 / 8 as the PWM analogWrite pins.

Be aware that pin 7/8 are NOT capable of PWM...

Only 3, 5, 6, 9, 10, and 11 provide 8-bit PWM output with the analogWrite() function.

//EDIT

Are you using a MEGA?

Be aware that pin 7/8 are NOT capable of PWM...

Well, the OP mentioned pin 50, and although they didn't say, this suggest a Mega, and on the Mega, PWM is available on pins 2 to 13.

Quote:
Be aware that pin 7/8 are NOT capable of PWM...

Well, the OP mentioned pin 50, and although they didn't say, this suggest a Mega, and on the Mega, PWM is available on pins 2 to 13.

Yeah, i just saw that, but he didnt mention it explicitly :slight_smile: Anyways...

Yeah it's a Mega 2560, shoulda mentioned that explicitly. Basically I was wondering if PWM would be applied after all the logic, before, or if it didn't mater. I guess now that I think about it all should be fine.

Thanks for your help you guys - anyone have any comments on how I could have more efficiently set up the Boolean logic for all the other cases.

//

EDIT: If the sensors output ~4v for high and low is generally <1v will I need to use the analogReference(INTERNAL2v56) to set the reference voltage for high/low at 2.56 instead of 5?

I have been searching and searching; Does someone know if there is some code to PWM an analog pin? Some sort of pseudo PWM trick?

I have used up all the legit PWMs and am hoping someone has seen this done before drumming it up from scratch. :wink:

Thanks!

You haven't said what sort of frequency you need, or how busy the processor is.

Not very busy; I have a simple RGB random color blending circuit. I am using analogWrite() so I believe ~ 490 Hz is the default (per analogWrite() - Arduino Reference) Anything over 100 Hz should be undetectable by our wonderfully 'slow' eyes. :wink: