Monday, March 21, 2016

Question 18 (Alternating LEDs)

bool other;
void setup()
{
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 10; i++) // this loop blinks one LED very fast
  {
    digitalWrite(12, HIGH);
    delay(50);
    digitalWrite(12, LOW);
    delay(50);
  }

  if (other == true)  // this LED only goes through a cycle once every 20 cycles of the other LED
  {
    other = false;
    digitalWrite(13, HIGH);
  }
  else
  {
    other = true;
    digitalWrite(13, LOW);
  }
}

Questions 15 - 17 (Two blinking LEDs)

void setup()
{
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop()
{

  digitalWrite(13, HIGH);   // Turn on pin 13 LED
  digitalWrite(12, LOW);    // Turn off pin 12 LED
 
  delay(1000);              // Wait for one second
 
  digitalWrite(13, LOW);    // Turn off pin 13 LED
  digitalWrite(12, HIGH);   // Turn on pin 12 LED
 
  delay(1000);              // Wait for one second
}

Question 14 - Blink (LED mostly on)

/*
SparkFun Inventor's Kit
Example sketch 01

BLINKING A LED

  Turn an LED on for one second, off for one second,
  and repeat forever.

Hardware connections:

  Most Arduinos already have an LED and resistor connected to
  pin 13, so you may not need any additional circuitry.

  But if you'd like to connect a second LED to pin 13, or use
  a different pin, follow these steps:

    Connect the positive side of your LED (longer leg) to Arduino
    digital pin 13 (or another digital pin, don't forget to change
    the code to match).
 
    Connect the negative side of your LED (shorter leg) to a
    330 Ohm resistor (orange-orange-brown). Connect the other side
    of the resistor to ground.

    pin 13 _____ + LED - _____ 330 Ohm _____ GND

    (We always use resistors between the Arduino and and LEDs
    to keep the LEDs from burning out due to too much current.)

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Welcome to Arduino!

// If you're brand-new to this, there will be some new things to
// learn, but we'll jump right in and explain things as we go.

// The Arduino is a tiny computer that runs programs called
// "sketches". These are text files written using instructions
// the computer understances. You're reading a sketch right now.

// Sketches have computer code in them, but also (hopefully)
// "comments" that explain what the code does. Comments and code
// will have different colors in the editor so you can tell them
// apart.

// This is a comment - anything on a line after "//" is ignored
// by the computer.

/* This is also a comment - this one can be multi-line, but it
must start and end with these characters */

// A "function" is a named block of code, that performs a specific,
// well, function. Many useful functions are already built-in to
// the Arduino; others you'll name and write yourself for your
// own purposes.

// All Arduino sketches MUST have two specific functions, named
// "setup()" and "loop()". The Arduino runs these functions
// automatically when it starts up or if you press the reset
// button. You'll typically fill these function "shells" with your
// own code. Let's get started!


// The setup() function runs once when the sketch starts.
// You'll use it for things you need to do first, or only once:


void setup()
{
  // The Arduino has 13 digital input/output pins. These pins
  // can be configured as either inputs or outputs. We set this
  // up with a built-in function called pinMode().

  // The pinMode() function takes two values, which you type in
  // the parenthesis after the function name. The first value is
  // a pin number, the second value is the word INPUT or OUTPUT.
 
  // Here we'll set up pin 13 (the one connected to a LED) to be
  // an output. We're doing this because we need to send voltage
  // "out" of the Arduino to the LED.

  pinMode(13, OUTPUT);

  // By the way, the Arduino offers many useful built-in functions
  // like this one. You can find information on all of them at the
  // Arduino website: http://arduino.cc/en/Reference
}


// After setup() finishes, the loop() function runs over and over
// again, forever (or until you turn off or reset the Arduino).
// This is usually where the bulk of your program lives:


void loop()
{
  // The 13 digital pins on your Arduino are great at inputting
  // and outputting on/off, or "digital" signals. These signals
  // will always be either 5 Volts (which we call "HIGH"), or
  // 0 Volts (which we call "LOW").

  // Because we have an LED connected to pin 13, if we make that
  // output HIGH, the LED will get voltage and light up. If we make
  // that output LOW, the LED will have no voltage and turn off.

  // digitalWrite() is the built-in function we use to make an
  // output pin HIGH or LOW. It takes two values; a pin number,
  // followed by the word HIGH or LOW:

  digitalWrite(13, HIGH);   // Turn on the LED

  // delay() is a function that pauses for a given amount of time.
  // It takes one value, the amount of time to wait, measured in
  // milliseconds. There are 1000 milliseconds in a second, so if
  // you delay(1000), it will pause for exactly one second:
 
  delay(1);              // Wait for one millisecond
 
  digitalWrite(13, LOW);    // Turn off the LED
 
  delay(1000);              // Wait for one second

  // All together, the above code turns the LED on, waits one
  // second, turns it off, and waits another second.

  // When the computer gets to the end of the loop() function,
  // it starts loop() over again. So this program will continue
  // blinking the LED on and off!

  // Try changing the 1000 in the above delay() functions to
  // different numbers and see how it affects the timing. Smaller
  // values will make the loop run faster. (Why?)
}

Question 13 - Blink (LED mostly off)

/*
SparkFun Inventor's Kit
Example sketch 01

BLINKING A LED

  Turn an LED on for one second, off for one second,
  and repeat forever.

Hardware connections:

  Most Arduinos already have an LED and resistor connected to
  pin 13, so you may not need any additional circuitry.

  But if you'd like to connect a second LED to pin 13, or use
  a different pin, follow these steps:

    Connect the positive side of your LED (longer leg) to Arduino
    digital pin 13 (or another digital pin, don't forget to change
    the code to match).
 
    Connect the negative side of your LED (shorter leg) to a
    330 Ohm resistor (orange-orange-brown). Connect the other side
    of the resistor to ground.

    pin 13 _____ + LED - _____ 330 Ohm _____ GND

    (We always use resistors between the Arduino and and LEDs
    to keep the LEDs from burning out due to too much current.)

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Welcome to Arduino!

// If you're brand-new to this, there will be some new things to
// learn, but we'll jump right in and explain things as we go.

// The Arduino is a tiny computer that runs programs called
// "sketches". These are text files written using instructions
// the computer understances. You're reading a sketch right now.

// Sketches have computer code in them, but also (hopefully)
// "comments" that explain what the code does. Comments and code
// will have different colors in the editor so you can tell them
// apart.

// This is a comment - anything on a line after "//" is ignored
// by the computer.

/* This is also a comment - this one can be multi-line, but it
must start and end with these characters */

// A "function" is a named block of code, that performs a specific,
// well, function. Many useful functions are already built-in to
// the Arduino; others you'll name and write yourself for your
// own purposes.

// All Arduino sketches MUST have two specific functions, named
// "setup()" and "loop()". The Arduino runs these functions
// automatically when it starts up or if you press the reset
// button. You'll typically fill these function "shells" with your
// own code. Let's get started!


// The setup() function runs once when the sketch starts.
// You'll use it for things you need to do first, or only once:


void setup()
{
  // The Arduino has 13 digital input/output pins. These pins
  // can be configured as either inputs or outputs. We set this
  // up with a built-in function called pinMode().

  // The pinMode() function takes two values, which you type in
  // the parenthesis after the function name. The first value is
  // a pin number, the second value is the word INPUT or OUTPUT.
 
  // Here we'll set up pin 13 (the one connected to a LED) to be
  // an output. We're doing this because we need to send voltage
  // "out" of the Arduino to the LED.

  pinMode(13, OUTPUT);

  // By the way, the Arduino offers many useful built-in functions
  // like this one. You can find information on all of them at the
  // Arduino website: http://arduino.cc/en/Reference
}


// After setup() finishes, the loop() function runs over and over
// again, forever (or until you turn off or reset the Arduino).
// This is usually where the bulk of your program lives:


void loop()
{
  // The 13 digital pins on your Arduino are great at inputting
  // and outputting on/off, or "digital" signals. These signals
  // will always be either 5 Volts (which we call "HIGH"), or
  // 0 Volts (which we call "LOW").

  // Because we have an LED connected to pin 13, if we make that
  // output HIGH, the LED will get voltage and light up. If we make
  // that output LOW, the LED will have no voltage and turn off.

  // digitalWrite() is the built-in function we use to make an
  // output pin HIGH or LOW. It takes two values; a pin number,
  // followed by the word HIGH or LOW:

  digitalWrite(13, HIGH);   // Turn on the LED

  // delay() is a function that pauses for a given amount of time.
  // It takes one value, the amount of time to wait, measured in
  // milliseconds. There are 1000 milliseconds in a second, so if
  // you delay(1000), it will pause for exactly one second:
 
  delay(1);              // Wait for one second
 
  digitalWrite(13, LOW);    // Turn off the LED
 
  delay(1);             // Wait for one millisecond

  // All together, the above code turns the LED on, waits one
  // second, turns it off, and waits another second.

  // When the computer gets to the end of the loop() function,
  // it starts loop() over again. So this program will continue
  // blinking the LED on and off!

  // Try changing the 1000 in the above delay() functions to
  // different numbers and see how it affects the timing. Smaller
  // values will make the loop run faster. (Why?)
}

Question 12 - Blink (Variation in Timing)

/*
SparkFun Inventor's Kit
Example sketch 01

BLINKING A LED

  Turn an LED on for one second, off for one second,
  and repeat forever.

Hardware connections:

  Most Arduinos already have an LED and resistor connected to
  pin 13, so you may not need any additional circuitry.

  But if you'd like to connect a second LED to pin 13, or use
  a different pin, follow these steps:

    Connect the positive side of your LED (longer leg) to Arduino
    digital pin 13 (or another digital pin, don't forget to change
    the code to match).
 
    Connect the negative side of your LED (shorter leg) to a
    330 Ohm resistor (orange-orange-brown). Connect the other side
    of the resistor to ground.

    pin 13 _____ + LED - _____ 330 Ohm _____ GND

    (We always use resistors between the Arduino and and LEDs
    to keep the LEDs from burning out due to too much current.)

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Welcome to Arduino!

// If you're brand-new to this, there will be some new things to
// learn, but we'll jump right in and explain things as we go.

// The Arduino is a tiny computer that runs programs called
// "sketches". These are text files written using instructions
// the computer understances. You're reading a sketch right now.

// Sketches have computer code in them, but also (hopefully)
// "comments" that explain what the code does. Comments and code
// will have different colors in the editor so you can tell them
// apart.

// This is a comment - anything on a line after "//" is ignored
// by the computer.

/* This is also a comment - this one can be multi-line, but it
must start and end with these characters */

// A "function" is a named block of code, that performs a specific,
// well, function. Many useful functions are already built-in to
// the Arduino; others you'll name and write yourself for your
// own purposes.

// All Arduino sketches MUST have two specific functions, named
// "setup()" and "loop()". The Arduino runs these functions
// automatically when it starts up or if you press the reset
// button. You'll typically fill these function "shells" with your
// own code. Let's get started!


// The setup() function runs once when the sketch starts.
// You'll use it for things you need to do first, or only once:


void setup()
{
  // The Arduino has 13 digital input/output pins. These pins
  // can be configured as either inputs or outputs. We set this
  // up with a built-in function called pinMode().

  // The pinMode() function takes two values, which you type in
  // the parenthesis after the function name. The first value is
  // a pin number, the second value is the word INPUT or OUTPUT.
 
  // Here we'll set up pin 13 (the one connected to a LED) to be
  // an output. We're doing this because we need to send voltage
  // "out" of the Arduino to the LED.

  pinMode(13, OUTPUT);

  // By the way, the Arduino offers many useful built-in functions
  // like this one. You can find information on all of them at the
  // Arduino website: http://arduino.cc/en/Reference
}


// After setup() finishes, the loop() function runs over and over
// again, forever (or until you turn off or reset the Arduino).
// This is usually where the bulk of your program lives:


void loop()
{
  // The 13 digital pins on your Arduino are great at inputting
  // and outputting on/off, or "digital" signals. These signals
  // will always be either 5 Volts (which we call "HIGH"), or
  // 0 Volts (which we call "LOW").

  // Because we have an LED connected to pin 13, if we make that
  // output HIGH, the LED will get voltage and light up. If we make
  // that output LOW, the LED will have no voltage and turn off.

  // digitalWrite() is the built-in function we use to make an
  // output pin HIGH or LOW. It takes two values; a pin number,
  // followed by the word HIGH or LOW:

  digitalWrite(13, HIGH);   // Turn on the LED

  // delay() is a function that pauses for a given amount of time.
  // It takes one value, the amount of time to wait, measured in
  // milliseconds. There are 1000 milliseconds in a second, so if
  // you delay(1000), it will pause for exactly one second:
 
  delay(500);             // Wait for half a second
 
  digitalWrite(13, LOW);    // Turn off the LED
 
  delay(1000);              // Wait for one second

  // All together, the above code turns the LED on, waits one
  // second, turns it off, and waits another second.

  // When the computer gets to the end of the loop() function,
  // it starts loop() over again. So this program will continue
  // blinking the LED on and off!

  // Try changing the 1000 in the above delay() functions to
  // different numbers and see how it affects the timing. Smaller
  // values will make the loop run faster. (Why?)
}

Question 11 - Blink Program (Original)

/*
SparkFun Inventor's Kit
Example sketch 01

BLINKING A LED

  Turn an LED on for one second, off for one second,
  and repeat forever.

Hardware connections:

  Most Arduinos already have an LED and resistor connected to
  pin 13, so you may not need any additional circuitry.

  But if you'd like to connect a second LED to pin 13, or use
  a different pin, follow these steps:

    Connect the positive side of your LED (longer leg) to Arduino
    digital pin 13 (or another digital pin, don't forget to change
    the code to match).
 
    Connect the negative side of your LED (shorter leg) to a
    330 Ohm resistor (orange-orange-brown). Connect the other side
    of the resistor to ground.

    pin 13 _____ + LED - _____ 330 Ohm _____ GND

    (We always use resistors between the Arduino and and LEDs
    to keep the LEDs from burning out due to too much current.)

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/


// Welcome to Arduino!

// If you're brand-new to this, there will be some new things to
// learn, but we'll jump right in and explain things as we go.

// The Arduino is a tiny computer that runs programs called
// "sketches". These are text files written using instructions
// the computer understances. You're reading a sketch right now.

// Sketches have computer code in them, but also (hopefully)
// "comments" that explain what the code does. Comments and code
// will have different colors in the editor so you can tell them
// apart.

// This is a comment - anything on a line after "//" is ignored
// by the computer.

/* This is also a comment - this one can be multi-line, but it
must start and end with these characters */

// A "function" is a named block of code, that performs a specific,
// well, function. Many useful functions are already built-in to
// the Arduino; others you'll name and write yourself for your
// own purposes.

// All Arduino sketches MUST have two specific functions, named
// "setup()" and "loop()". The Arduino runs these functions
// automatically when it starts up or if you press the reset
// button. You'll typically fill these function "shells" with your
// own code. Let's get started!


// The setup() function runs once when the sketch starts.
// You'll use it for things you need to do first, or only once:


void setup()
{
  // The Arduino has 13 digital input/output pins. These pins
  // can be configured as either inputs or outputs. We set this
  // up with a built-in function called pinMode().

  // The pinMode() function takes two values, which you type in
  // the parenthesis after the function name. The first value is
  // a pin number, the second value is the word INPUT or OUTPUT.
 
  // Here we'll set up pin 13 (the one connected to a LED) to be
  // an output. We're doing this because we need to send voltage
  // "out" of the Arduino to the LED.

  pinMode(13, OUTPUT);

  // By the way, the Arduino offers many useful built-in functions
  // like this one. You can find information on all of them at the
  // Arduino website: http://arduino.cc/en/Reference
}


// After setup() finishes, the loop() function runs over and over
// again, forever (or until you turn off or reset the Arduino).
// This is usually where the bulk of your program lives:


void loop()
{
  // The 13 digital pins on your Arduino are great at inputting
  // and outputting on/off, or "digital" signals. These signals
  // will always be either 5 Volts (which we call "HIGH"), or
  // 0 Volts (which we call "LOW").

  // Because we have an LED connected to pin 13, if we make that
  // output HIGH, the LED will get voltage and light up. If we make
  // that output LOW, the LED will have no voltage and turn off.

  // digitalWrite() is the built-in function we use to make an
  // output pin HIGH or LOW. It takes two values; a pin number,
  // followed by the word HIGH or LOW:

  digitalWrite(13, HIGH);   // Turn on the LED

  // delay() is a function that pauses for a given amount of time.
  // It takes one value, the amount of time to wait, measured in
  // milliseconds. There are 1000 milliseconds in a second, so if
  // you delay(1000), it will pause for exactly one second:
 
  delay(1000);              // Wait for one second
 
  digitalWrite(13, LOW);    // Turn off the LED
 
  delay(1000);              // Wait for one second

  // All together, the above code turns the LED on, waits one
  // second, turns it off, and waits another second.

  // When the computer gets to the end of the loop() function,
  // it starts loop() over again. So this program will continue
  // blinking the LED on and off!

  // Try changing the 1000 in the above delay() functions to
  // different numbers and see how it affects the timing. Smaller
  // values will make the loop run faster. (Why?)
}

Saturday, March 19, 2016

LED Project - Audioino

Arduino LED Project
Oscar Macdonald

The Project:

The Arduino uses a row of 8 LEDs  that will turn on or off every 10 milliseconds depending on the amplitude (loudness) of an audio signal, which is detected using an Arduino compatible microphone module. The microphone sends a voltage between 0 and 5 to the Arduino, which converts the voltage into a number between 0 and 1023. The 8 LEDs each represent a Byte of Binary data, and the total combination of possible on/off states of the LEDs result in a total of 255 unique frequencies (any amplitude greater than 255 will result in all lights being on). This is different from other projects that people have done; some people choose to make each light turn on as the audio becomes louder. I chose the ‘binary’ representation because it is an interesting way to show how audio can be ‘recorded’ onto a computer using binary.

finished project.


Fritzing Diagram
Schematic
In the above video, the microphone is detecting noise (tapping the microphone) and the LEDs are outputting.

In the above video, I used an alternate code (not part of this project) that shows the increase in noise by turning on the lights in sequence. Also shows a serial output from the arduino, that shows the microphone detecting noise.

C Code:

 /* Code written by Oscar Macdonald
 * Initial code adapted from Amanda Ghassaei (http://www.instructables.com/id/Arduino-Audio-Input/)
 * Inspiration from mashendavideo (https://www.youtube.com/watch?v=YMJFigrKXpw)
 * Credit: 
 * Campbell McDowall
 * William Smail
 * Peter Brook
 * 17/03/2016
 * 
 * This arduino sketch uses an Arduino Compatible Microphone Sound Sensor Module
 * (Catalogue #:XC4438) from Jaycar Electronics, to receive audio input,  
 * return an amplitude measurements in volts between 0 - 1023,
 * and display the number in binary form on the LEDs on the breadboard.
 */

int A0input;//storage for A0 data

void setup(){
  // Each pin represents a different binary digit
  pinMode(5, OUTPUT);   // 1
  pinMode(6, OUTPUT);   // 2
  pinMode(7, OUTPUT);   // 4
  pinMode(8, OUTPUT);   // 8
  pinMode(9, OUTPUT);   // 16
  pinMode(10, OUTPUT);  // 32
  pinMode(11, OUTPUT);  // 64
  pinMode(12, OUTPUT);  // 128
  Serial.begin (9600);  // for testing purposes, allows an arduino to send information back to the computer                                                                

}

void loop(){
  A0input = analogRead(0);//get new value from A0
  Serial.println(A0input);// For testing purposes, sends back an ASCII value (0-1023) that can be easily read
  int binary[] = {0, 0, 0, 0, 0, 0, 0, 0}; // array of binary values
  int index = 0;      // used in the loop for the array
  while(A0input > 0)  // this loop converts the input to a binary value
  {
    binary[index++] = A0input % 2;
    A0input = A0input / 2;
  }
  for(int i = sizeof(binary); i > 0; i--)
  {
    if (binary[i] == 1)
    {
      digitalWrite(i + 4, HIGH);  // turns the light on if the binary value is 1
    }
    else // binary[i] == 0
    {
      digitalWrite(i + 4, LOW); // turns the light off if the binary value is 0
    }
  }
  delay(10);
}

Reflection
The idea originally came from a brainstorming session in the first week of class. Every time I came up with an idea I would write it down. I decided that an audio input would have enough complexity to be a sufficient challenge, it could be adapted to include LEDs in multiple ways, and it would be satisfying to have the Arduino respond to voice or music.

I conducted some research on the subject of Arduinos and audio input. The first article I came across was an Instructables article by amandaghassaei (1). This article was inspiring and the list of materials was so detailed it made it look like it was an easy project to build; half the materials came with the original sparkfun kit and the rest were easy to obtain.
After attempting to recreate the circuit I realized I was out of my depth; I couldn’t understand the circuit diagram included on the website, and the images of the breadboard were unfocused and therefore hard to replicate. I gave up on this approach but my time wasn’t entirely wasted: I had some new circuit parts and I had learnt valuable information about audio input and amplifying audio input (without an amplifier, the audio will output between -2.5 and 2.5 volts, so it needs to be amplified to sit between 0 and 5).
Schematic of amandaghassaei’s project (1). While this schematic is highly informative, it was beyond my skill to replicate it. On reflection, I might have had a bit more success if I knew what I know now.

Photo by amandaghassaei (1)This isn’t art school, we’re not here to take photos out of focus! A shot from directly above would have been more instructive. 
I found a second Arduino audio tutorial on youtube (2) which looked a lot less complicated and didn’t require any extra components. After replicating their circuit I tried to get an audio signal. The audio input wire was loosely touching the 6.5mm audio pin without a proper socket or join, and there was a lot of interference from other sources. Touching the audio input wire to the positive pin of the XLR connector of the microphone. Thanks to Campbell McDowall, I discovered the Arduino Compatible Microphone Sound Sensor Module that was available for purchase. This solved both the problem of the complex circuit which would take up valuable space on the breadboard (1) and the microphone incompatibility with the Arduino (2).

After creating a circuit with the module and testing for audio, initially the output of the Arduino (using serial.println()) was constantly between 50-52. After configuring the circuit, more research and consultation, I discovered that the potentiometer on the module had to be configured to the right setting so the audio would be detected (3). After configuring the potentiometer the serial output began to show more relatable figures, and there was a change in the output when the microphone was tested. This output is not as accurate as I would like (the LEDs do not respond to the human voice in any observable way) but given more time or other materials this could be changed.
A serial monitor of the analogue output, showing the microphone is working.

I created a segment of code which converts the output of the microphone to binary values, and each LED represents a single binary value, so that any reading between 0 and 255 would give a binary value. I decided to use this LED output to add originality to my project and also as a replication of very simple data storage. I realized that the microphone could only detect the amplitude of the audio, not the frequency, so I have adjusted the goal of the project to detect amplitude instead.

If I did this project again I would use a larger breadboard so there was ample room for all the circuit components, and even to add more LEDs to make a larger range of binary data (to cover all 1024 different values). I would also have spent more time configuring the potentiometer so the microphone would detect the right amplitude (preferably human speech). With a more sensitive / correctly configured microphone this project could be used for cool projects like tone password doors: a door that only unlocks when the correct tune is played on a piano. This would rely on frequency rather than amplitude of audio, which might be beyond the scope of Arduinos, but there are more tutorials online which seem to think it is possible.

I would like to acknowledge Campbell McDowall for his support and advice. I would also like to thank William Smail and Peter Brook for their assistance with the microphone and their advice.

References:

3.    https://www.youtube.com/watch?v=aa3F4ALaEok