The Safe Room

Overview

This week, I designed a safe room using Arduino. The requirement was to create a circuit that will get information from at least one analog sensor and at least one digital sensor (switch) and use this information to control at least two LEDs, one in a digital fashion and the other in an analog fashion, in some creative way.

So, my creative idea involves a safe room that has a hidden secret controller. Every time someone enters the room, an intermediate warning is issued and the individual has 45 seconds to locate the hidden controller and adjust it to avoid triggering the security alarm. The location is highly confidential and only the top-tier managers know information. In case of an intruder, unless he is exceptionally smart enough to locate and adjust the controller, the person will be caught by this highly advanced security system.

Technical Implementation

One of the biggest challenges was to implement the idea on paper using the Arduino. After breaking into smaller steps, I figured it out. The digital switch was made using copper tape, one end on the board and one connected to the door. Once the door is open, it would send a HIGH to the connected digital input pin. Upon this, the always-on green light stops, and the yellow led starts glowing. Yellow led is controlled by the potentiometer (the controller). In the start it is set to a value of 1023 –  the bright yellow led and the individual has to switch off the led entirely using the potentiometer – to the value of 0. A timer is initiated as soon as the door is open and the individual has 45 seconds – 45000 milliseconds – to complete this. If the individual is successful, the green led turns back on. Otherwise, the red led is turned on digitally which signifies triggering the security alarm.

Here is the circuit:

Here’s the code:

//Global variable
const int knob = A0;
const int red_led = 13;
const int yellow_led = 11;
const int green_led = 8;
const int input = 7;
int starttime;

void setup() {
  //setting the modes
  pinMode(red_led, OUTPUT);
  pinMode(yellow_led, OUTPUT);
  pinMode(green_led, OUTPUT);
  pinMode(input, INPUT);

  //start timer
  starttime = millis();

  Serial.begin(9600);
  
}

void loop() {

  //when door is open
  if(digitalRead(input)==HIGH){
    //turn off the green led
    digitalWrite(green_led,LOW);
    //turn the yellow led on
    //control it anlogue way
    int knobValue = analogRead(knob);
    int mappedvalue = map(knobValue,0,1023,255,0);
    analogWrite(yellow_led,knobValue);

    //keep count of the time elapsed since the door is open
    Serial.print("millis elapsed : ");
    Serial.println(millis()-starttime);
    if((millis()-starttime) >= 45000){
      //if not controlled within time, trigger the alarm
      if(analogRead(knob) > 0){
        digitalWrite(red_led, HIGH);
      }
      //green switch back on successful completion
      else{
        digitalWrite(green_led, HIGH);
      }
    }
  }
  else{
    //if door closed, keep yellow light off and green on
    if(digitalRead(red_led) != HIGH){
      digitalWrite(yellow_led,LOW);
      digitalWrite(green_led,HIGH);
      Serial.println("You have 45 seconds to find and adjust the secret controller. Else alarm will go off!");
    }
  }

This is the video for when a known individual enters the room

This is in the case of an intruder

Week 9 Refill

Description:

I decided, after a struggle of thought, to make a simple water refill reminder. Drinking water is crucial and many people tend to try to forget. So I made this simple water that detects the weight of the bottle and knows how much water is left in the bottle/cup using a force sensor.

using the force sensor on its own did not give a range of weight enough for the water weight detector. so I decided to place the force sensor on a sponge.

I also used an on/off button that turns the circuit on when pressed and off when pressed once more

I also used 4 red lights that are more likely to capture someone’s attention and remind them to refill their water bottle when it’s empty.

and I used 4 blue led lights, more of the blue lights light up the more water there is in the bottle.

this is how my circuit looks:

the last set of wires that are neither connected to the button or the less is connected to the force sensor.

video:

the video shows how the circuit works:

Code:

const int outs[] = {3, 5, 6, 9,
                    10, 11, 12, 13
                   };
const int ins[] = {A2, A3};
int var, var2, b, r, maxx = 0;

long timer;
bool turnon = 0, curr, prev;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < sizeof(outs); i++) {
    pinMode(outs[i], OUTPUT);
  }
  Serial.begin(9600);
  for (int i = 0; i < sizeof(ins); i++) {
    pinMode(ins[i], INPUT);
  }
}

void loop() {
  if (curr != prev && curr == 1) {
    turnon = !turnon;
  }
  curr = digitalRead(ins[1]);
  timer = millis();
  var = analogRead(ins[0]);//0-250
  var = map(var, 0, 500, 0, 255);
  var2 = constrain(var, 250, 255);
  Serial.print(" on: ");
  Serial.print(turnon);
  Serial.print(" curr: ");
  Serial.print(curr);
  Serial.print(" prev: ");
  Serial.println(prev);


  if (turnon == 1) {
    on();
  }
  else {
    for (int i = 0; i < 8; i++) {
      analogWrite(outs[i], 0);
    }
  }
  //
  prev = digitalRead(ins[1]);

}

void redled(int n) {
  if (timer % (n * 4) < (n * 1)) {
    analogWrite(outs[4], var2);
    analogWrite(outs[5], 0);
    analogWrite(outs[6], 0);
    analogWrite(outs[7], 0);
  }
  else if (timer % (n * 4) < (n * 2)) {
    analogWrite(outs[4], 0);
    analogWrite(outs[5], var2);
    analogWrite(outs[6], 0);
    analogWrite(outs[7], 0);
  }
  else if (timer % (n * 4) < (n * 3)) {
    analogWrite(outs[4], 0);
    analogWrite(outs[5], 0);
    analogWrite(outs[6], var2);
    analogWrite(outs[7], 0);
  }
  else {
    analogWrite(outs[4], 0);
    analogWrite(outs[5], 0);
    analogWrite(outs[6], 0);
    analogWrite(outs[7], var2);
  }
}

void on() {

  if (var < 20) {
    b = -1;
    r = 4;
  }
  else if (var < 140) {
    b = 0;
    r = 0;
  }
  else if (var < 260) {
    b = 1;
    r = 0;
  }
  else if (var < 380) {
    b = 2;
    r = 0;
  }
  else {
    b = 3;
    r = 0;
  }

  // there is water
  if (r == 0) {
    for (int i = 0; i < b + 1; i++) {
      analogWrite(outs[i], var2);
    }
    for (int i = 7; i > b; i--) {
      analogWrite(outs[i], 0);
    }
  }
  else {
    for (int i = 0; i < 4; i++) {
      analogWrite(outs[i], 0);
    }
    redled(1000);

  }
}

 

Week 9: Rain Mayhem

For this week’s task, I used several LEDs, a photoresistor, and a button to simulate a rare event in Abu Dhabi: rain.

LEDs:

I used four colors of LEDs:

  1. blue to represent rain
  2. red and yellow to represent the social chaos caused by the rain;
  3. green to represent the calm state of no rain

The blue and green LEDs are connected individually in parallel, while the other six red and yellow LEDs are connected in pairs of series, which are then connected in parallel.

Photoresistor: 

For this task, I thought that using the values read directly from the photoresistor would not make sense because I’m only considering two states: rain and no rain. For this reason, I decided to set threshold variables. If the photoresistor value exceeds the variable, then it is considered not raining, and if the photoresistor value is lower than the threshold then it is considered raining. This can also be abstracted as the presence or lack of rain clouds.

Button:

The button in this task represents some control over the chaos (yellow and red LEDs) present while it’s raining. If it is raining and the button is pressed, then the chaos will stop. This can also be abstracted as some kind of enforcement of social distancing regulations which might be broken in the rain mayhem.

Limitations(?):

I had to change my threshold variable multiple times while working on the project because the lighting in my surroundings changed. I think for this project to work correctly, the threshold value has to be updated every time it is run.

Below are the visuals and code of the task:

View post on imgur.com

const int knob = A0;
const int calmLed = 3;
bool raining = 0;
int threshold = 85
0;
int activeBlue = 0;
int buttonPin = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 3; i <= 9; i++) {
    pinMode(i, OUTPUT);
  }
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonState = digitalRead(buttonPin);
  int knobVal = analogRead(knob);
  int mappedVal = map(knobVal, 420, 820, 0, 255);
  Serial.print("Knob Val: ");
  Serial.println(knobVal);
  Serial.print("Mapped Val: ");
  Serial.println(mappedVal);

  if (knobVal > threshold) {
    raining = 0;
  } else {
    raining = 1;
  }

  //control green light when not raining
  if (!raining) {
    //turn off all red, yello, and blue leds when not raining
    for (int i = 4; i <= 9; i++) {
      digitalWrite(i, LOW);
    }
    digitalWrite(3, HIGH);
    delay(500);
    digitalWrite(3, LOW);
    delay(500);
  } else {

    //control red and yellow leds when raining
    if (buttonState == LOW) {
      int x = 0;
      for (int i = 4; i <= 6; i++) {
        x = (int) random(0, 10);
        if (x > 7) {
          digitalWrite(i, LOW);
        } else {
          digitalWrite(i, HIGH);
        }
      }
    } else {
      for (int i = 4; i <= 6; i++) {
        digitalWrite(i, LOW);
      }
    }

    //control blue leds when raining
    int iterateBlue = (activeBlue % 3) + 7;
    digitalWrite(iterateBlue, HIGH);
    delay(200);
    digitalWrite(iterateBlue, LOW);
    activeBlue += 1;
  }
}

 

Little Robot (Analog) – Jade

EYELASH                                  EYELASH

 

Description / Mechanism

For this week’s assignment, I made a project that looks like a face of a robot, which has lights as its eyes, cables as nose and the distance sensor as mouth.

Normally, it looks straight at you, with the lights at the center of both sides on. If you turn the potentiometer left, the robot looks left, so the red lights are on. If you turn it right, the blue lights are on, showing that the robot is now looking right. If you move close to the robot’s face (mouth), it gets shy and closes its eyes, so the lights are all off.

If you press the button, the robot will enter into another state. Instead of looking at you, it starts processing data, so I have the delays between LEDs to show how data flows through. Then if you press again, it will go back to its original state.

 

Code

const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 5;
const int led5 = 6;
const int led6 = 7;
const int led7 = 8;
const int led8 = 9;
const int button = 10;
const int echo = 12;
const int trig = 13;
const int knob = A1;

int count = 0;
int ledState = LOW;
int buttonState1;
int buttonState2;
int de = 100;
bool off = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  
  pinMode(button, INPUT);
  pinMode(knob, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  buttonState1 = digitalRead(button); 
}

void loop() {
  buttonState2 =  digitalRead(button);
  if (buttonState2 != buttonState1) {
    if (buttonState2 == LOW) {
      count ++;
    }
  }
  buttonState1 = buttonState2;

  if (count%2 == 1) {
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  
  int knob_val = analogRead(knob);


  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(2);
  digitalWrite(trig, LOW); 
   
  int duration = pulseIn(echo, HIGH);
  int dist = duration/58.2;

  
  if (ledState == HIGH) {  
    digitalWrite(led1, HIGH);
    delay(de);
    digitalWrite(led2, HIGH);
    delay(de);    
    digitalWrite(led3, HIGH);
    delay(de);    
    digitalWrite(led4, HIGH);
    delay(de);
    digitalWrite(led5, HIGH);
    delay(de);
    digitalWrite(led6, HIGH);
    delay(de);    
    digitalWrite(led7, HIGH);
    delay(de);    
    digitalWrite(led8, HIGH);
    
    digitalWrite(led1, LOW);
    delay(de);
    digitalWrite(led2, LOW);
    delay(de);    
    digitalWrite(led3, LOW);
    delay(de);    
    digitalWrite(led4, LOW);
    delay(de);
    digitalWrite(led5, LOW);
    delay(de);
    digitalWrite(led6, LOW);
    delay(de);    
    digitalWrite(led7, LOW);
    delay(de);    
    digitalWrite(led8, LOW);
  } else {
    int del;
    if (dist <= 10) {
      off = true;
    } else {
      off = false;
    }
    if (off) {
      digitalWrite(led1, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(led5, LOW);
      digitalWrite(led8, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led6, LOW);
      digitalWrite(led7, LOW);
    } else {
    
    if (knob_val>300 and knob_val<630) {
      digitalWrite(led1, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(led5, LOW);
      digitalWrite(led8, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      digitalWrite(led6, HIGH);
      digitalWrite(led7, HIGH);
    }
    if (knob_val<=300) {
      digitalWrite(led1, HIGH);
      digitalWrite(led4, HIGH);
      digitalWrite(led5, LOW);
      digitalWrite(led8, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, HIGH);
      digitalWrite(led6, LOW);
      digitalWrite(led7, LOW);
    }
    if (knob_val>=630) {
      digitalWrite(led1, LOW);
      digitalWrite(led4, LOW);
      digitalWrite(led5, HIGH);
      digitalWrite(led8, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      digitalWrite(led6, HIGH);
      digitalWrite(led7, HIGH);
    }
    }
  }

  
}

 

Challenges

Initially, instead of the distance sensor, I was going to use the LDR sensor as the robot’s nose. However, the light in the dorm was not suitable for it and the output of the LDR value was quite random. I couldn’t figure out how to use the photo cell properly, so I changed it to the distance sensor, which is more reliable 🙂 Then I readjusted the position and found that the cables could be the nose!

The challenge I encountered was how to arrange the elements to create a correct circuit. Since I had many parts on the board, I needed to be careful not to overlap the positions for power/ground, so that they don’t touch each other to form another circuit. Thanks to Professor Aaron, I successfully connected the button and also the distance sensor.

The code part was not so challenging, but it took me some time to figure out the logics. The order of the conditions in the codes was difficult to sort out.

Overall, I think I have learned a lot from this project and have a better understanding of how the circuits work, especially how the sensors work.

Analog and Digital: Light up Dollhouse

This week’s assignment was to use 2 or more LEDs, one analog and one digital. My project for this assignment is a light-up mini dollhouse, I just used a dollhouse I borrowed from my sister and connected all my wires and LEDs to it!

First, I connected everything on the breadboard and made my code to make sure everything works properly. Once everything worked, I started removing the LEDs and the photoresistor (which is my analog component) one by one and attach them to my dollhouse. To attach them, I mainly used alligator clips, copper tape and washi tape.

The photoresistor makes the LED brighter as the sensor picks up dimmer light, in other words, as your finger gets closer to the sensor, the LED gets brighter. The other two LEDs are controlled by the button, and I connected them in parallel so that they can both be bright. Once I connected the LEDs and the photoresistor to the dollhouse and the button to the breadboard, it worked out! yay!

My circuit:

Demo Video:

 

Angry Dog!

Inspiration/idea:

This idea  came from me really missing my dog back home:(

If you are a dog owner you know they get really upset/angry when you stop petting them/being near to them. So I decided to use the ldr sensor (analog sensor) to imitate the motion of petting/being near to the dog. The dogs eyes start being lit up with red led light which portray the dogs anger, then when you get closer and touch the ldr sensor the dogs eyes stop glowing (he stops being mad). The touching of the sensor is like touching or petting the dog. Another option to make the dogs eyes stop glowing is giving him a treat which is portrayed using a yellow led light, when you click on the button  (digital sensor) you give the dog the treat so his eyes stop glowing and he isn’t upset anymore!

Process:

First of all I wanted to get the more difficult part of the process out of the way which is the ldr. I used the basic way to make an ldr, and followed the steps on our blog. Then I used constrain (map) in order to get the led lights to dim and brighten up all the way. Where I map the values from half the max until the max of the photocell readings into 0 -> 255 so that as they increase the brightness increases, and constrain that within 0 to 255. hence fulfilling the analog fashion needed for the project. This is the video of the ldr sensor working alone before continuing the rest of the project.

 

I  also extrapolated the power and the ground to the rest of the board using two jumper wires, in order to give myself extra space to work with. There I put the button digital switch to control the idea of the treat. The button is used to switch off the eyes as well and the yellow led with the treat on it, hence presenting the digital fashion needed for the project.

 

Finishing touches:

In order to show the story in a more creative way, I printed out images of the dog and the bone and made the led lights to be the eyes of the dog. And stuck the image of the bone to the yellow led light and the button itself to make it clear that pressing the button means giving the dog a bone.

Struggles:

First of all, I face a struggle in the beginning where my ldr sensor was not working at all, I talked to professor Aaron about it and it turns out the actual ldr was not working so I substituted it for another one and it worked, this took up a lot of my time. Another problem I faced was that I couldn’t get the leds to work with the ldr and button simultaneously, then I was able to figure it out by adding the analog write and digital write in an if statement as seen below:

int currentButtonState = digitalRead(buttonPin);
Serial.println(currentButtonState);

  // if the button is currently being prssed down, AND during the last frame is wasn't pressed down
  if (currentButtonState == HIGH && prevButtonState == LOW) {
      // flip the LED state

       ledState1 = !ledState1;
    ledState2 = !ledState2;
  }
      if (ledState1 == HIGH && ledState2 == HIGH) {           //if the bulbs were previously off then were turned on by button
    
      int ldrValue =analogRead(A0);                  //store reading from photocell
      int brightness = constrain( map(ldrValue, 596, 850, 0, 255), 0, 255);       //map the values from half the max until the max of the photocell readings into 0 -> 255 so that as they increase teh brightness increases, and constrain that within 0 to 255
      
Serial.println(ldrValue);

    analogWrite(ledRight, brightness);             //change output of sun bulb accordingly
    analogWrite(ledLeft, brightness);
    digitalWrite(ledPin, ledState2);//change output of moon bulb accordingly
    }
 else {
    digitalWrite(ledRight, ledState2); 
    digitalWrite(ledLeft, ledState2); //make bulbs's state = LOW, hence turned off
    digitalWrite(ledPin, ledState2);
    
    }

    prevButtonState = currentButtonState;

Also, another struggle I face was that the led light was not dimming and lighting up the way I wanted it to, in order to fix that I used constrain(map) and added the values I needed in order to make the led fully light up and dim down with the ldr.

Final result:

 

Code:

int buttonPin = 2;

int ledPin = 6;


int prevButtonState = LOW;


int brightness;
int ledLeft = 3;
int ledRight = 5;
int ledState1; 
int ledState2;

 
void setup() {


 pinMode(ledPin, OUTPUT);

pinMode(ledRight,OUTPUT);
pinMode(ledLeft,OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}

void loop() {



delay(20);





int currentButtonState = digitalRead(buttonPin);
Serial.println(currentButtonState);

  // if the button is currently being prssed down, AND during the last frame is wasn't pressed down
  if (currentButtonState == HIGH && prevButtonState == LOW) {
      // flip the LED state

       ledState1 = !ledState1;
    ledState2 = !ledState2;
  }
      if (ledState1 == HIGH && ledState2 == HIGH) {           
    
      int ldrValue =analogRead(A0);                  //store reading from photocell
      int brightness = constrain( map(ldrValue, 596, 850, 0, 255), 0, 255);       //map the values from half the max until the max of the photocell readings into 0 -> 255 so that as they increase teh brightness increases, and constrain that within 0 to 255
      
Serial.println(ldrValue);

    analogWrite(ledRight, brightness);             
    analogWrite(ledLeft, brightness);
    digitalWrite(ledPin, ledState2);
    }
 else {
    digitalWrite(ledRight, ledState2); 
    digitalWrite(ledLeft, ledState2); 
    digitalWrite(ledPin, ledState2);
    
    }

    prevButtonState = currentButtonState;    
 

  }

 

Cats Party : Lets go ! – Digital & Analog Sensors , Shamma’s Work

CATS PARTY

INTRODUCTION:

In week 10, I have continued working with Arduino hardware and got an in-depth understanding of both the analog and digital inputs. For my project, I had to use at least one analog and one digital sensor to control at 10 LEDs in some creative manner. So, I decided to use the proximity sensor as my digital sensor and the potentiometer as my analog sensor to control 5 LED’s each in a respective manner that creates light mashups.

INSPIRATION:

The inspiration behind the usage of different LED's is their colorful and attractive appearance which in my case would give a disco themed party. They light up the room in a distinctive manner providing the crowd with a lighthearted experience. Generally speaking, colors often provide a joyful emotion, and an ideal manner to execute this is was by using LED's as an inspiration. In this project, the aim is to mimic those lights on a smaller scale using arduino. The usage of the cat theme came as an inspiration from my sister's love of cats as well as the well-loved campus cats. It provides a fun twist to the project result and appearance, as well as personalizes it in a creative and unique manner.

IdEA OVERVIEW IMPLEMEnTAtION:

Once I grasped the notion of working with both the analog and digital sensor, I decided to make a unique led pattern with it. This implies that I can compare them with a progressive volume or fuel level indicator using the LED’s. To start off, I used 5 LEDs each for analog and digital sensor (total 10 LED’s). With the digital sensor, by default all the LEDs are off and as I place my hand on the proximity sensor, the LEDs switch on one by one till all 5 are lit up. So basically, I can swipe my hand on the sensor and turn on 1 LED, 2 LED or 3 LED as per the number of swipes on the sensor or time duration for which the hands are on the sensor. Later, when all the LEDs are on, any further swipe over the proximity sensor will result in resetting all the LEDs and they will then again start from the all off state. On the other hand, with the analog sensor, again all LEDs are off and as I turn my potentiometer the LEDs start switching on. From all off at one end, the LEDs light up one by one till all are on to the other end. In other words, this can be compared to a volume control knob on older TV’s or music players on vehicles where one end is mute and other end is max volume.

CHALLENGES & PROBLEMS:

In this week, I had decided to use the proximity sensor as a digital sensor. For that, I used one which I have ordered via amazon. Then, although working with the analog sensor was quite straight forward, I had to divide the output of the analog sensor into different ranges as per my concept. This have taken some time for me to comprehend it and the conversion of the ADC value from 0V - 5V to 0 – 1023 has required some effort and additional readings. One thing that aided my process was watching a few YouTube videos for the ADC value conversion concept.

PROCEDURE:

First, I used the Arduino board to power the + and – rails of the breadboard so that the entire circuit can use + and – from these rails whenever required in future connections. In the following image, a jumper wire goes into the +ve rail and the other goes into the -ve rail.

Having already connected the LED in the last assignment, I have quickly completed the following circuit and the anode of LED (longer leg) was connected to Arduino D7 and the resistor was connected to the ground rail as shown below.


I tested the simple LED circuit by uploading the following code and it have worked.
// C++ code

//it runs only once 

void setup()
{
  //pinmode is used to configure pin number 7 as an output 
  pinMode(7, OUTPUT);
}

// runs forever 
void loop()
{
  //writing a logic high which equals 5 volt or 1 in binary 
  //jumper wire connected to anode, LED switch on
  digitalWrite(7, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  
  //writing a logic low which equals  0 in binary 
  //jumper wire connected to anode, LED switch off
  digitalWrite(7, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}
The video shown below was the output !

 

Next, I wanted to test the digital sensor with a single LED, so I have connected the sensor on breadboard and powered it with the onboard power rails. I connected the output of sensor to digital pin 7 which was configured as an input. Moreover , I have utilized the following code and the output was as shown in video below.
// C++ code


void setup()
{
  //pinmode is used to configure pin number 7 as an output 
  pinMode(7, OUTPUT);
  //configuring 6 as input to read data from external switch/ sensor 
  pinMode(6,INPUT);
}

void loop()
{
  //LED switch off
  
  if(digitalRead(6)==LOW)
  {
    // LED switch on
    digitalWrite(7, HIGH);
  }
  else
  {
    // LED switch off
    digitalWrite(7, LOW);
  }

Then, I moved on to the connection of the digital proximity sensor with 5 LED’s. I removed the earlier LED and disconnected the sensor output pin. Now, only the power rails were connected to the respective pins as shown.

Next, I started to connect a single LED. All the other LED’s were kept ready to be placed on the breadboard.

Then, all the 5 LEDs were connected with the resistors. I kept the colours different to get a nice colourful effect.

Moving forward, I connected all the LEDs and the digital sensor with Arduino and wrote a code to test all LEDs with my sensor. This way I was able to test all LEDs individually. The output video is shown. 
// Shamma's code 

// LED connected to 3,4,5,6,7 in arduino board 
int gled = 3, b1led = 4, b2led = 5, yled = 6, rled = 7;

//sensor connected to pin 2 , the proximity sensor 
int sensor = 2;

// initialize count variable to zero
int count = 0;

void setup()
{
  //All leds are being configured as an output and sensor is configured as an input because we read from it
  pinMode(gled, OUTPUT);pinMode(b1led, OUTPUT);
  pinMode(b2led, OUTPUT);pinMode(yled, OUTPUT);
  pinMode(rled, OUTPUT);
  pinMode(sensor,INPUT);
  
  //by default, I am switching all the leds off, 
  //even if I remove those, code will work as my count is zero 
  digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
  digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
  digitalWrite(rled,LOW);
}


void loop()
//reading if proximity sensor is low, then I will increment count variable by one 

{
  //Once I place the hand, the count variable will switch from zero to 1,2,3,4,5,6
  //One is off, while next is on
  if(digitalRead(sensor)==LOW)
  {
    count = count + 1;
    
    //I use 5 LEDS, so I put 6 to restart the count 
    if(count == 6)
    {
      count = 0;
    }
  }

// IF CONDITION for each LED on the mother board 
  if(count == 0)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 1)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 2)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 3)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,HIGH);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 4)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,HIGH);
    digitalWrite(rled,LOW);
  }
  else if(count == 5)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,HIGH);
  }
  // once I place my hand, this delay will stop down the program 
  delay(500);
}

After successfully completing the digital sensor, I worked on analog sensor and chose potentiometer. So, another 5 LEDs were added which would be controlled via analog sensor.

Just to be double sure that all LEDs were connected properly, I wrote a small test LEDs code. This ensured that the wiring was properly done with no errors. The output was as following:
//
int gled = 3, b1led = 4, b2led = 5, yled = 6, rled = 7;
int sensor = 2;
int count = 0;

void setup()
{
  pinMode(gled, OUTPUT);pinMode(b1led, OUTPUT);
  pinMode(b2led, OUTPUT);pinMode(yled, OUTPUT);
  pinMode(rled, OUTPUT);
  pinMode(sensor,INPUT);

  digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
  digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
  digitalWrite(rled,LOW);
}

void loop()
{
  if(digitalRead(sensor)==LOW)
  {
    count = count + 1;
    if(count == 6)
    {
      count = 0;
    }
  }

// only difference is that when one lights up, the other follows without switching off 
  if(count == 0)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 1)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 2)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 3)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count == 4)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
    digitalWrite(rled,LOW);
  }
  else if(count == 5)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
    digitalWrite(rled,HIGH);
  }
  delay(500);
}

Finally, I added my analog sensor, the potentiometer in the circuit. It had 3 terminals out of which the corner ones were power rails and the middle one was the analog output which was connected to the analog input 0.

I wrote another small code to test only the analog sensor which turned the 5 LEDs dedicated for analog sensor one by one like shown.
// C++ code
//
int gled = 3, b1led = 4, b2led = 5, yled = 6, rled = 7;
//added the other 5 leds which will be used for my analog 
int g2led = 8, b3led = 9, b4led = 10, y2led = 11, r2led = 12;
int digital_sensor = 2;
//declared the analog sensor 
int analog_sensor = A0;
int count = 0;

void setup()
// all ten leds are configured as outputs 
{
  pinMode(gled, OUTPUT);pinMode(b1led, OUTPUT);
  pinMode(b2led, OUTPUT);pinMode(yled, OUTPUT);
  pinMode(rled, OUTPUT);
  
  pinMode(digital_sensor,INPUT);

  pinMode(g2led, OUTPUT);pinMode(b3led, OUTPUT);  
  pinMode(b4led, OUTPUT);pinMode(y2led, OUTPUT);
  pinMode(r2led, OUTPUT);
  
 // All 10 leds will be turned off

  digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
  digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
  digitalWrite(rled,LOW);
  
  digitalWrite(g2led,LOW);digitalWrite(b3led,LOW);
  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);
  digitalWrite(r2led,LOW);
}

void loop()
{
  // Analog Sensor Code
  // Reading the value from the analog sensor and storing it in the variable count 
  int count = analogRead(analog_sensor);

// I divided 1024 by 5 as I have 5 leds and took the range from one switch to another 
  if(count<10)
  {
    digitalWrite(g2led,LOW);digitalWrite(b3led,LOW);    digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW); 
  }
  // 
  else if(count>=10 && count<210)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,LOW);  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);
  }
  else if(count>=210 && count<410)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);
  }
  else if(count>=410 && count<610)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);  
  }
  else if(count>=610 && count<810)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,HIGH);    digitalWrite(r2led,LOW);
  }
  else if(count>810)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,HIGH);    digitalWrite(r2led,HIGH);
  }

//  // Digital Sensor Code
//  
//  if(digitalRead(digital_sensor)==LOW)
//  {
//    count = count + 1;
//    if(count == 6)
//    {
//      count = 0;
//    }
//  }
//
//  if(count == 0)
//  {
//    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
//    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
//    digitalWrite(rled,LOW);
//  }
//  else if(count == 1)
//  {
//    digitalWrite(gled,HIGH);digitalWrite(b1led,LOW);
//    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
//    digitalWrite(rled,LOW);
//  }
//  else if(count == 2)
//  {
//    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
//    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
//    digitalWrite(rled,LOW);
//  }
//  else if(count == 3)
//  {
//    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
//    digitalWrite(b2led,HIGH);digitalWrite(yled,LOW);
//    digitalWrite(rled,LOW);
//  }
//  else if(count == 4)
//  {
//    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
//    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
//    digitalWrite(rled,LOW);
//  }
//  else if(count == 5)
//  {
//    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
//    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
//    digitalWrite(rled,HIGH);
//  }
  delay(500);
}

At last, I integrated the digital sensor and analog sensor code in one final code and I could control all 10 LEDs using different types of sensors.
// C++ code
// This is a combination of code 2 and 3 , both the proximity sensor and the analog which is the potentiometer 
//Digital and analog sensor 

int gled = 3, b1led = 4, b2led = 5, yled = 6, rled = 7;
int g2led = 8, b3led = 9, b4led = 10, y2led = 11, r2led = 12;
int digital_sensor = 2;
int analog_sensor = A0;
int count = 0,count2 = 0;

void setup()
{
  pinMode(gled, OUTPUT);pinMode(b1led, OUTPUT);  pinMode(b2led, OUTPUT);pinMode(yled, OUTPUT);  pinMode(rled, OUTPUT);
  pinMode(digital_sensor,INPUT_PULLUP);
  pinMode(g2led, OUTPUT);pinMode(b3led, OUTPUT);    pinMode(b4led, OUTPUT);pinMode(y2led, OUTPUT);  pinMode(r2led, OUTPUT);
  digitalWrite(gled,LOW);digitalWrite(b1led,LOW);  digitalWrite(b2led,LOW);digitalWrite(yled,LOW);  digitalWrite(rled,LOW);
  digitalWrite(g2led,LOW);digitalWrite(b3led,LOW);  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);  digitalWrite(r2led,LOW);
}

void loop()
{
  // Analog Sensor Code
  int count = analogRead(analog_sensor);

  if(count<10)
  {
    digitalWrite(g2led,LOW);digitalWrite(b3led,LOW);    digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW); 
  }
  else if(count>=10 && count<210)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,LOW);  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);
  }
  else if(count>=210 && count<410)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,LOW);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);
  }
  else if(count>=410 && count<610)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,LOW);    digitalWrite(r2led,LOW);  
  }
  else if(count>=610 && count<810)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,HIGH);    digitalWrite(r2led,LOW);
  }
  else if(count>810)
  {
    digitalWrite(g2led,HIGH);digitalWrite(b3led,HIGH);  digitalWrite(b4led,HIGH);digitalWrite(y2led,HIGH);    digitalWrite(r2led,HIGH);
  }

  // Digital Sensor Code
  
  if(digitalRead(digital_sensor)==LOW)
  {
    count2 = count2 + 1;
    if(count2 == 6)
    {
      count2 = 0;
    }
  }

  if(count2 == 0)
  {
    digitalWrite(gled,LOW);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count2 == 1)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,LOW);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count2 == 2)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,LOW);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count2 == 3)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,LOW);
    digitalWrite(rled,LOW);
  }
  else if(count2 == 4)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
    digitalWrite(rled,LOW);
  }
  else if(count2 == 5)
  {
    digitalWrite(gled,HIGH);digitalWrite(b1led,HIGH);
    digitalWrite(b2led,HIGH);digitalWrite(yled,HIGH);
    digitalWrite(rled,HIGH);
  }
  delay(500);
}
Just to add a little spice, I decided to cut down a cartoon box I found that is recycled and turn it into a space for cats to party. I did that by printing a couple of images, using a card board to give definition in terms of the party stage by adding a couple of card board together. Then, I used cotton bods as they will act as the cat paws which will trigger the proximity sensor when being moved on top of it. Later, I did not have any markers to draw the entrance of the party, so I used some makeup powders and eyeliner that I had, so excuse me for the mess. I also added a small radio I had on top of the box so I can run inside it a little fun cat song. At last, I placed the Arduino inside the cat space party to make the experience more real.

CONCLUSION:

The experience with this assignment has opened my eyes to the use of different sensors and how to interface them with Arduino. By being able to control LEDs using analog and digital sensors, although it’s a simple thing,  it made me more enthusiastic about the more interesting applications that is to come.

Week 8: Hands Free Switch

Introduction:

I am obsessed with privacy. My dorm is my safe haven from the pressure of school work and socializing, so I am obviously annoyed when that peace is disturbed. It is from this that I had the idea of a Do-Not-Disturb button. An automatic version of the signs you hang on hotel room doors, to signal to roommates and visitors that I am not to be disturbed.

Procedure:

My initial idea was to have a simple circuit like we built last week. When I close my door, the circuit is closed, and if I pushed a button, a red LED would light up, and maintain state after the button was pushed. If I pushed it again, the LED would turn off, signaling that I am available. If my door was open, then the light would be off, as the circuit was broken.

However, in order to make this more hands-free, the Professor suggested that I use some other signal besides a push button to indicate that I was not to be disturbed. He suggested that I put conductive material on my pillow and the bed below the pillow, so when I was asleep the circuit would close and the light would come on. The circuit itself is very simple. I made a diagram of it in tinkerCAD, and attached a screenshot here.

The small breadboard off to the side represents the conductive material attached to the pillow. (I don’t know how to put conductive material in tinkerCAD). In this diagram, the circuit is closed. The code for this is also quite simple, with the Arduino reading the state of the “pillowPin” and writing that to the LED pin.

The procedure is relatively simple. When the conductive material on the bed and the pillow comes into contact, the circuit is closed.

When the circuit is closed, the Arduino will read the current state of the LED pin and change it. (The LED is always off to start). When the circuit is broken, the LED will turn off.

Demonstration:

https://youtu.be/ujdsTZ9_EqY

const int ledPin = 3;
const int pillowPin = 2;

bool sleepWake = LOW;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(pillowPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  bool currentState = digitalRead(pillowPin);
  if(currentState == HIGH){
    sleepWake = !sleepWake;
  }

}

Switch

Introduction

Our task in this report is to create an unusual switch that doesn’t require the use of our hands. I will use the arduino prototyping board for the digital input and output for the interaction.

The hands-free switch is attached to a digital input pin on the Arduino, and then, with code, I took the input from that and turn on an led (digital output). The arduino digital I/O is a requirement for this exercise. That means I need a pull-down resistor with our hands-free switch in order for Aurdino to read the input.

As a hand free switch we used two options:

-option 1 : LDR light dependent resistor that turns the light on and off depending on the light in the room.

-option 2: Ultrasonic sensor that detects distance and turns the light off and on depending on an obstacle in front of the sensor.

Challenge Faced

As working on the Arduino board for the first time, the challenges I faced were understanding the pin diagram and how to define each pin.

After looking at the breakout pinout of the Arduino uno, I was able to identify which pins I will use for our input and output.

I decided to use the pins:

  • 13 digital output for LED
  • 6 input for LDR
  • For ultrasonic sensor:

Procedure

First step was to identify the output and input.

As output we used the LED light, and we connected it using a pull up resistor as per the following circuit:

Code in the setup: pinMode(13, OUTPUT);

Code to turn on the led: digitalWrite(13, HIGH);

Code to turn off the led: digitalWrite(13, LOW);

 

Second step is to connect the switch:

Option 1 LDR:

We connected an LDR as the following circuit:

The pin of LDR went to analog pin on Arduino A5 through a pull down resistor.

If we cover the LDR the LED connected to pin 13will turn on.

The code used is:

Setup: Serial.begin(9600);

Loop: int ldrStatus = analogRead(ldrPin);

Option 2 : Ultrasonic sensor

Second option is using an ultrasonic sensor as the following:

We connected the ultrasonic sensor by the circuit:

So when the distance is low the green light will be on. Green light is connected to pin 12 in the Arduino.

Demonstration :

Conclusion

As we completed this project we realized how diverse is the Arduino and how it could be used in many different application.

long duration; 
int distance;
int echoPin=2 ;
int trigPin=3;

void setup() {

  Serial.begin(9600);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  pinMode(13, OUTPUT); 
  pinMode(12, OUTPUT);
  pinMode(A0, INPUT); 

}

void loop()
{
//LDR code
  int ldrStatus = analogRead(A0); 
  Serial.println(A0);
  if (ldrStatus <= 300) digitalWrite(13, HIGH);
  else digitalWrite(13, LOW);

  //ultrasonic code
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; 
  Serial.println("Distance: ");
  Serial.print(distance);
  if (distance <= 10) digitalWrite(12, HIGH);
  else digitalWrite(12, LOW);
  
}

 

Week 8: Unusual Switch

For this week’s task, I created a switch which can be activated by touching a piece of wire taped on top of my foot to an aluminum foil sheet taped on the bottom side of a chair, emulating a self-defense move. When the switch is activated, a servo starts moving two aluminum foil pieces back and forth towards an aluminum foil figure , emulating slapping a stickman.

Process:

I started with designing the figure and securely taping everything to the cardboard. That was the most difficult part. After figuring out that part, arranging the circuit was straightforward. Below are a few pictures detailing the process and a video demo of the switch:

Unusual Switch

My Arduino code is very short. It controls the motion of the servo, allowing for a short pause in between.

#include <Servo.h>

Servo servo;
const int servoPin = 2;
int currmillis = 0;
bool dir = 0;

void setup() {
  servo.attach(servoPin);
}

void loop() {

  if ((int)millis() - currmillis >= 1000) {
    currmillis = millis();
    dir = !dir;
  }

  if (dir) {
    servo.write(180);
  } else {
    servo.write(0);
  }

}