Ultrasonic sensor and motor sketch - need advice/help with code

Hi, I am looking for some advice on this sketch I wrote.

I basically combined 2 different demo sketches: an ultrasonic sensor sketch using newping.h and a motor shield demo.

I am using an Arduino Motor Shield and have connected the motor via channel A + and -. I also added code I found for changing PWM frequencies because the motor was making a high-pitched noise–the line I left active actually resolved it, though I have no clue how or why.

As an FYI, this sketch currently DOES work: a motor turns at a very low speed by default, then if something is within 120cm of the sensor, the motor turns at full speed.

My questions are:

  • How would I change this to have the motor not running by default and then run at full speed when something is within 120cm? Would I just change the analogWrite command (that I have in the else portion of my if/else statement) to 0 instead of 120?
  • What can I “clean up” in this sketch? I know this is probably pretty messy/incorrectly written, but it is working so I don’t know if I am doing something wrong.
  • Is there anything wrong with the pins I’ve set? I was worried that I might be doubling up the use of pin 12 since it’s being used by the sensor, but since it’s working I am assuming they are 2 different things?


/*-----( Import needed libraries )-----*/
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  12
#define  ECHO_PIN     11
#define MAX_DISTANCE  500 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  //TCCR2B = TCCR2B & B11111000 | B00000001;    // set timer 2 divisor to     1 for PWM frequency of 31372.55 Hz
  //TCCR2B = TCCR2B & B11111000 | B00000010;    // set timer 2 divisor to     8 for PWM frequency of  3921.16 Hz
  //TCCR2B = TCCR2B & B11111000 | B00000011;    // set timer 2 divisor to    32 for PWM frequency of   980.39 Hz
  //TCCR2B = TCCR2B & B11111000 | B00000100;    // set timer 2 divisor to    64 for PWM frequency of   490.20 Hz (The DEFAULT)
  //TCCR2B = TCCR2B & B11111000 | B00000101;    // set timer 2 divisor to   128 for PWM frequency of   245.10 Hz
  TCCR2B = TCCR2B & B11111000 | B00000110;    // set timer 2 divisor to   256 for PWM frequency of   122.55 Hz 
  //TCCR2B = TCCR2B & B11111000 | B00000111;    // set timer 2 divisor to  1024 for PWM frequency of    30.64 Hz

}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  //Serial.print("Ping: ");
  //Serial.print(DistanceIn); // Convert ping time to distance and print result 
  //Serial.print(" in     ");
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  //Serial.print("Ping: ");
  //Serial.println(" cm"); 
  if ( (DistanceCm <= 120) && (DistanceCm != 0) )
  //Serial.println("120 Cm or closer! ");
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed 
  analogWrite(3, 120);   //Spins the motor on Channel A at low speed

}//--(end main loop )---

sketch_ultrasonic_and_motor_final.ino (2.79 KB)

Please post your code.

use code tags

Ok, posted