Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
#define Motor1A 10 // H-Bridge input 1 #define Motor1B 11 // H-Bridge input 2 #define enable 3 // H-Bridge motor 1 enable#define Motor2A 9 // H-Bridge input 3 #define Motor2B 8 // H-Bridge input 4 #define enable2 5 // H-Bridge motor 2 enable#define ledPin 13 // sets the indicator led to pin 13#define pingPin 12void straight() // go straight function{ digitalWrite(enable, HIGH); digitalWrite(Motor1A, HIGH); digitalWrite(Motor1B, LOW); digitalWrite(enable2, HIGH); digitalWrite(Motor2A, HIGH); digitalWrite(Motor2B,LOW);}void turnRight() // turn right function { digitalWrite(enable, HIGH); digitalWrite(Motor1A, LOW); digitalWrite(Motor1B, HIGH); digitalWrite(enable2, HIGH); digitalWrite(Motor2A, HIGH); digitalWrite(Motor2B,LOW);}long duration; long ping(int numIter = 3) { duration = 0; // reset duration value for (int i = 0; i < numIter; i++) { pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration += pulseIn(pingPin, HIGH); // add the current reading to the total } return duration / 74 / 2 / numIter; // return averaged value in inches} void avoideWalls(){ if(duration < 10) // if the distance is less than 10 inches { digitalWrite(ledPin, HIGH); // turn on the LED turnRight(); // turns Walbot right using turnRight function delay(100); } else // otherwise { digitalWrite(ledPin, LOW); // turn off the LED straight(); // go straight using the straight function }} void setup(){ pinMode(Motor1A, OUTPUT); // sets motor pin 1A as output pinMode(Motor1B, OUTPUT); // sets motor pin 1B as output pinMode(Motor2A, OUTPUT); // sets motor pin 2A as output pinMode(Motor2B, OUTPUT); // sets motor pin 2B as output pinMode(enable, OUTPUT); // sets enable2 as output pinMode(enable2, OUTPUT); // sets enable as output pinMode(pingPin, OUTPUT); // sets pingPin as output pinMode(pingPin, INPUT); // sets pingPin as output pinMode(ledPin, OUTPUT); // sets ledPin as output straight(); // initializes Walbot to go straight}void loop() { digitalWrite(pingPin, HIGH); // turns on sonar ping(); avoideWalls(); // tells Walbot not to run into inanimate objects by calling avoideWalls function delay(20); // delay for 20 milliseconds}
long ping(int numIter = 3)
void loop() { digitalWrite(pingPin, HIGH); // turns on sonar ping(); avoideWalls(); // tells Walbot not to run into inanimate objects by calling avoideWalls function delay(20); // delay for 20 milliseconds}
void loop() { Serial.print("ping))):\t"); Serial.println(ping());}
Code: [Select]void loop() { digitalWrite(pingPin, HIGH); // turns on sonar ping(); avoideWalls(); // tells Walbot not to run into inanimate objects by calling avoideWalls function delay(20); // delay for 20 milliseconds}
Quote from: jlowe64 on October 21, 2010, 10:10:25 AMCode: [Select]void loop() { digitalWrite(pingPin, HIGH); // turns on sonar ping(); avoideWalls(); // tells Walbot not to run into inanimate objects by calling avoideWalls function delay(20); // delay for 20 milliseconds}Remove the line: "digitalWrite(pingPin, HIGH);"In your "ping();" function, you already have all the code you need to run the Ping sensor perfectly. Your ping() function however leaves the pin your ping sensor is attached to in INPUT mode, and when your main loop loops, you set the pin to HIGH.When you set a pin that is set for Input to HIGH, this actually enables an internal 20k pull-up resistor on that pin: http://arduino.cc/en/Tutorial/DigitalPins