## Friday, 11 September 2015

### Control different things at different distance with Arduino

In this post I'm going share a sketch with you which (sketch) can turn on and off as many things you want at different distance as you wish. This means you can turn something at a specific distance. For example you want to turn on a motor after 10cm and turn it off at 50cm, you can do this easily with the sketch below.

Note: I've used three leds here if you replace them with relay, motor and other stuff it will still work fine.

Things to change:

Change the pins as you wish.
int trigPin = 2;
int echoPin = 3;
int led1 = 4;
---
Make sure you write OUTPUT to every led.
pinMode(led1, OUTPUT);
---
if (distance >=1 && distance <=10){
Remember >= means from and <=  means under.
And the number you seeing after >= and <= is centimetre/s.
---

Here is the sketch.
 //define the pins for ultrasonic sensor and leds here int trigPin = 2; int echoPin = 3; int led1 = 4; int led2 = 5; int led3 = 6; void setup() {   Serial.begin (9600);   pinMode(trigPin, OUTPUT);   pinMode(echoPin, INPUT);   pinMode(led1, OUTPUT);   pinMode(led2, OUTPUT);   pinMode(led3, OUTPUT); } void loop() {    int duration, distance;   digitalWrite(trigPin, HIGH);   delayMicroseconds(1000);   digitalWrite(trigPin, LOW);   duration = pulseIn(echoPin, HIGH);   distance = (duration/2) / 29.1; if (distance >=1 && distance <=10){    Serial.print(distance);     Serial.println(" cm");     digitalWrite(led1, HIGH);     digitalWrite(led2, LOW);     digitalWrite(led3, LOW);   }   else if (distance >=11 && distance <=20){    Serial.print(distance);     Serial.println(" cm");     digitalWrite(led2, HIGH);     digitalWrite(led1, LOW);     digitalWrite(led3, LOW);   }   else if (distance >=21 && distance <=30){    Serial.print(distance);     Serial.println(" cm");     digitalWrite(led3, HIGH);     digitalWrite(led1, LOW);     digitalWrite(led2, LOW);   }   else {     Serial.print(distance);     Serial.println(" cm");     digitalWrite(led1, LOW);     digitalWrite(led2, LOW);     digitalWrite(led3, LOW);     delay(30);   } }

Watch the video to see the sketch in action.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6310077,"math_prob":0.9833082,"size":1948,"snap":"2019-51-2020-05","text_gpt3_token_len":536,"char_repetition_ratio":0.20164609,"word_repetition_ratio":0.08362369,"special_character_ratio":0.30749488,"punctuation_ratio":0.22997417,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9777329,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-17T16:41:35Z\",\"WARC-Record-ID\":\"<urn:uuid:35be4676-a227-4cca-bc60-6117ee6491ef>\",\"Content-Length\":\"60448\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:20091842-a4bd-4385-9aaf-4b1ab1be01a6>\",\"WARC-Concurrent-To\":\"<urn:uuid:e6ac7dbf-0519-45f6-bf4a-f8ea186fa252>\",\"WARC-IP-Address\":\"172.217.164.147\",\"WARC-Target-URI\":\"https://www.amazingtips247.co.uk/2015/09/control-different-things-at-different.html\",\"WARC-Payload-Digest\":\"sha1:TJ6KBKUU6YCJFOAV64YJL6FUPYXMOSEY\",\"WARC-Block-Digest\":\"sha1:INKT3IH33G5UESA7ITQGUPPER5OKVVNE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250589861.0_warc_CC-MAIN-20200117152059-20200117180059-00013.warc.gz\"}"}