Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
#define pingPin // define it long duration, inches, cm; pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH); // convert the time into a distance inches = duration / 74 / 2; cm = microseconds / 29 / 2;
/*This is a program by Brandon Sweeney to control the autonomous robot Walbot, it implements the LV MAX EZ1 sonar rangefinder and outputs the voltage signal sent from the rangefinder and its conversion in inches. This data is then used to control the actions of Walbot.This material is free, just give me credit for using it*/int Motor1A = 10; // H-Bridge input 1 int Motor1B = 11; // H-Bridge input 2 int enable = 3; // H-Bridge motor 1 enableint Motor2A = 9; // H-Bridge input 3 int Motor2B = 8; // H-Bridge input 4 int enable2 = 5; // H-Bridge motor 2 enableint ultraAn = 0; // select the input pin for the ultrasonic sensorint UltraEnable = 12; // sets enable to pin 12int ledPin = 13; // sets the indicator led to pin 13float val[10]; // array to store first 10 values coming from the sensorfloat valSum=0; // sum of first 10 readingsfloat valAvg=0; // average of the first 10 readingsfloat dist = 0; // variable to store the converted distance in inches// ********************Guns don't kill people. Chuck Norris kills People. **************************//void straight() // go straight function{ digitalWrite(enable, HIGH); digitalWrite(Motor1A, HIGH); digitalWrite(Motor1B,LOW); digitalWrite(enable2, HIGH); digitalWrite(Motor2A, HIGH); digitalWrite(Motor2B,LOW);}//************************ Chuck Norris does not sleep. He waits.********************************//void turnRight() // turn right function { digitalWrite(enable, HIGH); digitalWrite(Motor1A, LOW); digitalWrite(Motor1B,HIGH); digitalWrite(enable2, HIGH); digitalWrite(Motor2A, HIGH); digitalWrite(Motor2B,LOW);}//************When the Boogeyman goes to sleep every night, he checks his closet for Chuck Norris.****************//float distCalc() // distance calculating function converts analog input to inches{ valSum = 0; // resets valSum to zero after every loop for (int i=0; i<=9; i++) // for loop to gather first 10 int's { val[i] = analogRead(ultraAn); // read the value from the sensor valSum = (val[i]+valSum); // adds the first 10 values for averaging } digitalWrite(UltraEnable, LOW); // disables sonar valAvg = (valSum/10); // calculates the average dist = ((valAvg/204.8)/.009765625); // converts the analog value to distance in inches return dist; // return the distance values to the rest of the program}//********Chuck Norris is the reason why Waldo is hiding.********Chuck Norris counted to infinity - twice.********//void avoideWalls(){ if(dist < 16) // if the distance is less than 20 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 }}//*****Chuck Norris can slam a revolving door.******Chuck Norris does not get frostbite. Chuck Norris bites frost*****//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(UltraEnable, OUTPUT); // sets enable as output pinMode(ledPin, OUTPUT); // sets ledPin as output straight(); // initializes Walbot to go straight}//*****Chuck Norris can win a game of Connect Four in only three moves.******Chuck Norris can divide by zero.*****//void loop() { digitalWrite(UltraEnable, HIGH); // turns on sonar distCalc(); // gets the distance value from distCalc function avoideWalls(); // tells Walbot not to run into inanimate objects by calling avoideWalls function delay(20); // delay for 20 milliseconds}
long duration;long ping(int numIter) { 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}
int Motor1A = 10; // H-Bridge input 1 int Motor1B = 11; // H-Bridge input 2 int enable = 3; // H-Bridge motor 1 enableint Motor2A = 9; // H-Bridge input 3 int Motor2B = 8; // H-Bridge input 4 int enable2 = 5; // H-Bridge motor 2 enableint ultraAn = 0; // select the input pin for the ultrasonic sensorint UltraEnable = 12; // sets enable to pin 12int ledPin = 13; // sets the indicator led to pin 13float val[10]; // array to store first 10 values coming from the sensorfloat valSum=0; // sum of first 10 readingsfloat valAvg=0; // average of the first 10 readingsfloat dist = 0; // variable to store the converted distance in inches
#define a b// is equivalent toint a = b;// for pin def, in any case where the pin number will not change during runtime.
if (dist < 16) // if the distance is less than 20 inches
#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}