| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Arduino Bot- Sample Code

Page history last edited by Jonathan Dietz 10 years, 11 months ago

 

 

 

 

/* Sample sketch code for Arduino Car project

Arduino is supplied with 9V power; Motors with separate 6V supply to pin 8 on L293D

 

L293D H-Bridge Pins:

 

1- enable/speed left motor

2- Left Motor Input 1

3- Left Motor Output to + motorlead

4- GND

5- GND

6- Left Motor Output 2 to - motor lead

7- Left Motor Input 2

8- Motor Power + 6V to AA Battery Pack

 

9-  enable/speed for right motor

10- Right Motor Input 1

11- Right Motor Output to + motor lead

12- GND

13- GND

14- Right Motor Output 2 to - motor lead

15- Right Motor Input 2

16- 5 Volt Power from Arduino

 

Ultrasound Sensor:

 

Vcc to +5V

GND to GND

Trig Pin 7

Echo Pin 6

 

------------------------------------------------

*/

 

// Assign Pins for Left Motor

 

 

int motorleftPin1 = 3; // pin 2 on L293D
int motorleftPin2 = 4; // pin 7 on L293D


int enableleftPin = 5; // pin 1 on L293D

 

//assign pins for right motor

 

int motorrightPin1 = 9; // pin 10 on L293D
int motorrightPin2 = 10; // pin 15 on L293D


int enablerightPin = 11; // pin 9 on L293D

 

 

//set up ultrasound pins on HC-SR04 Ultrasound Sensor and define variables

int echoPin=6;

int trigPin=7;//set up ultrasound pins on HC-SR04
int led = 13;//assign LED pin
int threshold = 25; 
//assign threshold distance
int duration, cm;


void setup() {

// set the pins you're using as outputs:

 


pinMode(motorleftPin1, OUTPUT);
pinMode(motorleftPin2, OUTPUT);
 
pinMode(enableleftPin, OUTPUT); // or analogWrite(enableleftPin, speed 0-255); to adjust speed
 
pinMode(motorrightPin1, OUTPUT);
pinMode(motorrightPin2, OUTPUT);
pinMode(enablerightPin, OUTPUT); // or analogWrite(enablerightPin, speed 0-255); to adjust speed
 
// set enablePins high so that motor can turn on; this can also be used as a speed control using the analogWrite command

 

digitalWrite(enableleftPin, HIGH);
digitalWrite(enablerightPin, HIGH);

Serial.begin (9600); //open serial communications
// set up pins for HC-SR04 ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(led, OUTPUT); 
}

void loop() {

forward();
delay (2000); //go forwards for 2 seconds

backward(); //go backwards for 2 seconds
delay (2000);
}
 
 
void forward() {      //define going forwards function

digitalWrite(motorleftPin1, LOW);      // set pin 2 on L293D low
digitalWrite(motorleftPin2, HIGH);      // set pin 7 on L293D high

digitalWrite(motorrightPin1, LOW);     // set pin 2 on L293D low
digitalWrite(motorrightPin2, HIGH);      // set pin 7 on L293D high
}
 
void backward() {                                        //define going backwards function

digitalWrite(motorleftPin1, HIGH);           // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW);           // set pin 7 on L293D low

digitalWrite(motorrightPin1, HIGH);      // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW);      // set pin 7 on L293D low
}
//this function measures distance
 
void getDistance() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite(trigPin, LOW);
duration= pulseIn(echoPin, HIGH);
cm= duration /29 /2;
delay(100);
}
 
/* sample ultrasound code-----------------------------------------------
int echoPin=6;
int trigPin=7;//set up ultrasound pins on HC-SR04
int led = 13;//assign LED pin
int threshold = 25;
//assign threshold distance
void setup() {
Serial.begin (9600); //open serial communications
pinMode(trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(led, OUTPUT);
//describe inputs and outputs
}
void loop () {
int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite(trigPin, LOW);
duration= pulseIn(echoPin, HIGH);
cm= duration /29 /2;
Serial.print(cm);
Serial.println("cm");
delay(100);

if (cm > threshold) {
digitalWrite(led, HIGH); //turn on LED
}
else {
digitalWrite(led,LOW); //otherwise turn it off
}
}
*/

Comments (0)

You don't have permission to comment on this page.