Lab 4/Distance sensor and Led/ Wenyi(Jenny) Liu/Daniel

Name:Wenyi(Jenny) Liu

Instructor:Dan

Date:30th Sept

Project: using the data from the distance sensor to control the led

Materials: Led, Wires, Resistor, Breadboard, Arduino

Fist I borrowed the sensor and connected to my Arduino with the guidence online and then copied its sample code making the Arduino read its number. Then I pluged it in and found it working.

Then I moved on to the actuator part in which the distence controls the led whether is on or not.

I added a if conditionals into the loop. Since recently I’ve been learning stuff about Processing, it really took me some time to realise that its format is quite is slightly different and I had to declare its pin and digital or analog read and write. Then I made the cuicurt on the board. I first shortcircuited the led and corrected it right after Richard pointing it out and then it worked but not as I expected. I mean my code was to let the led on when I was near enough. However it went the opposite way.

void setup () {

Serial.begin (9600);

pinMode (A0, INPUT);

}

void loop () {

uint16_t value = analogRead (A0);

// uint16_t range = get_gp2d12 (value);

Serial.println (value);

//Serial.print (range);

Serial.println (” mm”);

Serial.println ();

delay (30);

int led = 9;//led is attached to 9

pinMode (led, OUTPUT);

if (value<=40) {

digitalWrite(led, HIGH);

} else {digitalWrite (led, LOW);

}}

uint16_t get_gp2d12 (uint16_t value) {

if (value < 10) value = 10;

return ((67870.0 / (value – 3.0)) – 40.0);

}

To solve this problem in the easiest way which was also the IMA way as Richard said, I just change value<=40 into>=40, and it went perfectly fine. Yeah, I got what I wanted.

Leave a Reply