Category Archives: Assignment

Arduino Analog Read and Tones

Class Recap:

  • Introduction to Arduino
  • Circuits, Symbols and Breadboarding

At the end of class we learned about analog inputs, and how they can be mapped to tone values. Digital inputs work in a similar way, instead of reading various values you will only read 1s and 0s, 5 volts or 0 volts. Arduino programming is very similar to Processing, you can add if statements and for loops; utilize the logic you have learned so far. By adding a conditional statement that listens to the button presses, you can trigger a sound. It could be a piece of music you compose or maybe you can make a piano if you make every button have its own sound.


Create a project that uses the tones function, with your piezo buzzer, you can add either an analog or digital input to trigger the sound. In this prototype you should experiment in making an enclosure. This interface should be conceptual and also interactive, think outside the box, or inside the music box, or an instrument, or a toy :)

Analog Read:


Analog Read:


/*Class Code: This sketch listens to light! Depending on the variable resistance of your photoresistor you can map its value to tones*/

int speaker = 11;
int sensor = A0;
int sensorValue = 0;

void setup() {
pinMode(sensor, INPUT);
pinMode(speaker, OUTPUT);

void loop() {
sensorValue= analogRead(sensor);
sensorValue = map(sensorValue, 1, 15, 31, 3000);
tone(speaker, sensorValue);

Digital Read:



Digital Read:



Learning For Loops: Code

// Hello! This is a for loop tutorial!
// You can copy and paste this on to a new processing sketch!
// By: aisencc
In a for loop you have 3 portions: (init; test; update){ stuff }
The value is first initiated, then tested, then the loop does “stuff”, and then it is updated.

An initiating value happens only once the first time the loop runs and it looks like:
int i= 0;
This initiating value is what your number count will start from. Usually we start counting from 0.
We use “i” because in math i is used sometimes in place for an intereger.
If we are using nested forloops we will initiate the value with “j” then “k” and so forth.
We could use “int ilovethisnumber = 0;” but that is just too long.

The test is much like the comparator tests we use in if statements, it looks like:
i < height;
i < 100;
This means that once i grows bigger than the height of our sketch the loop will stop.

The update is the opperation that allows the i to grow or shrink, it looks like:
i++; will add 1 everytime
i–; will subtract 1.
i+=10; adds 10 every time.
The update is last command to run after each turn of the loop,
and it will stop once the number is to high or too low and fails the test.

Run this sketch and see what happens.
Comment stuff out below, and run one forloop at a time to see which loop is drawing the lines you see

// remember that “//” allows you to comment things out, and not affect the code
// you can also use /* stuff */ to comment stuff out

void setup() {
size(600, 900); //window size
background(255); // white background
stroke(125, 125, 255); // color of our lines

for (int i = 0; i < height; i+=10) {
line(0, i, width, i);
for (int i = 0; i < width; i+=10) {
line(i, 0, i, height);

stroke(255, 30, 30);

for (int i = height/2; i < height; i = i+5) {
line(0, i, width, i);

for (int i = 40; i < 80; i = i+5) {
line(30, i, 80, i);
//// run this later! :)
//void draw() {
// stroke(0);
// for (int i = 0; i < height; i = i+90) {
// line(mouseX, i, mouseY, i);
// }
// for (int i = 0; i < width; i = i+90) {
// line(i, mouseX, i, mouseY);
// }