Week 5 – sensors

Goal of the project : Turn on the light according to distance and ambient light
Core components : Led light, photocell, 220 Ohm resistor, 1k resistor, ultrasonic sensor, arduino
How it works : the ultrasonic sensor measures the distance and the photocell measures the ambient light. An if statement combines the two measurements to control the led light
Problems encountered : the led light blink when is on. I couldn’t find a way to connect the photocell data without using a map function

const int trigPin = 12; //naming a constant value / giving a variable name / #define acts as a find and replace
const int echoPin = 13;
const int led = 3;

int sensorValue;
const int analogInPin = A0;
int LEDbrightness;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT); //trigPin is sending out the signal
pinMode(echoPin, INPUT); //echoPin is retrieving information, therefore it is INPUT

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; //Time it took for the echo to be received and divided in half then divided by the speed of sound

Serial.println(” cm”);
delay(500); //delay half a second before repeating the loop

//digitalWrite(led, LOW);

sensorValue = analogRead(analogInPin);

LEDbrightness = map(sensorValue, 0, 100, 0, 255);

if (distance <= 6 ){
digitalWrite(led, LEDbrightness);
else {
digitalWrite(led, LOW);

Leave a Reply

Your email address will not be published. Required fields are marked *