Basic PWM Question and General Code Advice


code: [select]
int sensorfarright = 52; //declares sensor inputs
int sensorright = 50;
int sensorleft = 24;
int sensorfarleft = 22;
int sensorcolor = a0; // analog sensor input 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 i'm doing project ece 110 class , we're building autonomous car capable of following white piece of tape around black table. i'm looking implement pwm because want our car run @ constant (slow) speed, , way have of doing given variable resistor on , if turn down way car doesn't have enough power move.  i want turn way , change duty cycle of pwm control our speed.  i want know if implementation of analogwrite function work want do.

also, other comments code appreciated. first time coding ever , know nothing declaring functions, calling them, etc general advice appreciated.  i love arduino community!

-james

it should work.

sure, dont know var resistor have there , on, bu basically...

http://www.arduino.cc/en/tutorial/pwm

you chose 7 / 8 pwm analogwrite pins.

be aware pin 7/8 not capable of pwm...

only  3, 5, 6, 9, 10, , 11  provide 8-bit pwm output analogwrite() function.

//edit

are using mega?


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Basic PWM Question and General Code Advice


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums