RGB Light

I really wanted to make a cycling RGB light for this week’s homework so I did some research on the RGB LED in the kit and how to use it, and I ended up making a circuit in which the switch lets the user switch between control of the two LEDs, one RGB and the other just blue and a potentiometer to control the color of the RGB and the brightness of the other LED. To learn how to use the RGB LED, I went through other projects that used it online and learned that it sort of acts as 3 separate LEDs one red, one green and one blue. Using analogWrite() with each pin allows you to generate different colors the same way we used color() in p5js.

I wanted to make the light cycle through colors as seen in some gaming keyboards, for example. To achieve this, I mapped the value from the potentiometer to 0-767 (256×3), and I found that keeping one of the 3 colors at 0 and changing the other two colors according to the mapped value would achieve a nice cycling effect. I was inspired by this project to come up with this.

When the switch is pressed, the RGB LED would turn off, the blue LED would turn on and the potentiometer would then be used to control the brightness of the LED.

Here is my code and video:

bool pressed = false;
unsigned long timer = 0;
int prevButtonState = 0;
void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT); //bluepin
  pinMode(3, OUTPUT); //redpin
  pinMode(5, OUTPUT); //greenpin
  pinMode(10, OUTPUT); //ledpin
  pinMode(9, OUTPUT); //switch
}
void loop() {
  int potValue = analogRead(A0);
  int buttonState  = digitalRead(9);
  if (buttonState == 1 && prevButtonState == 0){
    pressed = !pressed;
  }
  prevButtonState = buttonState;
  int mappedValue;
  int r;
  int g;
  int b;
  if (pressed){
    digitalWrite(2, LOW);
    mappedValue = map(potValue, 0, 1023, 0, 767);
    int color = mappedValue;
    color = constrain(color, 0, 767);
    if (color <= 255){
      r = 255 - color;
      g = color;
      b = 0;
    }
    else if (color <= 511){
      r = 0;
      g = 511 - color;
      b = color - 256;
    }
    else{
    r = color - 512;
    g = 0;
    b = 767 - color;
    }
    analogWrite(6, r);
    analogWrite(5, g);
    analogWrite(3, b);
  } else {
    analogWrite(6, 0);
    analogWrite(5, 0);
    analogWrite(3, 0);
    mappedValue = map(potValue, 0, 1023, 0, 255);
    analogWrite(10, mappedValue);
  }
}

Week 9 – object detector (kind of)

Idea

Thinking about the use of LEDs in a more practical and engineering way, I came up with the idea of creating an object detection sensor, which is widely used in tesla cars, home security systems, etc.

Implementation

As an object detector, I used an ultrasonic sensor (digital input). I also used a photosensor as a sort of solar power, which enables the work of the whole system (analog input). 

There are three modes to detect an object depending on its distance from the ultrasonic sensor. You can identify if the object is close or far away by looking at which LED light is turned on and which sound is produced. If an object is close, the red LED will be turned on. If an object is at a medium distance, the yellow LED light turns on, which serves as a warning. And if an object is at a normal distance, the green LED is turned on all the time. 

Code

int sensorPin = A0; // select the input pin for the potentiometer
int powerPin = 7;
int ledPin1 = 11; //  red LED
int ledPin2 = 10; // yellow LED
int ledPin3 = 9; // green LED
int buzzPin = 4;
const int trigPin = 12;
const int echoPin = 13;

long duration;
int distance;

bool gameStarted = false;

int sensorValue = 0; // variable to store the value coming from the sensor

void playSound(int hz) {
  tone(buzzPin, hz);
  delay(hz);
  noTone(buzzPin);
}

void setup() {
  // declare the ledPins as an OUTPUT:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buzzPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(powerPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  digitalWrite(powerPin, HIGH);

  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);

  //to turn on the object detection by photo sensor (solar power)
  if (sensorValue <= 30) {
    gameStarted = false;
    delay(2000);
  }

  //if there is no light, the system does not work
  else if (sensorValue > 30) {
    gameStarted = true;
  }
  
  if (gameStarted) { //to measure a distance
    if (distance <= 5) { //red led turns on at the lowest distance

      // turn the ledPin on
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      playSound(200);

      delay(100);
      // turn the ledPin off:
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      delay(100);
    }
    else if (distance > 5 && distance <= 10) { //yellow led turns on at the medium distance
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
      playSound(1000);

      delay(100);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      delay(100);
    }

    else if (distance > 10) { //to turn on the green led

      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH);

      delay(100);

      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);

      delay(100);
    }
  }
}

Drive Safe 🚙 (Assignment 6)

Inspiration 🚘:

When faced with the use of both analog and digital sensors, my mind initially went to cars. Especially their interface right in front of the steering wheel that indicates fuel, if lights are turned on or off, speed, and many other components. I imagine they must use both,  digital and analog circuits, for all these LED’s and indicators. And thus, decided to build my own. Throughout my brainstorm process I was also reminded of Ruta, my favorite card game. In this board game, you need a green light card to signify the car running. A yellow/red light would indicate you could not drive, nor obtain points.

Building Process 🚧:

Going off from this concept, I placed three LED’s, a red one, a yellow one, and a green one in the shape of a stoplight. This would be my visualization of the car running or not. I then added three elements that are crucial for driving successfully:

      1. Pressing the Accelerator
      2. Adjusting the Steering Wheel
      3. Turning on the Headlights if it’s dark

With these requirements, I built the following:

      1. A digital circuit for the red light. It is on, only if you press on the red button, which represents the gas pedal.
      2. A circuit with an analog sensor for the yellow LED. It will blink unless the steering wheel, a potentiometer,  is oriented towards the right side, in which it will be turned off.
      3. An analog circuit with the Blue LED light. It will light up as per the light it receives from the photosensor.
      4. IF the light received passes a certain threshold, the accelerator is pressed, and the steering wheel is facing the correct way, the green light will turn on, and you will be on your way!

Here is my Code:

const int greenPin = 7;
const int yellowPin = 4;
const int redPin = 2;
const int buttonPin = 3;
const int wheelPin = 8;
const int bluePin = 9;
unsigned long timer = 0;
bool onOff = LOW;

void setup() {
  Serial.begin(9600);
 pinMode(greenPin, OUTPUT);
 pinMode(yellowPin, OUTPUT);
 pinMode(redPin, OUTPUT);
 pinMode(bluePin, OUTPUT);
 pinMode(buttonPin, INPUT);

}

void loop() {
bool redState = digitalRead(redPin);
bool yellowState = digitalRead(yellowPin);

// RED //
byte buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
  digitalWrite(redPin, LOW);
} else
{digitalWrite(redPin, HIGH);}
// RED//

//YELLOW//
int wheelValue = analogRead(A0);
int mappedValue = map(wheelValue, 0, 1023, 0, 255);



if (mappedValue > 110 && mappedValue < 140) {
  digitalWrite(yellowPin, LOW);
} else {

 if(millis()>timer){
      onOff = !onOff;
      timer = millis() + 200;
      digitalWrite(yellowPin, onOff);
      }
      }

//YELLOW//


// BLUE //
int lightValue = analogRead(A1);
int mappedLightValue = map(lightValue, 655, 1000, 0, 255);
int constrainedValue = constrain(mappedLightValue, 0, 255);

analogWrite(bluePin, constrainedValue);
//Serial.println(lightValue);
// BLUE //

// GREEN //
if (redState == LOW && yellowState == LOW && mappedValue > 110 && mappedValue < 140 && constrainedValue > 200){
 
 digitalWrite(greenPin, HIGH);
} else {
  digitalWrite(greenPin, LOW);  
  }
// GREEN //
 
}

 

Initially, I had the yellow LED to blink at a speed determined by the analog input of the potentiometer. The higher the value, the fastest the blinking. However, this gave the project an unorganized structure and distracted the user from the goal of turning on the car. Thus, I gave it a set value.

Another important thing to note is that during the day, the blue LED will always be turned on. This is intentional, and is used to signify how headlights are only needed at night. If you wish to change this, alter the mappedLightValue variable and make the 655 higher to a threshold of about 950+.

Final Product 🚨:

I am very happy with how Drive Safe turned out. After I completed the circuit, I made some decorations to make it look cleaner and easier to understand. I have tested it multiple times, in multiple scenarios and it seems to work perfectly fine. I also wrote a set of instructions for anyone using it without me.

In the future, I would like to experiment with other type of sensors, and figure out a way to make cables not visible, as they damage the aesthetic quality of the piece a lot.

Study Lamp

In this assignment, I decided to make study lamp which can be turned on or off and have adjustable brightness. To implement this, I decided to make an LED respond to how close I am to it. The LED is turned on using the switch and its brightness is varied using the ultrasonic sensor. I calculate the distance away from the ultrasonic sensor in centimeters using the time it takes for the ultrasonic sensor to read the trigger pulses.  Now, the closer I am to the ultrasonic sensor, the brighter the LED becomes if it is switched on. So, when I am studying, I can change the brightness levels depending on how close I sit next to the ultrasonic sensor.

Here is the circuit:

 

Here is the code:

const int trigPin = 3;
const int echoPin = 2;
const int led = 9;
const int buttonInp = 11;

bool run = false;
bool prevButtonState = LOW;

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  pinMode(led, OUTPUT); 
  pinMode(buttonInp, INPUT);
  Serial.begin(9600);
}
void loop() {
  byte buttonState  = digitalRead(buttonInp);

  Serial.println(buttonState);

  // check to see if the button is pressed and last time it wasn't
  if (buttonState == HIGH && prevButtonState == LOW) {
    //toggle run
    run = !run;
  }
  // record the current button state for use next time through loop
  prevButtonState = buttonState;
  
  if(run){
    // Clear trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    
    //Set trigPin to HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    
    // Calculating the distance
    distance = duration * 0.034 / 2;
    // Serial.println(distance);

    // Adjusting brightness of LED to be distance mapped to 0-255
    int brightness = map(distance, 0, 15, 0, 255);
    brightness = constrain(brightness, 0, 255);
    brightness = 255 - brightness;
    analogWrite(led, brightness);
  }
  else {
    //LED off
    digitalWrite(led, LOW);
  }
}

Here is the final product:

 

Hind – Traffic Light!

My idea was to make a traffic light with the red, yellow and green LEDs. I decided to use a single switch button that cycles between modes as well as a potentiometer that changes the LED activated.

In order to make the button that cycles between modes, I got a lot of help from https://gist.github.com/navillus5/6699360

The 3 modes of my traffic light:

  1. All LEDS blinking
  2. Potentiometer activates either of the red, yellow, and green LEDs when dialed to a specific point
  3. Off

Overall I think I executed my idea well as everything I intended for the assignment works, however one small issue I ran into is that when switched to the 2nd mode, the LEDS are very dim. My best guess is that it has to do with the resistors but I haven’t tested out this theory as I don’t want to accidentally damage my LEDs.

My Code:

const int buttonPin = 3;
const int redLight = 9;
const int yellowLight = 6;
const int greenLight = 5;
unsigned long timer = 0;
bool onOff = LOW;
byte prevButtonState = LOW;
bool blinking = false;
byte buttonMode = 0;
bool traffic = false;

boolean currentState = LOW;
boolean lastState    = LOW;
boolean stateChange  = false;

int currentButton = 0;
int lastButton    = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(redLight, OUTPUT);
  pinMode(yellowLight, OUTPUT);
  pinMode(greenLight, OUTPUT);
  digitalWrite(redLight, LOW);
  digitalWrite(yellowLight, LOW);
  digitalWrite(greenLight, LOW);
  pinMode(buttonPin, INPUT);
}

void loop() {
  currentState = debounceButton();
  stateChange = checkForChange(currentState, lastState);
  currentButton = getButtonNumber(lastButton, currentState, stateChange);
  lastState  = currentState;
  lastButton = currentButton;
  
  // put your main code here, to run repeatedly:
  int potValue = analogRead(A0);
  int mappedPotValue = map(potValue, 0, 1023, 0, 255);
  int constrainedPotValue = constrain(mappedPotValue, 0, 255);
  Serial.println(currentButton);
//  byte buttonState  = digitalRead(buttonPin);

  if (currentButton == 0) {
    blinking = true;
//    buttonMode = buttonMode + 1;
  }
  if (currentButton == 1) {
    blinking = false;
    traffic = true;
  }
  if (currentButton == 2) {
    traffic = false;
  }

  if (traffic == true) {
    if (constrainedPotValue > 170) {
       digitalWrite(redLight, HIGH);
       digitalWrite(yellowLight, LOW);
       digitalWrite(greenLight, LOW);
    }
    if (constrainedPotValue <= 170 && constrainedPotValue > 85) {
       digitalWrite(redLight, LOW);
       digitalWrite(yellowLight, HIGH);
       digitalWrite(greenLight, LOW);
    }
    if (constrainedPotValue <= 85) {
      digitalWrite(redLight, LOW);
      digitalWrite(yellowLight, LOW);
      digitalWrite(greenLight, HIGH);
    }
  }
  
  if (blinking == true) {
    if (millis() > timer) {
      onOff = !onOff;
      timer = millis() + 250;
      digitalWrite(redLight, onOff);
      digitalWrite(yellowLight, onOff);
      digitalWrite(greenLight, onOff);
    }
  } else {
    digitalWrite(redLight, LOW);
    digitalWrite(yellowLight, LOW);
    digitalWrite(greenLight, LOW);
  }
}

// FUNCTIONS

//debounce
boolean debounceButton()
{
  boolean firstCheck   = LOW;
  boolean secondCheck  = LOW;
  boolean current = LOW;  
  firstCheck  = digitalRead(buttonPin);
  delay(50);
  secondCheck = digitalRead(buttonPin);  
  if (firstCheck == secondCheck){
    current = firstCheck;
  }
  return current;
}


//checks for change
boolean checkForChange(boolean current, boolean last)
{
  boolean change;  
  if (current != last){
    change = true;
  }
  else {
  change = false;
  }  
  return change;
}

//gets button num
int getButtonNumber(int button, boolean state, boolean change)
{
  if (change == true && state == LOW){
    button++;
    if (button > 2){
      button = 0;
    }
    Serial.println(button);
  }
  return button;
}


 

Dozing Off

Idea:

My idea for the assignment is a person who is studying, turned on his desk lamp (digital input and output) then doses off and as the pressure sensor senses the body, the light starts dimming (analog input and output).

Clearer video where desk lamp lights https://studio.youtube.com/video/I4NiezThfGQ/edit

const int ledPin = 2;
const int buttonPin = 3;
const int eyePin = 5; 
int pressureValue = A0; 

byte prevButtonState = LOW;
bool brightness = HIGH;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(eyePin, OUTPUT);
  pinMode(pressureValue, INPUT);
 
  Serial.begin(9600);
}

void loop() {
  // read the button pin
  // store that in a local variable
  byte buttonState  = digitalRead(buttonPin);
  int pressureValue = analogRead(A0);

 int mappedPressureValue = map(pressureValue, 0,400, 0, 255);
  Serial.print(pressureValue);
    
   int constrainedValue = constrain(mappedPressureValue, 0, 255);
    analogWrite(5, constrainedValue);//0-255
   
  // print out the state of the button stored in the variable
  // record the current button state for use next time through loop
  prevButtonState = buttonState;

 
  if (buttonState == HIGH && prevButtonState == LOW) {
   digitalWrite(ledPin, HIGH);
    }
  // otherwise turn the LED off
   else {
    digitalWrite(ledPin, LOW);
  }
}

 

Yunho Lee Analog&Digital Switch – Christmas Tree Decoration

Concept and Inspiration

The inspiration for the project is Christmas tree decorations that have LED light bulbs blinking to make the tree look pretty.

There are one analog and one digital input that the circuit includes.

Digital – The red digital button switch chooses the blinking mode of the LED lights. (1 – off, 2 – on, 3 – blinks)

Analog – The potentiometer’s input decides the speed of the blinking of the LED lights and also the speed of the carol being played from the speaker.

How I implemented the Carol to be played in the speaker – First, I made an array of melodies where “0” indicates MUTE and from 1 to 11 each means a note. As time passes, the index will move on to the next element in the melodies array, playing the music as it is written in the melodies array.

The code I used is inserted below.

  • I have bad connections with the blue lightbulbs, so it sometimes turns off in the video.
  • I was going to make mode #4 that automatically turns the light bulb on and off depending on the light in the room, but I think I broke my light sensor while bending it, and was not able to make it happen.
#include "pitches.h"

int melody[] = {
  5,5,5,6,5,5,3,3,3,0,0,0,5,5,5,6,5,5,3,3,3,0,0,0,9,9,0,0,9,9,7,7,7,0,0,0,8,8,0,0,8,8,5,5,5,0,0,0,6,6,0,0,6,6,8,8,7,7,6,6,5,5,5,6,5,5,3,3,3,0,0,0,6,6,0,0,6,6,8,8,7,7,6,6,5,5,5,6,5,5,3,3,3,0,0,0,9,9,0,0,9,9,11,11,9,9,7,7,8,8,8,8,0,0,10,10,10,10,0,0,8,8,5,5,3,3,5,5,5,4,2,2,1,1,1,1,1,1,0,0,0,0,0,0
};

int notes[] = {
  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5
};

byte prevButtonState = LOW;
byte patternNum = 0;
unsigned long timer = 0;
unsigned long timer2 = 0;
unsigned int index = 0;

bool redOn = LOW;
bool yellowOn = LOW;
bool greenOn = LOW;
bool blueOn = LOW;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(12, INPUT);
  Serial.begin(9600);
}

void loop() {
  byte buttonState  = digitalRead(12);

  int lightVal = analogRead(A0);

  int mappedPotValue = map(lightVal, 0, 1023, 150, 500);
  Serial.println(mappedPotValue);
  
  if (buttonState == HIGH && prevButtonState == LOW) {
    if (patternNum<2){
      patternNum++;
    }
    else patternNum = 0;
  }
  prevButtonState = buttonState;

// Music play code
  if (millis() > timer) {
    timer = millis() + mappedPotValue;
    if (melody[index] != 0){
      tone(5, notes[melody[index]-1], mappedPotValue+20);
    }
    else noTone(5);
    index++;
    if (index == 144) index = 0;
  }
// Light bulbs Control
  if (patternNum == 0){
    redOn = LOW;
    yellowOn = LOW;
    greenOn = LOW;
    blueOn = LOW;
  }
  else if (patternNum == 1){
    redOn = HIGH;
    yellowOn = HIGH;
    greenOn = HIGH;
    blueOn = HIGH;
  }
  else if (patternNum == 2){
    if (millis() > timer2) {
      timer2 = millis() + mappedPotValue;
      redOn = !redOn;
      yellowOn = !yellowOn;
      greenOn = !greenOn;
      blueOn = !blueOn;
    }
  }
  else {
    redOn = LOW;
    yellowOn = LOW;
    greenOn = LOW;
    blueOn = LOW;
  }

  digitalWrite(8, redOn);
  digitalWrite(7, yellowOn);
  digitalWrite(4, greenOn);
  digitalWrite(2, blueOn);
}

 

Z’s Butterfly is back!

Ideation 

I struggled a lot with coming up with a creative idea for this assignment. I thought about my love for butterflies and considered making something butterfly related (again). Initially I thought of making a glow in the dark butterfly which would light up in the dark. However, I was still unsure about this hence reached out to Prof. with my idea and he helped me navigate through a more creative idea. Using flex sensors (analog sensor), as the butterfly flies over to the Zinnia flower, the LED gets brighter and brighter (as the flex sensor bends). For context, adult butterflies land on these flowers to suck on some delicious nectar.  The button (digital sensor), when pressed, blinks red to indicate that the butterfly is about to approach the flower.

Implementation 

It took a while to figure out how to wire everything as I’m still wrapping my head around Arduino. I used online resources to help, I watched a ton of YouTube videos and tried to make sense of it. I first coded and wired the circuits for the flex sensors & LED1. I had to make sure I was using the right resisters for each component otherwise it would not work well. I also learnt that these sensors are very sensitive – I also tested this out by printing out the values in my code.

Once I got the flex sensor wired up, I coded and wired the circuit for the button & LED2. I then printed out my butterfly & flower, taped the butterfly onto the flex sensor and attached the flower image to a lollipop stick so it’s easier to hold. The coding for the flex sensor was relatively straightforward however the coding for the button was more complex (therefore I used an online resource to help me with this – https://forum.arduino.cc/t/how-to-make-a-led-light-stay-on-when-press-switch/195814/5). The code I was writing initially for the button would only light up the LED if the button was pressed continuously and I wanted the LED to stay on when the button was pressed and then switched off when the button was pressed again.

An issue I was facing was to get the flex sensor and button to work at the same time. I was able to get them to work individually however then had to make adjustments in my code in order to enable them both to work simultaneously.

Code

//for the button
const int ledPin2 = 2;
const int buttonPin = 5;

unsigned long timer = 0;
bool onOff = LOW;
byte prevButtonState = LOW;
bool blinking = false;

//for the flex sensor
const int ledPin1 = 3; 
const int flexPin = A0; 

int value;

void setup() {
  //for the flex sensors
  pinMode(ledPin1, OUTPUT); 
  Serial.begin(9600); 

//  //for the button 
  pinMode (ledPin2,OUTPUT); 
  pinMode (buttonPin,INPUT);
}

void loop() {
  //for the flex sensors 
  value = analogRead (flexPin); 
  Serial.println (value); 
  value = map (value, 550, 800, 0, 255); 
  //   value = map (value, 700, 900, 0, 255); 
  analogWrite(ledPin1, value); 
//  delay (100);
  
  //for the button which blinks the LED (code from class exercise)
    byte buttonState  = digitalRead(buttonPin);
  Serial.println(buttonState);
  
  if (buttonState == HIGH && prevButtonState == LOW) {
    blinking = !blinking;
  }
  prevButtonState = buttonState;
  if (blinking == true) {
    if (millis() > timer) {
      onOff = !onOff;
      timer = millis() + 250;
      digitalWrite(ledPin2, onOff);
    }
  } else {
    digitalWrite(ledPin2, LOW);
  }
}

The final piece 

Video 

Dev: Night Flower

Inspiration

Pictures: Flowers Glow Under UV-Induced Visible Fluorescence

Source: Read more in this article 

For this weeks assignment I drew inspiration from this kind of night glowing flowers that only bloom up at night in the absence of sunlight/UV light. I wanted to create something similar with LED and the photo sensor in order to glow up the flower in the absence of light. In order to do this I had to use a photosensor add mad the brightness on to the range of values between 0 and 255.

This also allowed me to control the brightness level as the intensity of light changed. As shown in the video below I wanted to mimic a sunrise and sunset effect where the flower would brighten up as the environment becomes darker.

Implementation

I started by hooking up a LED and a push down button with the basic on off implementation. On top of this I used a photosensor in a 10k resistor for the connection on the circuit. The LED and button served as digital input to read data into the program while the photo center served as analog input for this project. In order to control the light I thought of doing something different than the traditional HIGH and LOW method. I thought of implementing it by varrying its light value depending on the brightness level. This was done by using analogWrite by sending the mapped light value to the LED pin.

As shown in the code below I tried to play around with the values in order to map it in a way that allowed the brightness to smoothly transition and show the dimming and fading feature. (Although this is was very satisfying for me unfortunately the camera on my phone was not able to capture the dimming effect).

Demo Video

Arduino Code

int led = 3; // LED pin 
int ldr = A0; // LDR pin
int button = 5; // Button pin
bool on = false; // Boolean variable for the state of the LED 

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT); // Set the LED pinmode
  pinMode(ldr, INPUT); // Set the LDR pinmode
  pinMode(button, INPUT); // Set the button pinmode
}
void loop() {
  
// read the brightness from the ldr
  int brightness = analogRead(ldr);
// check if switch is clicked
  if (on == false && digitalRead(button)== HIGH){
    on = true; // Setting on to be true
  }
  if (on == true && digitalRead(button) == HIGH){
    on = false;  // Setting on to be false
  }

   int mappedLightValue = map(brightness, 60, 700, 255, 0); // nothing the brightness onto the range 0 to 255 but in reverse order
  // if switch is clicked, then proceed 
  if (on==true){ // if the on state is true
    if (brightness < 370) { // 
      analogWrite(led, mappedLightValue); // writing the lights value to the LED pin
//      Serial.println(mappedLightValue); // call printing out the mapped light value for checking
    }
    else {
      digitalWrite(led, LOW); // turning the light off if it's too bright
    }
  }
  else if (on==false) {digitalWrite(led, LOW);}  // Setting the on state to be false otherwise
}

 

 

Digital and Analog Switches: Nicholas

Inspiration

I wanted to create a moving spotlight using a potentiometer that work as if you are pointing at a light bulb and turning it on. I also wanted to create two different modes that would alternate between analog and digital signals using a button.

Implementation

First, I had to create the logic for the analog and digital switches based on the potentiometer value. I did this with the following code:

const int LEDpins[] = {3, 5, 6, 10, 11};

double mapf(float value, float fromLow, float fromHigh, float toLow, float toHigh) {
  return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
} 

double mappedD(float mapValue, int currPin){
  double absDist = abs(mapValue - currPin);
  return mapf(absDist, 0, PIN_LENGTH-1, 255, 0);
}

//code for analog write
double mapValue = mapf(potValue, 0, 1023.0, 0, PIN_LENGTH-1);
for(int i = 0; i < PIN_LENGTH; i++){
    analogWrite(LEDpins[i], max(0,mappedD(mapValue, i)-100));
}

//code for digital write
int intMapVal = map(potValue, 0, 1023, 0, PIN_LENGTH-1);
for(int i = 0; i < PIN_LENGTH; i++){
    digitalWrite(LEDpins[i], intMapVal==i);
}

By creating my own map functions, I was able to have a floating point value that determined how much each light bulb should light up. For the analog setting, the light was too bright at most values, so I decreased the values by 100 and constrained them to prevent integer underflow.

I also did added some logic for the button handling, and a light show would play while holding the button down, and would switch between analog and digital on release.

if(buttonState == LOW && prevState == HIGH){
   toggled = !toggled;
   for(int i = 0; i < PIN_LENGTH; i++){
      analogWrite(LEDpins[i], false);
    }
 }
 if(buttonState == HIGH && prevState == HIGH){
  if(millis() > timer){
    timer=millis()+interval;
    for(int i = 0; i < PIN_LENGTH; i++){
      LEDOn[i] = random(2);
    }
   }
   for(int i = 0; i < PIN_LENGTH; i++){
      digitalWrite(LEDpins[i], LEDOn[i]);
    }
 }

In doing this, I was able to create a light show dependent on the switch, which takes in a digital input, and potentiometer, which takes in a continuous input.

Reflection

Moving forwards, I would like to experiment with different mathematical functions for displaying luminosity as a linear scale may not have been best. I would also like to explore different ways to turn the knob as it was very difficult to turn and observe the effects without blocking the view.