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.

Home Security – Week 9

Ideation

For this week, I wanted to use digital and analog sensors to create a home security setup. The idea is very simple, and it consists of four key components.

Components

  1. The switch: this is what allows the person to turn on the security alarm
  2. The red LED: this blinks when the alarm is triggered
  3. The buzzer: this beeps when the alarm is triggered
  4. The pressure pad: this detects if anyone is attempting to enter the house

In addition, there is a lamp controlled via the potentiometer. You can think of this as a bedside lamp that the person turns off before going to bed.

Schematic and Code

Find the code here.

Video

Week 10 – Servo and Tone

pitches.h:

/*************************************************
 * Public Constants
 *************************************************/
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#include <Servo.h>
#include "pitches.h"

Servo servo;
int servoPos = 100;
int whichNote = 0;
int notes[10] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5};
int durations[10];

void setup() {
  servo.attach(9);
  pinMode(4, OUTPUT);
  Serial.begin(9600);

  // set the durations with a random coinflip
  for (int i = 0; i < 10; i++) {
    int coinFlip = random(2);
    if (coinFlip == 0)
      durations[i] = 8;
    else
      durations[i] = 4;
  }
}

void loop() {
  int val = analogRead(A0);

  // the rate is 1 second divided by the duration of the note
  int rate = 1000 / durations[whichNote];
 
  // get the current time
  unsigned long currentTime = millis();

  // trigger a note
  if (currentTime % rate == 0  ) {
    tone(4, notes[whichNote], random(100, 400));
    whichNote = random(10);
    delay(1);
  }

  // do the servo at half speed
  if (currentTime % (rate * 2) == 0  ) {
    servoPos = 50;
    servo.write(servoPos);
  } 
  
  // else if not triggereing the servo, then every 10 milliseconds move the servo arm back a little bit
  // can't do it every frame as that is too fast for the servo
  else if (currentTime % 10 == 0) {
    servoPos -= 1;
    servo.write(servoPos);
  }
}
  • Timer0 – used for millis(), micros(), delay() and PWM on pins 5 & 6
  • Timer1 – used for Servos, the WaveHC library and PWM on pins 9 & 10
  • Timer2 – used by Tone and PWM on pins 3 & 11

Class Discussion – Nicholas & Lily

Questions:
1. Agree or Disagree: “Your task in designing an interactive artwork is to give your audience the basic context, then get out of their way.”
2. What was your favorite computing creation from the first reading?
3. What strategies can you use to incorporate “shut up and listen” methodology in future IM projects?
4. How can we account for generation differences (ie. familiarity with tech) when we create Interactive art?
5. Share a (rough) drawing of a potential interactive art project that utilizes ideas from the first reading — for this one, think about it, the actual drawing will happen during class!

Yunho Lee Hand-Free Switch Assignment

Concept and how the switch operates

Concept: In most of the human living places in the United Arab Emirates, seawater is one of the main sources of daily water. I designed the switch so that it can let the user know when the sea water level inside the water tank is above a certain point.

How the Switch operates: Saltwater has Na+ (Sodium ions) and Cl- (Chloride ions) that help the water conduct electricity. Therefore, the two metal rods connect the two sides of the circuit through the saltwater’s conductivity, turning the LED on when the water level rises enough to hit both rods.

Week 9b – Analog Input & Output

// NOTE: to make a voltage divider for the photo resistor (which you need)
// use a 10k ohm resistor

void setup() {
  // begin the serial connection at 9600 bits per second
  // (the baud rate)
  Serial.begin(9600);
  // for analog output connect to any of the PWM pins
  // these all have a tilda beside the pin number (~)
  pinMode(5, OUTPUT);
}

void loop() {
  // read the potentiometer on analog input pin A0
  int potValue = analogRead(A0); // analog read gives values in a maximum range of 0-1023
  // map the values from the potentiometer from 0-1023 
  // into the range of an analog write (0-255)
  int mappedPotValue = map(potValue, 0, 1023, 0, 255);
  
  // read the photocell on analog input pin A1
  int lightValue = analogRead(A1);

  // print out the light value so we know the range
  Serial.println(lightValue);
  
  // based on the lowest and the highest numbers from the sensor, 
  // map those to the analogWrite range
  // for example, if the lowest number is 500 nd the highest is 900 
  // then do:
  int mappedLightValue = map(lightValue, 500, 900, 0, 255);

  // sometimes the numbers might go lower or higher than 500 or 900
  // it's important to constrain the mapped value 
  //to never go lower than 0 and never higher than 255
  int constrainedValue = constrain(mappedLightValue, 0, 255);

  // finally, take the number from the photocell, that was mapped,
  // and then constrained, and use that for the call to analogWrite
  analogWrite(5, constrainedValue);//0-255
}

 

Week 8: Unusual Switch

This week’s assignment was to create an Unusual switch, where hands isn’t required to turn on the switch. My idea was around creating a track and when a ball passes over it, the bulb/switch turns on.

Here is the circuit:

Basically, I have connected one end of the switch with the track (copper strip) and one to the foil, over which the ball would pass.

Here are some pictures of the circuit:

While the ideation took some time, it was fun making it!

Light Sensor

For my circuit, I decided to use an LDR, because to me touch-less circuits oftentimes include elements that could rely on sound or on light. I used a 2222 Transistor to switch the process of having my led light up when there is light, which felt somewhat useless. Instead, I have my circuit configured in a way that inverses this process and makes my LED light up when the LDR is not met with light (detects darkness).

The use of this circuitry could easily be attributed to smart lighting systems which turn the light on when it is dark. I was initially inspired by this process of smart systems as I have them integrated all over my room. It was very interesting looking at a very simplified version of this process and making it myself with the basic mechanics.

In this demo I use my hands to create the shadow over the LDR, but you could use anything to create a shadow, which is why I would say my circuit is hands-free, as no hand contact comes to the circuit for the light bulb to turn on, only shadow/light.

https://drive.google.com/file/d/1tnnrFYYkjPxqjYdOkOOp6tNWB11C8N0z/view?usp=sharing