As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Friday, June 12, 2020

Analog Output Experimentation

In this project, I used the photoresistor and buzzer to make a buzzer that would interact with the photoresistor and button.

BREADBOARD:


CODE:
The code is very simple and just takes the reading of the photosensor and maps that as the pitch for the buzzer. When the button is pressed the buzzer will sound at the frequency based on the photoresistor.

int buttonPin = 2;

void setup() {
  // initialize serial communications (for debugging only):
  Serial.begin(9600);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // print the sensor reading so you know its range
  Serial.println(sensorReading);
  // map the analog input range (in this case, 400 - 1000 from the photoresistor)
  // to the output pitch range (120 - 1500Hz)
  // change the minimum and maximum input numbers below depending on the range
  // your sensor's giving:
  int thisPitch = map(sensorReading, 0, 500, 120, 1500);

  // play the pitch:
  if (digitalRead(buttonPin)){
    tone(9, thisPitch, 10);
    delay(1);        // delay in between reads for stability
  }
}

Take Apart Project


In this project, I decided to take apart my desk "AC" fan. This fan has a water/ice tray that can be used to cool down the air of the fan to give an airconditioning affect. This fan is very simple with a battery and small board to adjust fan speed and intermittent fan mode. It also has a few LED to indicate battery charge and the fan speed/mode. There is also a negative ion generator which does not seem to do much, but apparently can release negative ions that go into the air to attach to positively charged particles like dust, smoke or other allergens that would then dissipate from the air in your room.

This fan simply takes input from the fan speed to turn on and from there you can adjust the mode. Once the button is pressed the signal will be sent to the fan module to start spinning its motor. If the mode is pressed it will then intermittently turn off and on every 10 seconds. Indicator lights will also turn on when each button is pressed to give feedback to the user.

Simple Circuit Experimentation

In this project, I use 2 LED's and a photoresistor to make a simple circuit that mimics cop light patterns. When there is more light present to the photoresistor the lights flash quicker.

BREADBOARD:

CODE:
The method I used to get the photoresistor to change the speed of the flashes was to make the blinkDelay variable map the light value within a specific range. When the light is mapped it can be fine-tuned to the correct range of blinkDelay values you want.
The only issue I found was that the blinks would not gradually get faster, but it would quickly change its blink delay after it went through a cycle. So in essence, when a light is flashed on the photoresistor it would finish its cycle then update to the shorter blink delay.
This can be seen in the video above.

//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0

//Variables
int LED1 = 8;
int LED2 = 12;
int blinkDelay;
int lightVal;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  lightVal = analogRead(pResistor);
  blinkDelay = map(lightVal, 200, 700, 400, 100);
  Serial.println(lightVal);
    for(int x = 0; x < 2; x++) {
      digitalWrite(LED1, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(blinkDelay);
      digitalWrite(LED1, LOW);
      delay(blinkDelay);
    }
    delay (blinkDelay);
    for(int x = 0; x < 2; x++) {
      digitalWrite(LED2, HIGH);
      delay(blinkDelay);                       // wait for a second
      digitalWrite(LED2, LOW);    // turn the LED off by making the voltage LOW
      delay(blinkDelay);                       // wait for a second
    }
  }

Final Project (automatic fridge closer)

In this project, I use the photoresistor, stepper motor, LED, and buzzer to make a self-closing fridge. The goal of this project is to make something that detects when the fridge is open for X amount of time and closes it after that time.

BREADBOARD:

HOW IT WORKS:
This video has a 5 second delay not a 5 minute delay for the purposes of testing and shooting this video***

CODE:
I started writing this code being able to detect when the fridge is open. When the fridge is open the light inside the fridge is on. I used the photoresistor to detect when the light is on and would wait 5 minutes before blinking its LED and buzzing for a couple seconds. After it would begin moving the motor to close the fridge.
I ran into issues with the mechanism knowing when to turn off and reset. I had a few variables that would hold its value after the fridge being closed. Meaning it would remember it already waited 5 minutes. In order to fix this issue, I had to reset the variable LEDcount back to zero after every time the motor moved. Also when the photoresistor was receiving zero light value the same variable would reset to 0 so no residual counts would be held after a cycle has gone by.

#include <Stepper.h>

// Conncetions
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
int LED = 2;
int BUZZER = 3;

// Variables
int lightVal;
float checkTime = 0;
bool ledState = 0;
int ledCount = 0;
int buzzCount = 0;

// Stepper motor variables
const float STEPS_PER_REV = 32;
const float GEAR_REDUCTION = 64;
const float STEPS_PER_OUTPUT_REV = STEPS_PER_REV * GEAR_REDUCTION;
int StepsReq;
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  checkTime = millis();
}

void loop() {
  lightVal = analogRead(pResistor);
  Serial.println(lightVal);

  if (lightVal >= 200) {                              // if the fridge light is on then...
    if (millis() - checkTime >= 100) {                // blink led
      ledState = ! ledState;
      digitalWrite(LED, ledState);
      checkTime = millis();
      ledCount++;
    }
    if (ledCount >= 3000) {                             // if the LED has been on for 5 minutes (3000 counts = 300000ms = 5 min) then...
      buzzCount = 0;
      while (buzzCount < 500) {
        tone(BUZZER, 100, 100);                       // play tone
        delay(1);                                     // delay in between reads for stability
        buzzCount++;
      }

      delay(2000);                                    // delay before closing fride
   
      StepsReq = STEPS_PER_OUTPUT_REV / 2;            // Make the motor turn CW 180 degrees
      steppermotor.setSpeed(700);
      steppermotor.step(StepsReq);
      delay(4000);                                    // delay before resetting closing arm

      StepsReq = - STEPS_PER_OUTPUT_REV / 2;          // the minus makes it turn the other way
      steppermotor.setSpeed(700);
      steppermotor.step(StepsReq);
      delay(4000);                                    // delay before it resets and checks if the fridge remains open
   
      digitalWrite (LED, 0);                          // turn the led off so the light turns back on in the next loop (DOESNT WORK!!!)
    }
  }
  else {
    ledCount = 0;                                     // resetting variables incase fridge is closed before it counts up
    digitalWrite (LED, 0);
  }
}

ISSUES:
I was unable to get a material to attach to the end of my stepper motor to actually physically move the fridge closed. I, unfortunately, did not have the materials or tools to build the proper arm without it coming off or falling apart. Due to this, my project was unsuccessful in its final result. However,
I learned a lot in building and troubleshooting details within my code and physically wiring. I believe if I were to 3D print the proper arm, this project would be feasible.

Voltage Divider Experimentation

For this project, while setting up the circuit I ran into an issue of my LED not lighting up. After double checking the resistors and removing the LDR to learn that the LED was lighting up properly, (like Lily) I learned that the code itself needed to be changed to reflect my lighting situation. I also thought the value of the LDR being 25 seemed really low after realizing this.




After that was fixed, I changed the code so that the LED would blink in a loop unless the value of the LDR was less than 400, in which case it would turn off. Through this experimentation is how I was inspired to create an alarm that alerts me when my cat is on the counter.



//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
const int ledPin=9;       // Led pin at Arduino pin 9

//Variables
int value;          // Store value from photoresistor (0-1023)

void setup(){
 pinMode(ledPin, OUTPUT);  // Set lepPin - 9 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
}

void loop(){
  value = analogRead(pResistor);
 
  if (value >= 400){
    digitalWrite(ledPin, LOW);
    delay(100);
    digitalWrite(ledPin, HIGH);
    delay(100);
   
  }
  else{
    digitalWrite(ledPin, LOW); //Turn led off
  }

}

Basic Switch Experimentation

I referenced the Arduino tutorial on how to use a button switch to light up my LED.

I ran into an issue where the LED wouldn't turn on, but I knew that the button was working based on the status of the Arduino itself. After checking that the resistors were correct and moving wires around, I realized that one of my wires connecting the LED to ground had broken off in the Arduino itself. After replacing that wire, everything went much more smoothly.



Goodbye, yellow wire




Analog output experimentation

When I first started with this tutorial, I was excited because it seemed like it was just a refresher on the previous guide and tutorial on photoresistor usage and implementation. The circuit was easy to run through, but I got stuck on the whole “map” function deal.

A quick google search yielded this page from Arduino. I initially thought it gave the arduino another option in light output, allowing for more “in-between” values (instead of digitalWrite HIGH & digitalWrite LOW). 

However, THIS guide summarized the map function as an analog/digital conversion- “The map function is intended to change one range of values into another range of values and a common use is to read an analogue input (10 bits long, so values range from 0 to 1023) and change the output to a byte so the output would be from 0 to 255.”

So, from my understanding, the analog input (the photoresistor) would send the arduino values, but the code would convert the range of values into something with a greater span, so an easier transition could be had from a brighter to a dimmer output. While it wasn’t exactly what I was thinking it would do, this was cool because it gave me the option to make the light for my final sloooooowly turn on as the sun set, as opposed to a harsh on/off when the read values from the photoresistor fell or rose above a specified value. 

However, with that in mind, I knew this would be rough to implement because coding isn’t exactly my strong suit. I wasn’t able to get it to work as planned, but this was the code I thought would work best. 


While I wasn’t able to get any of the other assignment features complete (integrate a potentiometer or a second component in general), I’m happy I have a better understanding of the analog side of the arduino!