Assignment 6: Analog Input & Output (Mood Lamp!)

Concept & Inspiration

As part of the 6th Assignment of Intro to IM, we were tasked with the objective of reading input from at least one analog sensor, and at least one digital sensor (switch). This data would then be used to control at least two LEDs, one in a digital fashion and the other in an analog fashion, in some creative way.

I have always had a great emphasis on the ‘lighting’ around me, and therefore am fond of purchasing different lamps and setting them up to have a good ambiance. This brings me to an interesting lamp I saw once at the convenience store, called Mood Lamp. If you are not aware of what those are, the following video should help you get an idea of it:

Therefore, being inspired from the concept of a ‘switching’ light and wanting to add some additional functionality to it, I decided to test my Arduino and Circuit Making skills to implement a model Mood Lamp. The concept was to use the button or switch as the digital sensor and the light sensor as the analog sensor (controlled by another button) which modifies the brightness of the light. The primary button would be used to modify the color modes of the LED, and a secondary button would toggle the brightness functionality of the light sensor.

Implementation

Pictures of Circuit

With this, I produced the following circuit:

Demo Video

A demo video, in detail, can be seen here:

The implementation of the Mood Lamp on the breadboard can be seen above. To make it clear, since the quality of Video did not allow the RGB LED Bulb to be captured to perfection, the Switch on the right, Yellow colored, is used to switch between the different modes of light. In our case, the modes are: Switched Off, Rainbow (Fading), Blue, Green, and Red. Alongside this, the blue button on the left, toggles the brightness adjustment option by making use of the light sensor. The meaning of this is, if the light in the room is brighter, the bulb will glow brighter, and if the light is less, it will grow lighter. Since it is a mood lamp, usually used at night, the purpose of it is to adapt with the light. At night, it should automatically adapt to the surrounding light to make its own light visible.

Code

Since the Code cannot be linked with Arduino, unlike P5js, I will be pasting the entire code here for anyone’s reference!

// Declaration of Constants
const int BUTTON_PIN_LIGHT = 7;  // Connecting the Light Button to Pin 7
const int BUTTON_PIN_LDR = 8;  // Connecting the Photoresistor Button to Pin 7
const int PIN_RED   = 9; //Red LED connected on pin 9
const int PIN_GREEN = 10; //Green LED connected on pin 10
const int PIN_BLUE  = 11; //Blue LED connected on Pin 11
const int LIGHT_SENSOR_PIN = A0; // Connecting the Photoresistor to Pin A0
   
//Color intensities and direction
//Initial Values for the Fading Rainbow Color Mode
int red             = 254;
int green           = 1;
int blue            = 117;
int red_direction   = -1;
int green_direction = 1;
int blue_direction  = -1;
  
// Function to set the color of the RGB LED
void setColor(int R, int G, int B) {
  analogWrite(PIN_RED,   R);
  analogWrite(PIN_GREEN, G);
  analogWrite(PIN_BLUE,  B);
}

// Declaration of changing variables
int ledState = 1; // tracks the current state of LED
int ldrState = 1; // tracks the current state of LED
int lastlightButtonState; // the previous state of the light button
int currentlightButtonState; // the current state of the light button
int lastLDRButtonState; // the previous state of the ldr button
int currentLDRButtonState; // the current state of the ldr button
int analogValue; // tracks the light intensity from the LDR
int brightness; // tracks the brightness extracted from the analog Input
  
void setup() {
  Serial.begin(9600);
  // Settings the Inputs and Outputs of the previously specified pins
  pinMode(BUTTON_PIN_LIGHT, INPUT);
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
  // Detect initial values of the buttons
  currentlightButtonState = digitalRead(BUTTON_PIN_LIGHT);
  currentLDRButtonState = digitalRead(BUTTON_PIN_LDR);
}
  
void loop() {
  lastlightButtonState  = currentlightButtonState; // Store Previous State of the light button
  currentlightButtonState = digitalRead(BUTTON_PIN_LIGHT); // Get new state of the light button
  // Same for the LDR Button
  lastLDRButtonState  = currentLDRButtonState;
  currentLDRButtonState = digitalRead(BUTTON_PIN_LDR);

  // Gets the Analog Value from the LDR Sensor
  Serial.print("Light Sensor Value: ");
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin
  Serial.println(analogValue);

  // Detects if there has been a change in the state of the switch - if it has been pressed
  if(lastLDRButtonState == HIGH && currentLDRButtonState == LOW) {
    Serial.println("LDR (Blue) Button is pressed: ");
    // Change state of LDR Switch
    if(ldrState == LOW) {
       ldrState = HIGH;  
       Serial.println("Turning Brightness Variation on");
    }
    else {
      ldrState = LOW;
      Serial.println("Turning Brightness Variation off");
    }
  }

  if (ldrState==HIGH){  // If the Brightness option has been switched on
    brightness = map(analogValue, 0,1023, 0, 255);
  }
  else{ // Otherwise
    brightness = 255;
  }

  // If the light button has been toggled
  if(lastlightButtonState == HIGH && currentlightButtonState == LOW) {
    Serial.print("Light (Yellow) Button is pressed: ");

    Serial.println(ledState);
  
    // Change the state of LED
    if(ledState == 0) {
       ledState = 1;  
       setColor(0, 0, 0); //set LED to Off
    }
    else if (ledState == 1){
      ledState = 2;
      Serial.println("Turning LED Rainbow");
    }
    else if (ledState == 2){
      ledState = 3;
      Serial.println("Turning LED Blue");
    }
    else if (ledState == 3){
      ledState = 4;
      Serial.println("Turning LED Green");
    }
    else{
      ledState = 0;
      Serial.println("Turning LED Red");
    } 
  }
  // Changing the colors of the LEDs depending on the State they should be in
  if (ledState == 2){
      red = red + red_direction;
      green = green + green_direction;
      blue = blue + blue_direction;
        
      // Change direction for each color if it reaches 255
      if (red >= brightness || red <= 0)
      {
        red_direction = red_direction * -1;
      }
      if (green >= brightness || green <= 0)
      {
        green_direction = green_direction * -1;
      }
      if (blue >= brightness || blue <= 0)
      {
        blue_direction = blue_direction * -1;
      }
      setColor(red, green, blue);
  }
  else if (ledState == 3){
    setColor(0, 0, brightness); //set LED to Blue
  }
  else if (ledState == 4){
    setColor(0, brightness, 0); //set LED to Green
  }
  else if (ledState == 0){
    setColor(brightness, 0, 0); //set LED to Red
  }
}

// References taken from:
// https://www.thegeekpub.com/277872/arduino-rgb-led-tutorial/
// https://www.thegeekpub.com/275412/use-a-button-to-toggle-an-led-arduino-tutorial/
// https://www.thegeekpub.com/275412/use-a-button-to-toggle-an-led-arduino-tutorial/

 

The code is making use of complex state machines, particularly the ledState, and even the ldrState. These states are toggled with the pressing of buttons, and then ultimately control the modifications to other values

Difficulties & Improvements

As I am controlling the brightness, in the RGB LED, I was able to control the brightness when each individual light was switched on only itself with some discrete value. However, as the values fluctuated for the Rainbow mode, the implementation of the Brightness option did not go as planned:

if (ledState == 2){
    red = red + red_direction;
    green = green + green_direction;
    blue = blue + blue_direction;
      
    // Change direction for each color if it reaches 255
    if (red >= brightness || red <= 0)
    {
      red_direction = red_direction * -1;
    }
    if (green >= brightness || green <= 0)
    {
      green_direction = green_direction * -1;
    }
    if (blue >= brightness || blue <= 0)
    {
      blue_direction = blue_direction * -1;
    }
    setColor(red, green, blue);
}

As shown above, I have tried to set the maximum values as the brightness, however, since the values are changing very fast, there is some issue with the brightness not being adjusted with the above code. I have tried to implement the usage of MOD, like red%brightness, but that has resulted in no effort either!

Visual Distance Detector

The idea of this work was inspired by the car parking sensor. While parking your car starts playing a sound that gets louder the closer you get to an object. Likewise in my Arduino the close you get to the object the “Redder” the RGB LED light becomes. The farther you are from the object, the sensor becomes a blueish color. This way you can visually measure the distance between you and an object.

The ultrasonic sensor measures the time taken to get the response back from the object, which I later converted into the distance. RGB LED light displays the color based on the distance calculated before. A switch turns on the whole program when pressed. To display, the program works a green LED light lights on when the switch is pressed.

 

 

This snippet of the code is the main logic of the program. Quite simple. If the button is pressed then display the distance by showing Red and Blue colors. Otherwise, turn them to zero, and the green Led light to Low value.

rgb_color_value= constrain(distance, 0, 255);
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    analogWrite(PIN_RED, max(0, 255 - 2 * rgb_color_value));
    analogWrite(PIN_BLUE, rgb_color_value);
  }
  else {
    digitalWrite(LED_PIN, LOW);
    analogWrite(PIN_RED, 0);
    analogWrite(PIN_BLUE, 0);
  }

In the future, I would like to add the sound just like in the car. This will allow measuring distance without looking anywhere. Also, a cool idea would be added to connect a projector to Arduino that displays an image stating “Stop! Too close” or “It’s ok. Go farther”. It would definitely look cool 🙂

Week 9 – Variable Threshold Lightsensor

 

Concepts

In this week’s HW, the concept is a  variable threshold light sensor where the blue led lights up depending on if the ambient light is higher than the threshold which is controlled by the switch.

I built a simple Arduino circuit that used an analog sensor and a digital sensor to control two LEDs. To begin, I connected an LDR (Light-Dependent Resistor) and a switch to the Arduino board. The LDR measured the ambient light level, and the switch was used to control the threshold at which the LDR LED turns on.

The Demo


Code

For the coding part, I programmed the Arduino to turn on the LDR LED when the ambient light level fell below the threshold set by the switch. Conversely, when the ambient light level was lower than the threshold, the LDR LED turned off. To make the circuit more interactive, I added a feature that allowed me to adjust the threshold of the LDR LED by pressing the switch. Each time I pressed the switch, the threshold decreased by 200, making the LDR LED turn on at a lower ambient light level. Once the threshold reached 0, it looped back to the maximum value of 1023, allowing me to adjust the threshold again.

digitalSensorState = digitalRead(digitalSensorPin);
if (digitalSensorState == HIGH) {
  switchPressCount++;
  ldrThreshold -= 200;
  if (ldrThreshold < 0) {
    ldrThreshold = 1023;
  }

In addition to controlling the digital LED with the switch and LDR, I also programmed the analog LED to change its brightness depending on the threshold set by the switch. The analog LED became brighter as the threshold decreased. This was done using the map function.

int brightness = map(ldrThreshold, 0, 1023, 255, 0);
analogWrite(analogLEDPin, brightness);

Challenges and Future changes

During the development of this project, one of the challenges I faced was accurately calibrating the LDR threshold with the switch. Ensuring that the threshold value was accurate and consistent required some trial and error. Additionally, mapping the threshold value to the analog LED brightness required some experimentation to ensure that the changes in brightness were noticeable and distinct. In terms of possible future changes and improvements, one idea would be to add additional sensors, such as a motion sensor, to further expand the functionality of the circuit.

Thirst meter – Analog and Digital Input and LEDs

Concept
This project is a simulation of human thirst. We have two LED lights:
– A blue light, which indicates how ‘full’ of water we are. In the code, our ‘fullness’ is represented by the variable ‘capacity’. The lower the capacity, the thirstier we are. The blue light’s brightness indicates our capacity.
– A red light, which is a warning light. When our capacity goes below a threshold, the red light blinks, as a warning that we’re thirsty.
Moreover, we have two inputs:
– A push button. Clicking it refills our capacity to maximum.
– A photosensor. When we cover the photosensor with our hand, the capacity goes down faster, which is similar to how physical activity (e.g. exercise) makes us thirstier faster.
Video demonstration
Circuit
The circuit.
Code

Because the code is pretty long, you can find the whole thing here.

Forgotten Item Switch

Concept

The concept is pretty simple make an LED that reminds you if you forgot to grab something before leaving your room. The light will stay on until you picked up all the things you need before going outside. Only when you picked up everything it will turn off.

Circuit

1 LED, 5 jumper wires, a breadboard, an Arduino board, and 1 220-Ohm resistor were used.  The switch makes use of a simple circuit that introduces an opened circuit when at least one item is still on the table and a closed circuit when all items are taken out.

Reflection

After building this switch, I gained some practical experience in working with an Arduino board and various electronic components. I learned how to connect different components together on a breadboard and how to use jumper wires to make connections between them. I also learned how to use a resistor to limit the current flowing through the LED.

I learned about the concept of switches and how they can be used to turn electrical circuits on and off.  During the process of building the switch, I encountered some difficulties or errors, and I had to use my problem-solving skills to identify and fix the issues. This taught me the importance of troubleshooting and problem-solving when building electronic circuits.

Finally, successfully building the switch and seeing it work gave me a sense of satisfaction and accomplishment. It also gave me some inspiration for further projects or ideas for using switches and other electronic components in creative ways.

Overall, building this switch was a valuable learning experience that helped me develop my skills in electronics and problem-solving.

Diary Switch

Concept

The concept for this switch is derived from the idea of bookmarks. In this case, when you close a book, there is a light indicator. I made this as a motivation to write poems in an orderly manner before sleeping every night.

Circuit

The switch makes use of a simple circuit that introduces an opened circuit when a book is opened and a closed circuit when the book is closed. See below for circuit diagram.

Remarks

The assignment gave me a keen understanding of how the Arduino breadboard works as I needed to come up with a way to separate the circuit without removing wires from the Arduino Uno board. Overall, challenges I faced was coming up with a creative switch idea.

 

Assignment 5: Unusual Switch

Police Car

Concept

The concept of the creating this unusual switch was to use to design the circuit in such a manner that the LEDs lights would represent the alternating lights on a police car. Coincidentally, I also found a metallic bell in my room which I thought would be the perfect complement for this switch.

The following circuit was used to give this effect:

 

 

 

 

 

 

The red and the blue LEDs were connected in parallel each with its own resistor to each side of the bell. They are connected to the Arduino by a 5V wire and another ground wire. The second ground wire was connected to the center of the bell. The bulbs were initially placed in series but the idea was to light bulbs alternatively such that if one bulb is off the other can still turn on when the bell strikes the respective edge. The wires were attached to the bell as shown in the picture below:


Through this assignment, I got a much better understanding of how the rows and columns in a breadboard our connected to each other. I also learned the importance of using resistors after having short circuited two LED lights. I accidentally connected the whole circuit without using any resistors which led to the LED lights blowing up. Overall, this was a very fun project to work on and it was very creatively stimulating to design a switch using an external object.

Demo Video:

This is how the complete switch works:

 

HW 5: Creative Switch using a trash bin

Inspiration

Making a creative hands-free switch that could turn on a light inside a trash can served as the motivation for this project. The intention was to design a switch that could be activated with the foot or another body part, eliminating the need to touch the trash can with your hands. This could be useful in situations where your hands are dirty or full, and you want to avoid the spread of germs or in the case where the room is dark.

Concept

The concept for the switch was to use two pieces of aluminum foil attached to any two surfaces of the trash bin that come in contact as soon as the pedal of the trash bin is pressed. When the bin is opened, the foils come into contact, completing the circuit and allowing current to flow through an LED. The switch is powered by an Arduino board, and the LED provides visual feedback to let the user know when the switch has been activated.

Implementation

To implement the switch, I first attached two conductive aluminum foils to the lining of the trash bin, making sure they were securely attached and wouldn’t come loose over time. I then connected one wire to the positive terminal of the LED and the other wire to the positive terminal of the Arduino board (3.3V). I connected the negative terminal of the LED to the ground terminal of the board, completing the circuit.

When the trash bin is opened, the two wires come into contact, allowing current to flow through the LED and light it up. The LED provides visual feedback to the user, letting them know that the switch has been activated.

Pictures

Circuit Image
Aluminum foil plates

Challenges

One of the main challenges I faced was making sure that the conductive wires were securely attached to the lining of the trash bin. I experimented with a few different adhesives before finding one that worked well. I also tried out by putting the foil in different parts of the trash can before finally finding a place that remained completely hidden to the user and would definitely come in contact every time the bin was opened. Another challenge was making sure that the wires were positioned in such a way that they wouldn’t come into contact accidentally and complete the circuit when the switch wasn’t being used.

Reflections

I am pleased with how the switch turned out. It is simple yet effective, and it meets the goal of creating a hands-free switch for a trash bin light. I learned a lot about working with conductive materials and designing circuits, and I feel that this project could be a useful starting point for other similar projects in the future.

Assignment 5: Unusual Switch

Concept

As part of the 5th Assignment of Intro to IM, being tasked with using creativity to build a switch, I tried to think of a practical use case of such a switch. In the process of brainstorming, while I was having water in a mug, I tried to sip but realized the mug was empty. This triggered an idea for me to design a mug, which essentially indicates the level of water that remains within it.

Implementation

With limitations of equipment available, I had to make do with the things I had around me and within the Kit provided.Hence, I decided to use three lights, which could intuitively indicate the level of water in the mug – Red (Low), Yellow (Medium), and Green (High). As for the mug, to demonstrate the idea, I decided to use a transparent container (which I got with my Iftar Meal from D2), and connected the + (Power/Positive) of the three LED Bulbs at different levels, and connected Ground at the lowest level. Hence, when there was water in the Container, the LED Bulbs would light up according to the level of water that was left in the container.

Pictures of Circuit

With this, I produced the following circuit:

Demo Video

A demo video, in detail, can be seen here:

Improvements

While the existing version of the tool does not use any code, the utilization of the Arduino Coding Environment could make it more advanced, such that allowing us to only have the ‘highest level’ of LED switched on. In our case, when the water level is the highest, all three LEDs stay switched on, and with code we could reduce that to the light only on that level being switched on.

Unusual Switch | Mouth Stick

Inspiration

In my favorite movie “The Intouchables”[1+1, alternatively], the main character Philippe is a disabled person, and he is paralyzed below his neck. There was a moment in the film when it was shown how he uses the mouth stick, instead of hands, to turn pages of the physical book. Mouth stick is a useful tool for those who do not have hand functionality and need assistance with writing, typing, pointing, or turning a page of a book as in the movie.

For this homework assignment, I tried to replicate the idea in a quite abstract way of using mouth to touch a panel and let LED light signal that the touch occurred. It may be useful on an idea basis as the signal to turn the page using digital devices with touch sensors now.

Implementation

Implementation is very similar to the circuit we built in class using LED light. The difference lied in adding two more wires that were also covered in aluminum foil as the conductive material. One of the wires was connected to a cardboard panel covered by foil, while the other one was connected to a plastic straw that I used to replicate mouth stick. The foils/wires had to touch with each other to make circuit complete and let LED turn on. When there was no contact, LED light was off.  Thus, touching panel with mouth stick was signaled by turning on LED light.

Demo