You are on page 1of 5

1) motor

int p[]={9,2,4};

byte incoming;

static byte t=0;

String n=String("");

void zero( byte t)

analogWrite(p[0],t);

void setup()

pinMode(p[0],OUTPUT);

pinMode(p[1],OUTPUT);

pinMode(p[2],OUTPUT);

Serial.begin(9600);

Serial.println("Enter 1 for clockwise or 2 for anticlockwise");

zero(0);

byte insert()

while(n=="")

Serial.setTimeout(500);
n=Serial.readString();

incoming=n.toInt();

n="";

return incoming;

void loop()

incoming=insert();

Serial.println(incoming);

if(incoming==1)

Serial.println("Enter the speed from 0 to 255");

t=insert();

Serial.println(t);

zero(t);

digitalWrite(p[1],HIGH);

digitalWrite(p[2],LOW);

else if(incoming==2)

Serial.println("Enter the speed from 0 to 255");

t=insert();

zero(t);
digitalWrite(p[1],LOW);

digitalWrite(p[2],HIGH);

2) ultrasonic

int ping = 7;

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

Serial.begin(9600);

// the loop routine runs over and over again forever:

void loop() {

long duration,cm,inches;

pinMode(ping,OUTPUT);

digitalWrite(ping,LOW); // turn the LED on (HIGH is the voltage level)

delayMicroseconds(2);

digitalWrite(ping,HIGH); // turn the LED on (HIGH is the voltage level)

delayMicroseconds(10); // wait for a second

digitalWrite(ping, LOW); // turn the LED off by making the voltage LOW

pinMode(ping,INPUT);

duration=pulseIn(ping,HIGH);
cm=(duration/2)/29;

inches=(duration/2)/74;

Serial.print(cm);

Serial.print(" cm, ");

Serial.print(inches);

Serial.print(" inches");

Serial.println();

delay(1000);

3) ir sensor

// Pin 13 has an LED connected on most Arduino boards.

// give it a name:

int LED = 13;

int isObstaclePin=7;

int isObstacle=HIGH;

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

pinMode(LED, OUTPUT);

pinMode(isObstaclePin, INPUT);

Serial.begin(9600);

}
// the loop routine runs over and over again forever:

void loop() {

isObstacle=digitalRead(isObstaclePin); // turn the LED on (HIGH is the voltage


level)

if(isObstacle==LOW) {

Serial.print("OBATACLE! OBATACLE! OBATACLE!");

digitalWrite(LED,LOW);// wait for a second

else

Serial.print("clear");

digitalWrite(LED,HIGH);

delay(2000);

You might also like