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(3, 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

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);
  }

}

 

“Please Don’t Laugh When I Am Sleeping”

INSPIRATION

My suitemates would bring friends over to “study” at 3 AM. They would laugh, play music, and shout all the way to 5 AM when my roommate and I had 9 AM classes the next day. Therefore, I am creating an indicator that there is someone asleep in the room and the wall is paper-thin, even though they probably know that but chose to ignore the fact. Anyways, if this subtle hint doesn’t work, I will do something else.

IDEA

When the two aluminum foil touch, the boolean turns true, and green turns off and red turns on. On the other hand, when they part, the boolean turns false, and green turns on and red turns off.

BREADBOARD & ARDUINO BOARD

Photos

VIDEO DEMO

CODES

void setup() {
  // put your setup code here, to run once:
  pinMode(4, INPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (bool digitalRead(4) == HIGH) {   //pressed, if the two aluminum foils touch
    digitalWrite(3, HIGH);        //turns on red LED
    digitalWrite(2, LOW);         //turns off green LED
  }
  else {                          //released, if the two aluminum foils part
    digitalWrite (3, LOW);        //turns off red LED
    digitalWrite(2, HIGH);        //turns on green LED
  };
}

CHALLENGES

  1. Maybe it’s some problems with my codes. The green light turns on immediately when the two aluminum foils touch. However, it takes 5 to 7 seconds for the boolean flips, and then the red light turns on.
  2. Sometimes when the foil connected to the resistor and the ground gets in contact with something conductive, green turns off and red turns on, signifying that the boolean flips. This may be a problem with the circuit, but I am still figuring it out.

Unusual Switch

This week, our assignment was to make an unusual switch that works without using our hands. For my project, I decided to make 2 switches that will turn on when the copper strip attached on the ground and the strip attached to your shoe touch (these are connected to the circuit via long wires). When they touch, the LED lights on either side will light up! So if you use your left foot, the yellow LED on the left will light up. If you use your right, the blue one on the right will light up.

I tried to make my circuit as neat as possible, so I organized my wires by color. Red ones are for power, black for GND, green for the left side, blue for the right. As for the longer wires, the ones connected to the strip on the ground are yellow, and the ones connected to your shoe are white. 

Here’s the code:

Basically, if the switch is on, the led will light up. If its not on (the strip in the floor and the shoe aren’t touching) the LED will turn off.

//R = right side, L= left side.

const int ledpinR = 2;
const int ledpinL = 3;

const int switchpinR = 4;
const int switchpinL = 5;

bool switchreadR;
bool switchreadL;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledpinR, OUTPUT);
  pinMode(ledpinL, OUTPUT);

  pinMode(switchpinR, INPUT);
  pinMode(switchpinL, INPUT);

}

void loop() {
  // right LED code:
  
  switchreadR = digitalRead(switchpinR);

  if (switchreadR == true) {

    digitalWrite(ledpinR , HIGH );
  } else {
    digitalWrite(ledpinR , LOW);
  }

// left LED code:
  switchreadL = digitalRead(switchpinL);

  if (switchreadL == true) {

    digitalWrite(ledpinL , HIGH );
  } else {
    digitalWrite(ledpinL , LOW);
  }
}

Images:

The circuit
The bottom of the shoe

 

Video:

https://youtube.com/shorts/rRdpnEHXotk?feature=share

Buzz Off: An Unusual Switch

In this production assignment, we had to create an unusual switch that does not require the use of hands and involves digital input and output for interaction. For this, I created a buzz game, where touching the course using the loop would close the switch. This, in turn, would stop the green LED and the red LED will start blinking.

This unusual game requires you to move the loop across the course using wrists. Sounds interesting but it is pretty difficult. I spent so much time simplifying and making the course yet interesting. I am yet to find someone that could complete this with the loop between wrists and without buzzing the circuit.

Here’s the complete circuit :

 

 

 

 

 

 

 

 

 

Let’s play:

P.S Sorry for the bad video editing/ timing

The code is pretty simple and short. I have added a variable that keeps track of how many frames the switch has been closed –  a measure of how bad you are at this game.

//global variables
int ledPingreen = 2;
int ledPinred = 4;
int input_ledPin = 7;
int touches = 0;
unsigned long timer = 0;
int timerAmount = 50;
bool flip = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(input_ledPin, INPUT);
  pinMode(ledPinred, OUTPUT);
  pinMode(ledPingreen, OUTPUT);
  
  //initiate timer
  timer = millis();

  //keep track of touches and monitor it
  touches = 0;
  Serial.begin(9600);
  
}

void loop() {
  //monitoring touches
  Serial.print("Number of touches until now : ");
  Serial.println(touches);

  //if the switch is closed
   if(digitalRead(input_ledPin)==HIGH){
    //increment number of touches
    touches += 1;
    //blink the red LED
    if(millis()>= timer){
      flip = !flip;
      digitalWrite(ledPinred, flip);
      timer = millis()+timerAmount;
    }
    //turn of green LED
    digitalWrite(ledPingreen, LOW);
   }

   //if open
   else{
    //turn green LED on and red off
    digitalWrite(ledPinred, LOW);
    digitalWrite(ledPingreen, HIGH);
   }
}

Week 8 The Switch

Description:

I have always been the worst when it came to forgetting different things at different places. Forgetting my card at the most random places was getting me locked out of my room more than I would have liked. So I decided to make a switch that reminds you to put your card back in its place sometime out.

I started by placing a switch in the circuit and making it work using a normal push-button switch. once I finished the circuit I replaced one end of the switch with a wire connected to the card and the other end with a wire connected to a cardholder that is to be stuck to a phone.

I connected the wires to copper tape that I stuck on contacting sides of the card and the cardholder so that when the card is in place, the switch is closed.

I used a yellow led light for the normal time when the card is placed in its location. when you remove the card from the cardholder, a timer starts, when the time span in the code ends, the red light starts to blink to remind you to put the card back in.

IMAGES:

Breadboard Circuit:

Whole Circuit:

Card Connection

Video:

CODE:

long mytime=500, timer = 0;
const int outs[] = {2, 4};
const int ins = A2;
int curr = 0, prev=0;
bool flip=false;
long blinkk=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (int i = 0; i < sizeof(outs); i++) {
    pinMode(outs[i], OUTPUT);
  }
  pinMode(ins, INPUT);
}

void loop() {
  curr = digitalRead(ins);// <500, <900, >900
  if (curr == 0) {
    Serial.print(timer);
    Serial.print(" ");
    Serial.println(mytime);
    if (mytime == timer) {
      if(blinkk<=millis()){
        flip=!flip;
        blinkk+=500;
      }
      digitalWrite(outs[0], flip);
      digitalWrite(outs[1], LOW);
    }
    else{
      timer+=1;
      digitalWrite(outs[0], LOW);
      digitalWrite(outs[1], LOW);
    }
  }
  else {
    {
      timer=0;
      digitalWrite(outs[1], HIGH);
      digitalWrite(outs[0], LOW);
      blinkk=millis();
    }
  }
  prev = digitalRead(ins);
}

 

 

 

Handless Switch : Sleep No More!

Description

Create an unusual switch that doesn’t require the use of your hands. Use Arduino digital input and output for the interaction.

Inspiration

Imagine you could identify all the listeners who sleep during your speaking sessions even if the listener sleeping is at the backbench. All you need is a LED that is positioned at a visible location and turns on immediately, a listener puts his or her head on the table.

Process

I started off by creating a simple circuit with an LED. I added a digital input pin on the Arduino and then, with code, I take the input and turn on the LED as a digital output. By bringing together the power pinned down by the resistor and the buttonPin, the LED turns on and when separated goes off. In order to bring the two together, I attached one to the forehead and the other to the table.

Final Work

Challenges

I wanted to attach one of the cables to my neck and the other one under my chin but it never worked so I decided to go with the idea above.

Code
const int ledPin = 2;
const int buttonPin = 3;


bool onOff = false;
bool prevButtonState = LOW;

void setup() {
   pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT);

}

void loop() {
  
  bool currentState = digitalRead(buttonPin);
  
 if(currentState == HIGH){
      digitalWrite(ledPin, true);
    }

  else{
    
     digitalWrite(ledPin, false);
    }

     
   

}

 

Bell the Cat – A Carnival Switch Ring Toss

For this week’s assignment, we had to make a switch using Arduino digital input and output for interaction. I started off with the idea of a switch where when you put a hat on a cat figurine, the light would switch on. But I scrapped that idea since it would involve using my hands.

So, I decided to adapt my idea to lower the degree of interaction hands may have on the switch. I ended up with a carnival-like game Ring toss, where you win if you throw a ring and it goes on a bottle. In my version, the bottle is a cat, and I’m throwing a ring of aluminum foil. When the ring falls on the cat, the green light glows and you win.

For this, I had two jumper wires from the breadboard attached to aluminum foil and this was pasted to the cat’s ears and body, and the ring was made of aluminum foil as well. So when the ring fell on the cat, the aluminum foil would complete the circuit which would go as Arduino digital input. 

I had some trouble understanding how to incorporate Arduino digital input so after playing around with the push button switch schematic, I realized what the assignment meant. 

Here’s the video showing the switch and there’s pictures of the whole setup below it.

The Setup
The Setup – Cat