arduino projects & tutorials

42
1 Arduino Tutorials For more visit www.anshupandey.com 1. LED Blinking Code for ARDUINO 2. Range Finder using Arduino and HCSR04 Ultrasonic Sensor 3. Burglar Alarm using PIR Sensor 4. Temperature & Humidity Data Logging using DHT11 and Arduino 5. DTMF Controlled Robot using Arduino 6. Android Controlled Robot using Bluetooth Module HC05 & Arduino 7. Digital Keypad Interfacing with Arduino 8. GSM Module Interfacing with Arduino 9. RFID Interfacing with Arduino 10. Accelerometer Controlled Robot

Upload: anshu-pandey

Post on 21-Jan-2018

247 views

Category:

Technology


11 download

TRANSCRIPT

Page 1: Arduino projects & tutorials

1 Arduino Tutorials

For more visit www.anshupandey.com

1. LED Blinking Code for ARDUINO

2. Range Finder using Arduino and HCSR04 Ultrasonic Sensor

3. Burglar Alarm using PIR Sensor

4. Temperature & Humidity Data Logging using DHT11 and Arduino

5. DTMF Controlled Robot using Arduino

6. Android Controlled Robot using Bluetooth Module HC05 & Arduino

7. Digital Keypad Interfacing with Arduino

8. GSM Module Interfacing with Arduino

9. RFID Interfacing with Arduino

10. Accelerometer Controlled Robot

Page 2: Arduino projects & tutorials

2 Arduino Tutorials

For more visit www.anshupandey.com

LED Blinking Program for Arduino

Hardware Required

Arduino or Genuino Board

LED

220 ohm resistor

Circuit

To build the circuit, connect one end of the resistor to Arduino pin 13. Connect the long

leg of the LED (the positive leg, called the anode) to the other end of the resistor.

Connect the short leg of the LED (the negative leg, called the cathode) to the Arduino

GND, as shown in the diagram and the schematic below.

Most Arduino boards already have an LED attached to pin 13 on the board itself. If you

run this example with no hardware attached, you should see that LED blink.

The value of the resistor in series with the LED may be of a different value than 220

ohm; the LED will lit up also with values up to 1K ohm.

Page 3: Arduino projects & tutorials

3 Arduino Tutorials

For more visit www.anshupandey.com

Program:

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

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

delay(1000); // wait for a second

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

delay(1000); // wait for a second

}

Page 4: Arduino projects & tutorials

4 Arduino Tutorials

For more visit www.anshupandey.com

Range Finder using Arduino and HCSR04 Ultrasonic

Sensor

In this tutorial we present a method of measuring the distance between an Arduino and

nearby objects. It is of particular use for automated robots for giving them ‘eyes’ to look

out for nearby objects, measure the distances between them, and acting upon that

distance. We present the HC-SR04 range finder sensor

The cheap sensors normally have 2 cylindrical objects resembling a speaker with pins

in the middle. Well, one of them is a speaker, the other is a microphone.

The sensor has 4 pins +5v, GND, Trig and Echo. Ignoring the obvious (+5v and the

GND), we have the remaining 2 which are used to calculate the distance. The

microcontroller pulls the Trig pin to High for 10 microseconds, then pulled down again.

At this point the sensor sends an ultrasonic signal (around 40kHz I believe) from the

speaker and waits for an echo. If an echo is picked by the sensor, the Echo pin is pulled

down (being previously pulled up by the microcontroller). The time it takes from

triggering the signal to the echo is the time taken for sound to travel from the sensor to

the object and back.

Knowing the speed of sound which is roughly 340.29m/s we can calculate the distance

traveled by sound in that time, then dividing it by 2 to get the distance from the object.

Page 5: Arduino projects & tutorials

5 Arduino Tutorials

For more visit www.anshupandey.com

Connections:

Arduino => UltraSonic Sensor

+5V => +5V

GND => GND

Echo => D11 (or any other pin)

Trigger => D12 (or any other pin)

Page 6: Arduino projects & tutorials

6 Arduino Tutorials

For more visit www.anshupandey.com

Program:

int trig=5; //trigger pin connected to pin 5

int echo=6; //echo pin connnected to pin 6

void setup() {

// put your setup code here, to run once:

pinMode(trig,OUTPUT); //defining trig as output

pinMode(echo,INPUT); //defining echo as input

Serial.begin(9600);}

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(trig,LOW); //generating transmitting pulse

delayMicroseconds(2);

digitalWrite(trig,HIGH);

delayMicroseconds(10);

digitalWrite(trig,LOW);

double duration=pulseIn(eccho,HIGH); //measuring time to get high pulse on echo

duration=duration/2; //dividing duration by 2

float distance=0.034*duration; //calculating distance

Serial.println(distance);

}

Page 7: Arduino projects & tutorials

7 Arduino Tutorials

For more visit www.anshupandey.com

Burglar Alarm using PIR Sensor

The circuit diagram to build a simple burglar alarm or an intruder alarm using arduino is

given below.

PIR Sensor – is the heart of this simple burglar alarm circuit using arduino. A PIR

sensor – is basically a motion sensor or a motion detector which identifies any object

that moves inside its range of view. PIR sensor identifies infra red radiations emitted by

any object under its radar range.

Buzzer – is used to create a sound alarm when ever a movement is identified inside the

range of PIR sensor. A transistor 2N2222 is used to drive the buzzer. The maximum

current that can be sourced or sinked from an arduino pin is 20mA (the total current

being 200mA from different pins). But the buzzer will need more than just 20mA for its

proper functioning. So how to give the necessary current required fir buzzer ? We use

switching transistor 2N222 for this purpose. It can act as a switch and at the same time

it provides the required current amplification. A 2N2222 transistor with a gain of 100 can

give upto 1A current at its output. Another purpose of using a transistor in between

arduino pin and buzzer is isolation. A short circuit of the buzzer will destroy only the

collector – emitter junction of transistor. Since their is isolation at the base region of

transistor (base is connected to arduino), the destruction of collector-emitter junction will

not affect base and hence our arduino will be safe from getting burned! The 100 ohms

resistor at base is used to limit base current of transistor.

Switch – a push button switch is used to reset the burglar alarm once its activated. The

capacitor is used for bypassing bouncing effects of a switch ( debouncing capacitor).

Page 8: Arduino projects & tutorials

8 Arduino Tutorials

For more visit www.anshupandey.com

Connections Explained

Arduino – Pin 7 – Output of PIR Sensor | Pin 6 – Push button switch | Pin 8 – Buzzer

Buzzer – + pin to Vcc (5 volts) | other pin to collector side of 2N2222

Transistor – 2N2222 – NPN – Collector to Buzzer | Emitter to Ground | Base to Arduino

through 100 Ohm Resistor

Switch – One end of switch to +5V | Other end to Ground through a 10K current limiting

resistor

PIR Sensor – has got 3 pins – Vcc to +5 volts | GND to Ground | OUT pin to Arduino pin

7

Program:

int sensor=7; //The output of PIR sensor connected to pin 7

int push_switch=6; // push button switch connected to pin 6

int buzzer=8; // buzzer connected at pin 8

int sensor_value; //variable to hold read sensor value

void setup()

{

pinMode(sensor,INPUT); // configuring pin 7 as Input

pinMode(push_switch,INPUT); // configuring pin 6 as Input

pinMode(buzzer,OUTPUT); // configuring pin 8 as OUTPUT

}

void loop()

{

Page 9: Arduino projects & tutorials

9 Arduino Tutorials

For more visit www.anshupandey.com

sensor_value=digitalRead(sensor); // Reading sensor value from pin 7

if(sensor_value==HIGH) // Checking if PIR sensor sends a HIGH signal to Arduino

{

digitalWrite(buzzer,HIGH); // Activating the buzzer

}

if(digitalRead(push_switch==HIGH))// Checking if pushbutton was pressed

{

digitalWrite(buzzer,LOW); // turning OFF the buzzer

}}

Page 10: Arduino projects & tutorials

10 Arduino Tutorials

For more visit www.anshupandey.com

Temperature & Humidity Data Logging using DHT11

and Arduino

This project is about a simple USB temperature logging system using arduino uno and

the serial monitor function in the arduino IDE. The system monitors the temperature &

humidity every 2 seconds and shows it on the arduino serial monitor. The temperature

is shown in °Celsius. The system is interfaced to the PC through the USB port. DHT11

is used as the temperature & Humidity sensor.

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a

capacitive humidity sensor and a thermistor to measure the surrounding air, and spits

out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use,

but requires careful timing to grab data. The only real downside of this sensor is you can

only get new data from it once every 2 seconds, so when using our library, sensor

readings can be up to 2 seconds old.

Page 11: Arduino projects & tutorials

11 Arduino Tutorials

For more visit www.anshupandey.com

For using DHT11, you will have to import and add the DHT11 library into

Arduino.

Program:

#include <dht.h>

dht DHT;

int dhtpin=A2;

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);}

void loop() {

// put your main code here, to run repeatedly:

DHT.read11(dhtpin);

Serial.print("Temperature: ");

Serial.print(DHT.temperature);

Serial.print(" Humidity: ");

Serial.print(DHT.humidity);

Serial.println(" ");

delay(1000);

}

Page 12: Arduino projects & tutorials

12 Arduino Tutorials

For more visit www.anshupandey.com

DTMF Controlled Robot using Arduino

DTMF controlled Robot runs over mobile DTMF technology that exists in Dial tone.

DTMF stands for Dual Tone Multiple Frequency. There are some frequencies that we

use to create DTMF tone. In simple words by adding or mixing two or more frequencies

generates DTMF tone.

Page 13: Arduino projects & tutorials

13 Arduino Tutorials

For more visit www.anshupandey.com

Required Components

Arduino UNO

DC Motor

Mobile Phone

DTMF decoder Module

Motor Driver L293D

9 Volt Battery

Battery Connector

Aux wire

Robot Chasis with wheel

Connecting wires

Page 14: Arduino projects & tutorials

14 Arduino Tutorials

For more visit www.anshupandey.com

Program:

#define m11 3

#define m12 4

#define m21 5

#define m22 6

#define D0 19

#define D1 18

#define D2 17

#define D3 16

void forward()

{

digitalWrite(m11, HIGH);

digitalWrite(m12, LOW);

digitalWrite(m21, HIGH);

digitalWrite(m22, LOW);

}

void backward()

{

digitalWrite(m11, LOW);

digitalWrite(m12, HIGH);

digitalWrite(m21, LOW);

digitalWrite(m22, HIGH);

}

void left()

{

digitalWrite(m11, HIGH);

digitalWrite(m12, LOW);

Page 15: Arduino projects & tutorials

15 Arduino Tutorials

For more visit www.anshupandey.com

digitalWrite(m21, LOW);

digitalWrite(m22, LOW);

}

void right()

{

digitalWrite(m11, LOW);

digitalWrite(m12, LOW);

digitalWrite(m21, HIGH);

digitalWrite(m22, LOW);

}

void Stop()

{

digitalWrite(m11, LOW);

digitalWrite(m12, LOW);

digitalWrite(m21, LOW);

digitalWrite(m22, LOW);

}

void setup()

{

pinMode(D0, INPUT);

pinMode(D1, INPUT);

pinMode(D2, INPUT);

pinMode(D3, INPUT);

pinMode(m11, OUTPUT);

pinMode(m12, OUTPUT);

pinMode(m21, OUTPUT);

Page 16: Arduino projects & tutorials

16 Arduino Tutorials

For more visit www.anshupandey.com

pinMode(m22, OUTPUT);

}

void loop()

{

int temp1=digitalRead(D0);

int temp2=digitalRead(D1);

int temp3=digitalRead(D2);

int temp4=digitalRead(D3);

if(temp1==0 && temp2==1 && temp3==0 && temp4==0)

forward();

else if(temp1==0 && temp2==0 && temp3==1 && temp4==0)

left();

else if(temp1==0 && temp2==1 && temp3==1 && temp4==0)

right();

else if(temp1==0 && temp2==0 && temp3==0 && temp4==1)

backward();

else if(temp1==1 && temp2==0 && temp3==1 && temp4==0)

Stop();

}

Page 17: Arduino projects & tutorials

17 Arduino Tutorials

For more visit www.anshupandey.com

Android Controlled Robot using Bluetooth Module HC05 &

Arduino

So here android phone is used as transmitting device and Bluetooth module placed in

car is used as receiver. Android phone will transmit command using its in-built Bluetooth

to car so that it can move in the required direction like moving forward, reverse, turning

left, turning right and stop.

Bluetooth Module

HC Bluetooth module consists two things one is Bluetooth serial interface module and a

Bluetooth adaptor. Bluetooth serial module is used for converting serial port to

Bluetooth.

Bluetooth Connections:

Rx → Serial receiving pin

Tx → Serial transmitting pin

GND → ground

Vcc → +5volt dc

Page 18: Arduino projects & tutorials

18 Arduino Tutorials

For more visit www.anshupandey.com

Components Used:

Arduino Uno

Chassis

HC05

A to B USB Cable

Battery

Motors -2

Wheels -2

L293D Motor Driver Board

Caster Wheel

Android Phone with any bluetooth App (I prefer Arduino Centrale)

Page 19: Arduino projects & tutorials

19 Arduino Tutorials

For more visit www.anshupandey.com

Program:

int m11=4;

int m12=5;

int m21=6;

int m22=7;

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

pinMode(m11,OUTPUT);

pinMode(m12,OUTPUT);

pinMode(m21,OUTPUT);

pinMode(m22,OUTPUT);

}

void loop() {

// put your main code here, to run repeatedly:

char x=Serial.read();

if (x=='f')

{digitalWrite(m11,HIGH);

digitalWrite(m12,LOW);

digitalWrite(m21,HIGH);

Page 20: Arduino projects & tutorials

20 Arduino Tutorials

For more visit www.anshupandey.com

digitalWrite(m22,LOW);

delay(2000);

}

else if (x=='b')

{digitalWrite(m11,LOW);

digitalWrite(m12,HIGH);

digitalWrite(m21,LOW);

digitalWrite(m22,HIGH);

delay(2000);}

else if (x=='r')

{digitalWrite(m11,LOW);

digitalWrite(m12,HIGH);

digitalWrite(m21,HIGH);

digitalWrite(m22,LOW);

delay(2000);}

else if (x=='l')

{digitalWrite(m11,HIGH);

digitalWrite(m12,LOW);

digitalWrite(m21,LOW);

digitalWrite(m22,HIGH);

delay(2000);}

Page 21: Arduino projects & tutorials

21 Arduino Tutorials

For more visit www.anshupandey.com

else if (x=='s')

{digitalWrite(m11,LOW);

digitalWrite(m12,LOW);

digitalWrite(m21,LOW);

digitalWrite(m22,LOW);

delay(2000);}

}

Page 22: Arduino projects & tutorials

22 Arduino Tutorials

For more visit www.anshupandey.com

Digital Keypad Interfacing with Arduino

Connections:

Keypad Pin R1 –> Arduino Pin 2

Keypad Pin R2 –> Arduino Pin 3

Keypad Pin R3 –> Arduino Pin 4

Keypad Pin R4 –> Arduino Pin 5

Keypad Pin C1 –> Arduino Pin 6

Keypad Pin C2 –> Arduino Pin 7

Keypad Pin C3 –> Arduino Pin 8

Keypad Pin C4 –> Arduino Pin 9

Page 23: Arduino projects & tutorials

23 Arduino Tutorials

For more visit www.anshupandey.com

Program:

/*this keypad tutorial number one by omar tarek 29-06-2014 */

byte h=0,v=0; //variables used in for loops

const unsigned long period=50; //little period used to prevent error

unsigned long kdelay=0; // variable used in non-blocking delay

const byte rows=4; //number of rows of keypad

const byte columns=4; //number of columnss of keypad

const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad

const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of

keypad

byte keypad() // function used to detect which button is used

{

static bool no_press_flag=0; //static flag used to ensure no button is pressed

for(byte x=0;x<columns;x++) // for loop used to read all inputs of keypad to ensure no

button is pressed

{

if (digitalRead(Input[x])==HIGH); //read evry input if high continue else break;

else

break;

if(x==(columns-1)) //if no button is pressed

{

Page 24: Arduino projects & tutorials

24 Arduino Tutorials

For more visit www.anshupandey.com

no_press_flag=1;

h=0;

v=0;

}

}

if(no_press_flag==1) //if no button is pressed

{

for(byte r=0;r<rows;r++) //for loop used to make all output as low

digitalWrite(Output[r],LOW);

for(h=0;h<columns;h++) // for loop to check if one of inputs is low

{

if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it)

continue

continue;

else //if one of inputs is low

{

for (v=0;v<rows;v++) //for loop used to specify the number of row

{

digitalWrite(Output[v],HIGH); //make specified output as HIGH

if(digitalRead(Input[h])==HIGH) //if the input that selected from first sor loop is

change to high

{

Page 25: Arduino projects & tutorials

25 Arduino Tutorials

For more visit www.anshupandey.com

no_press_flag=0; //reset the no press flag;

for(byte w=0;w<rows;w++) // make all outputs as low

digitalWrite(Output[w],LOW);

return v*4+h; //return number of button

}

}

}

}

}

return 50;

}

void setup()

{

for(byte i=0;i<rows;i++) //for loop used to make pin mode of outputs as output

{

pinMode(Output[i],OUTPUT);

}

for(byte s=0;s<columns;s++) //for loop used to makk pin mode of inputs as inputpullup

{

pinMode(Input[s],INPUT_PULLUP);

}

Page 26: Arduino projects & tutorials

26 Arduino Tutorials

For more visit www.anshupandey.com

Serial.begin(9600); //to use serial monitor we set the buad rate

}

void loop()

{

if(millis()-kdelay>period) //used to make non-blocking delay

{

kdelay=millis(); //capture time from millis function

switch (keypad()) //switch used to specify which button

{

case 0:

Serial.println(1);

break;

case 1:

Serial.println(2);

break;

case 2:

Serial.println(3);

break;

case 3:

Serial.println("F1");

break;

Page 27: Arduino projects & tutorials

27 Arduino Tutorials

For more visit www.anshupandey.com

case 4:

Serial.println(4);

break;

case 5:

Serial.println(5);

break;

case 6:

Serial.println(6);

break;

case 7:

Serial.println("F2");

break;

case 8:

Serial.println(7);

break;

case 9:

Serial.println(8);

break;

case 10:

Serial.println(9);

break;

Page 28: Arduino projects & tutorials

28 Arduino Tutorials

For more visit www.anshupandey.com

case 11:

Serial.println("F3");

break;

case 12:

Serial.println("Mode");

break;

case 13:

Serial.println(0);

break;

case 14:

Serial.println("Cancel");

break;

case 15:

Serial.println("Enter");

break;

default:

;

}

}

}

Page 29: Arduino projects & tutorials

29 Arduino Tutorials

For more visit www.anshupandey.com

GSM Module Interfacing with Arduino

A GSM Module is basically a GSM Modem (like SIM 900) connected to a PCB with

different types of output taken from the board – say TTL Output (for Arduino, 8051 and

other microcontrollers) and RS232 Output to interface directly with a PC (personal

computer). The board will also have pins or provisions to attach mic and speaker, to

take out +5V or other values of power and ground connections. These type of provisions

vary with different modules.

Lots of varieties of GSM modem and GSM Modules are available in the market to

choose from. For our project of connecting a gsm modem or module to arduino and

hence send and receive sms using arduino – its always good to choose an arduino

compatible GSM Module – that is a GSM module with TTL Output provisions.

Page 30: Arduino projects & tutorials

30 Arduino Tutorials

For more visit www.anshupandey.com

Program:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

void setup()

{

mySerial.begin(9600); // Setting the baud rate of GSM Module

Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)

delay(100);

}

void loop()

{

if (Serial.available()>0)

switch(Serial.read())

{

case 's':

SendMessage();

break;

case 'r':

RecieveMessage();

break;

}

Page 31: Arduino projects & tutorials

31 Arduino Tutorials

For more visit www.anshupandey.com

if (mySerial.available()>0)

Serial.write(mySerial.read());

}

void SendMessage()

{

mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode

delay(1000); // Delay of 1000 milli seconds or 1 second

mySerial.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); // Replace x with mobile number

delay(1000);

mySerial.println("I am SMS from GSM Module");// The SMS text you want to send

delay(100);

mySerial.println((char)26);// ASCII code of CTRL+Z

delay(1000);

}

void RecieveMessage()

{

mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS

delay(1000);

}

Page 32: Arduino projects & tutorials

32 Arduino Tutorials

For more visit www.anshupandey.com

RFID Module Interfacing with Arduino

Connections:

On RFID sensor: PIN 1 -> Tx

PIN 2 -> Rx (Not Used)

PIN 3 -> NC

PIN 4 -> GND

PIN 5 -> VCC (+5V)

Program:

#include <SoftwareSerial.h>

#define ADD_TAG_CODE "210014DFE309" //change this ID with your own card TAG

#define DEL_TAG_CODE "210014E2BD6A" //change this ID with your own card TAG

SoftwareSerial rfid = SoftwareSerial(5, 6);

String msg;

String ID ; //string to store allowed cards

void setup()

{

Serial.begin(9600);

Serial.println("Serial Ready");

Page 33: Arduino projects & tutorials

33 Arduino Tutorials

For more visit www.anshupandey.com

rfid.begin(9600);

Serial.println("RFID Ready");

}

char c;

void loop(){

while(rfid.available()>0){

c=rfid.read();

msg += c;

Serial.println(msg);

Serial.println(msg.length());

}

msg=msg.substring(1,13);

if(msg.indexOf(ADD_TAG_CODE)>=0) add();

else if(msg.indexOf(DEL_TAG_CODE)>=0) del();

else if(msg.length()>10) verifica();

msg="";

}

Page 34: Arduino projects & tutorials

34 Arduino Tutorials

For more visit www.anshupandey.com

void add(){

Serial.print("What TAG do you wanna grant access?: ");

msg="";

while(msg.length()<13){

while(rfid.available()>0){

c=rfid.read();

msg += c;

}

}

if(ID.indexOf(msg)>=0) {

Serial.println("\nAccess already granted for this card.");

msg="";

}

else{

Serial.print("Card: ");

Serial.println(msg);

ID += msg;

ID += ",";

//Serial.print("ID: ");

// Serial.println(ID);

Page 35: Arduino projects & tutorials

35 Arduino Tutorials

For more visit www.anshupandey.com

msg="";

Serial.println("Access granted for this card.");

}

}

void del(){

msg="";

Serial.print("What TAG do you wanna deny access?: ");

while(msg.length()<13){

while(rfid.available()>0){

c=rfid.read();

msg += c;

}

}

msg=msg.substring(1,13);

if(ID.indexOf(msg)>=0){

Serial.println(msg);

Serial.println("TAG found. Access for this card denied.");

//ID.replace(card,"");

int pos=ID.indexOf(msg);

Page 36: Arduino projects & tutorials

36 Arduino Tutorials

For more visit www.anshupandey.com

msg="";

msg += ID.substring(0,pos);

msg += ID.substring(pos+15,ID.length());

ID="";

ID += msg;

//Serial.print("ID: ");

//Serial.println(ID);

} else Serial.println("\nTAG not found or already denied");

msg="";

}

Page 37: Arduino projects & tutorials

37 Arduino Tutorials

For more visit www.anshupandey.com

Accelerometer Controlled Robot

Components Required:

For transmitter-

Arduino Uno

ADXL335 accelerometer

433 MHz RF transmitter

Breadboard

For receiver and robot-

Arduino Uno

433 MHz RF receiver

L293D motor driver IC

Chassis and wheels

2 DC motors

Breadboard

Program:

Transmitter Code:

int xPin=0;//Connect x pin of adxl335 to pin A0

int yPin=1;//Connect y pin of adxl335 to pin A1

void setup()

{

Page 38: Arduino projects & tutorials

38 Arduino Tutorials

For more visit www.anshupandey.com

Serial.begin(9600);//Initialise the serial connection

}

void loop()

{

int xval=analogRead(xPin);

int yval=analogRead(yPin);

Serial.print("xval=");

Serial.println(xval);//Use xval to determine threshold for different directions

Serial.print("yval=");

Serial.println(yval); //Use yval to determine threshold for different directions

delay(2000); //used to display values after 2s delay

Serial.print("\n");//print after 2s in a new line

}

Receiver Code:

//Connect the Receiver data pin to Arduino pin 11

#include <VirtualWire.h>

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages

byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message

Page 39: Arduino projects & tutorials

39 Arduino Tutorials

For more visit www.anshupandey.com

int lm=9;

int lmr=8;

int rm=10;

int rmr=7;

int ledPin=13;//led on pin 13 is ON except when bot is stationary

void setup()

{

//Serial.begin(9600);//Initialise the serial connection debugging

pinMode(ledPin,OUTPUT);

pinMode(lm,OUTPUT);

pinMode(lmr,OUTPUT);

pinMode(rm,OUTPUT);

pinMode(rmr,OUTPUT);

vw_setup(2000); // Bits per sec

vw_rx_start(); // Start the receiver

}

void loop()

Page 40: Arduino projects & tutorials

40 Arduino Tutorials

For more visit www.anshupandey.com

{

uint8_t buf[VW_MAX_MESSAGE_LEN];

uint8_t buflen = VW_MAX_MESSAGE_LEN;

if (vw_get_message(buf, &buflen)) // Non-blocking

{

int i;

//Serial.print("Got: ");//debugging

for (i = 0; i < buflen; i++)

{

// Serial.print(buf[i],HEX);//You may also use integer values debugging

//Serial.print(' ');// debugging

if (buf[i]==0x73)//Stationary

{

digitalWrite(lm,LOW);

digitalWrite(lmr,LOW);

digitalWrite(rm,LOW);

digitalWrite(rmr,LOW);

digitalWrite(ledPin,LOW);

Page 41: Arduino projects & tutorials

41 Arduino Tutorials

For more visit www.anshupandey.com

}

else

{

if(buf[i]==0x66)//Forward

{

digitalWrite(lm,LOW);

digitalWrite(lmr,HIGH);

digitalWrite(rm,HIGH);

digitalWrite(rmr,LOW);

digitalWrite(ledPin,HIGH);

}

if (buf[i]==0x61)//Backward

{

digitalWrite(lm,HIGH);

digitalWrite(lmr,LOW);

digitalWrite(rm,LOW);

digitalWrite(rmr,HIGH);

digitalWrite(ledPin,HIGH);

}

if (buf[i]==0x72)//Left

Page 42: Arduino projects & tutorials

42 Arduino Tutorials

For more visit www.anshupandey.com

{ digitalWrite(lm,LOW);

digitalWrite(lmr,LOW);

digitalWrite(rm,HIGH);

digitalWrite(rmr,LOW);

digitalWrite(ledPin,HIGH);

}

if (buf[i]==0x6C)//Right

{

digitalWrite(lm,LOW);

digitalWrite(lmr,HIGH);

digitalWrite(rm,LOW);

digitalWrite(rmr,LOW);

digitalWrite(ledPin,HIGH);

}

}

}

//Serial.print("\n");// debugging

}

//delay(1000);

}