Week 9: Analog and Digital Sensor

Concept:

To construct this project, I had to learn how to use the ultrasonic sensor online, which measures the distance between the sensor and an item in front of it by sending signals and calculating the time it takes for it to return. I set up the ultrasonic sensor but I wanted to control the brightness of an LED lamp by the distance measured by it. Hence, I made the scale of the brightness of the lamb range between 0 cm away from the ultrasonic sensor and 100 cm (1m). 1m(and beyond) being the brightest and 0 being the dullest. To achieve this, I had to multiply the distance measured by the ultrasonic sensor by 2.55; if the distance is more than 100 cms, it tunes it down to 255.

Afterward, I wanted to activate the ultrasonic sensor only when a button is pressed while acknowledging the user that the ultrasonic sensor. Therefore I made two LEDs dependent upon the button that blinks for around 2 seconds, while the ultrasonic sensor takes the input and alters the brightness of the LED bulb that is dependent upon the distance.

Code:

At the beginning of the code, the global variables are initialized with the duration it takes the ultrasonic takes back the variable distance to store it in. with the pins the echo and the trig are connected to.

The LEDS are connected to dynamic or analog pins according to their usage. as well as one pin to check if the button is pressed.

int buttonPin = A0;
int redLed = 2;
int blueLed = 4;

int greenLed = 6;

int echo = 11;
int trig = 10;

long duration;
long distance;

In setup, the pins for the leds and ultrasonic are set up as well as the Serial to know the distance exactly and the brightness of the main LED.

void setup() {
  // setup pin modes
  Serial.begin(9600);
  pinMode(redLed, OUTPUT);
  pinMode(blueLed, OUTPUT);

  pinMode(trig, OUTPUT); 
  pinMode(echo, INPUT);
}

In the loop function, the program continuously checks if the button is pressed, and if it is pressed it calls the function blink.

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);

  int buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH){
    blink(2);
  }
}

The blink function makes the red and blue LEDs blink while calling a function to make the ultrasonic sensor start getting data and alter the brightness of the green LED.

void blink(int period){
  period = period * 5;
  for(int i = 0; i < period; i++){
    digitalWrite(redLed, LOW);
    digitalWrite(blueLed, HIGH);
    delay(100);
    digitalWrite(redLed, HIGH);
    digitalWrite(blueLed, LOW);
    delay(100);
    updateSensor();
  }
  digitalWrite(redLed, LOW);
  digitalWrite(blueLed, LOW);

}

The update sensor function gets data for the ultrasonic sensor by getting the time difference it takes for the ultrasonic waves to go out and come back, and by this data calculates the distance. then transforms the distance to a number that can be converted to an analog brightness.

void updateSensor(){
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034 / 2; 

  int brightness = ((int)(distance * 2.55) );
  brightness = (brightness > 255)? 255 : brightness;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, ");
  Serial.print("brightness: ");
  Serial.println(brightness);


  analogWrite(greenLed,brightness);
}

 

Reflection:

In this project, to be honest I achieved everything I was looking for. However, I was thinking about alerting the user while the ultrasonic sensor is running by a speaker and making the program start in another more interactive way than a button like a sound command recognition. I am not sure how can that be possible since it’s complex and needs AI, but if it’s manageable I am looking forward to working on a project like that.

In my project, the distance is changing is the brightness of the LED lamp. I was thinking about making it change to something more interactive like the speed of a motor.

 

 

 

Leave a Reply