Vous êtes sur la page 1sur 5

//

//Jessica Erwood - Coding Assignment 1

//22/05/2017

//

//

//

//PURPOSE:

//

//Measures distance using an ultrasonic sensor - Jaycar Part


No:XC4442

//Processor unit is an Arduino UNO - Jaycar Part No:XC4410

//Displays approximate closeness using colours on an RGB LED module - Jaycar


Part No:XC4428

//

//Blue- out of range (3m typically)

//Green- in range, within 50cm-30cm

//Orange- within 10cm

//Red- within 5cm

//DEFINES Region:

//Defines or setups up various constant values and Arduino pin numbers used by
the sensor and the display

//Setup the distances which are displayed by each colour

//these values are in centimeters

#define GREENDISTANCE 30

#define AMBERDISTANCE 10

#define REDDISTANCE 5
//define Ultrasonic sensor module pins here - this is the pins that the sensor is
connected to on the Arduino board.

#define UVCC 10

#define UTRIG 11

#define UECHO 12

#define UGND 13

//define LED module pins here - this is the pins that the sensor is connected to on
the Arduino board.

//LED module is a RGB type module so that various colours can be displayed by
the one module.

#define LEDPLUS 3

#define LEDBLUE 0

#define LEDRED 1

#define LEDGREEN 2

//End of DEFINES Region:

//SETUP Region:

//The setup() method is ran once just after the Arduino is powered up.

//The setup() section is where you do any initialisation steps

void setup()

usonicsetup();

ledsetup();

//End of SETUP Region:


//Main Program Loop Region:

//The loop() method runs continuously after the Arduino has started and has
been initialiseds.

//The loop() section is were you place any code you want to run over and over
again.

void loop()

byte r=0; //for red led

byte g=0; //for green led

byte b=0; //for blue led

long d; //for distance

d=usonic(17400)/58; //convert ping time to distance in cm

if(d==0){d=300;} //sometimes returns 0 when not in range

if(d<AMBERDISTANCE){r=1;} //turn on red LED if red or amber needs to be


shown

if((d>=REDDISTANCE)&&(d<300)){g=1;} //turn on green LED if amber or


green needs to be shown

if(d>=300){b=1;} //turn on blue otherwise, so you know it's working

ledset(r,g,b); //set the LED's

delay(200); //wait a bit so it won't flicker too much

//End of Main Program Loop Region:

//Set up LED pins as output pins

void ledsetup()

pinMode(LEDPLUS, OUTPUT);
digitalWrite(LEDPLUS, HIGH);

pinMode(LEDRED, OUTPUT);

digitalWrite(LEDRED, HIGH);

pinMode(LEDGREEN, OUTPUT);

digitalWrite(LEDGREEN, HIGH);

pinMode(LEDBLUE, OUTPUT);

digitalWrite(LEDBLUE, HIGH);

//Setup LED Module

void ledset(byte r, byte g, byte b)

r=!r; //invert if we're using common +

g=!g;

b=!b;

digitalWrite(LEDRED, r); //set LED Module outputs

digitalWrite(LEDGREEN, g);

digitalWrite(LEDBLUE, b);

//Setup Ultrasonic sensor

void usonicsetup(void)

pinMode(UGND, OUTPUT);

digitalWrite(UGND, LOW);

pinMode(UVCC, OUTPUT);

digitalWrite(UVCC, HIGH);
pinMode(UECHO, INPUT); //ECHO pin is input

pinMode(UTRIG, OUTPUT); //TRIG pin is output

digitalWrite(UTRIG, LOW);

long usonic(long utimeout) //utimeout is maximum time to wait for


return in us

long b;

if(digitalRead(UECHO)==HIGH){return 0;} //if UECHO line is still low from last


result, return 0;

digitalWrite(UTRIG, HIGH); //send trigger pulse

delay(1);

digitalWrite(UTRIG, LOW);

long utimer=micros();

while((digitalRead(UECHO)==LOW)&&((micros()-utimer)<1000)){} //wait for


pin state to change- return starts after 460us typically

utimer=micros();

while((digitalRead(UECHO)==HIGH)&&((micros()-utimer)<utimeout)){} //wait
for pin state to change

b=micros()-utimer;

return b;

Vous aimerez peut-être aussi