index2018-10-01

Week 4 ~ Make something!

This week I failed several times. I wanted to make a device in which every component was somehow affected by every other component - I couldn’t stop thinking about how I wanted to make something like Pee Wee Herman’s Breakfast Machine. I built nothing like this but I will eventually!

The tools I used this week were the potentiometer, the servo motor, and an LED.

At first I tried to use a photo resistor but found it to be a little restrictive. In order for the photo resistor to be an effective component within my device it needed obvious differences in surrounding light and this proved to be a little difficult without manually shining a flashlight or something like that.

With my next attempt I decided on a system. The potentiometer would control the movement of the servo motor. An object sitting on the servo motor would posses one end of a switch. Another object, posessing the other end of the switch, would be waiting until the servo turned 180 degrees and completed the switch by reaching where it was sitting. The completed switch would turn the LED on. The servo motor, by way of the potentiometer, would then go backwards 180 degrees, turning the switch off.

First I made the switch, pictured below. The one thing that has almost become second nature is connecting my breadboard to the Arduino and then running power around the entire board. The things to check for are: a wire running from the 5V port of the arduino to the power bus of the breadboard, a wire running from the ground port of the arduino to the ground bus of the breadboard, a cable running from one power bus of the breadboard to the another power bus on the other side, and finally a cable running from one ground bus of the breadboard to another ground bus on the other side.

To make the actual switch I used digital pin 2. I am using digital input because the switch is going to send an off or on signal in to the arduino. To make the switch I did the following: ran a wire from the arduino digital input 2 to 20B on the breadboard, next to 20B on 20C is one end of a wire for one end of my switch. Next to that wire on 20D is one end of a 220OHM resistor which is connected to a wire in 20I which is finally connected to ground.

To create the other end of my switch I ran a wire from the power bus to 22B and placed the wire for the other side of my switch next to it in 22C. I tested the switch by plugging in my arduino and running Serial.println(digitalRead(2)). When I touched the two switch wires to together the Serial printed the number 1, when I took them apart the Serial printed 0.

make-a-switch

Next I wired up the servo motor. Since I want to control the motor through the potentiometer it will be using a digital output pin because I will be writing code that will tell the Servo motor to start and move at a particular time in a particular direction. The white wire of my servo is placed directly into digital pin 3. The red wire is connected to the power bus and the black wire is connected to the ground bus. I tested the servo motor by importing the servo library and writing myservo.attach(3); inside of my setup() code. At this point the motor doesn’t actually do anything but it makes a little buzzing sound so I know that it is receiving power.

wire-up-servo-motor

Theeeen I added the potentiometer. Since the potentiometer will be adjusted using my hand and I will want to read the position of it in order to update the position of my motor I needed to use an analog pin. In this case I used analog pin 0. A wire goes directly from A0 to the middle leg of the potentiometer. The back two legs are connected to power and ground. To test pontentiometer I used Serial.println(analogRead(A0));

add-potentiometer

Fiiiiinally I wired up the LED. I used digital pin 4, connected the wire to a 220ohm resistor and into the anode of the LED. The cathode is connected to ground.

wire-up-led

To test the LED I wrote an if else statement against the condition of my switch. If the switch is on, turn the LED on, if its off, turn the LED off.

Here is a video of the project in action:

Here is the code:

#include <Servo.h>

Servo myservo;

void setup() {
  Serial.begin(9600);
  // motor
  myservo.attach(3);  
  // switch
  pinMode(2, OUTPUT);
  // LED
  pinMode(4, OUTPUT);
}

void loop() {
  int potValue = analogRead(A0);
  // checking value of switch
  Serial.println(digitalRead(2));
  // maps values of pot and converts them to degrees up to 180
  potValue = map(potValue, 0, 1023, 0, 180); 
  // move motor 180 deg
  myservo.write(potValue); 
  delay(5);
  // if switch is on
  if (digitalRead(2) == 1) {
     // turn red LED on
    digitalWrite(4, HIGH);
  } else {
    // turn red LED off
    digitalWrite(4, LOW);
  }
}