quarta-feira, 17 de fevereiro de 2016

Arduino Project #2: Musical keyboard (6 notes) with buzzer and pushbuttons


Overview

With Arduino, we can generate sounds in several frequencies using a simple buzzer or speaker.
Arduino's API comes with specific functions for this given task: tone() and noTone() [1].

In this project, we'll use a bunch of pushbuttons in order to create a musical keyboard!

The goal is to produce distinct tones for each of the 6 available pushbuttons.
These six tones should correspond to these musical notes: C5, D5, E5, F5, G5, and A5.
Yeah, B5 is missing due to lack of space in my small 400-contacts breadboard.

Note frequencies were retrieved from Physics of Music Notes (MTU Physics) [2].



Components

  • 1 buzzer (or speaker)
  • 6 pushbuttons
  • 6 current resistors
  • Jumper wires
  • Breadboard
  • Arduino Uno compatible

Schematics

[https://github.com/hjort/arduino-toys/blob/master/buzzer/MusicalKeyboard.fz]


Source Code

#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
#define BUTTON3_PIN 5
#define BUTTON4_PIN 6
#define BUTTON5_PIN 7
#define BUTTON6_PIN 8
#define BUZZER_PIN  9

#define FIRST_BUTTON BUTTON1_PIN
#define LAST_BUTTON  BUTTON6_PIN

#define FREQ_C5 523.25
#define FREQ_D5 587.33
#define FREQ_E5 659.25
#define FREQ_F5 698.46
#define FREQ_G5 783.99
#define FREQ_A5 880.00

const float freqs[] = {FREQ_C5, FREQ_D5, FREQ_E5, FREQ_F5, FREQ_G5, FREQ_A5};

void setup()
{
  for (int pin = FIRST_BUTTON; pin <= LAST_BUTTON; pin++)
    pinMode(pin, INPUT);
}

void loop()
{
  int button = 0, freq = 0;
  for (int pin = FIRST_BUTTON; pin <= LAST_BUTTON; pin++) {
    if (digitalRead(pin) == HIGH) {
      button = pin - FIRST_BUTTON;
      freq = int(freqs[button]);
      tone(BUZZER_PIN, freq, 100);
    }
  }
  delay(100);
}

[https://github.com/hjort/arduino-toys/blob/master/buzzer/MusicalKeyboard.ino]


Project in Action




References

  1. Arduino Language Reference Guide - Tone: https://www.arduino.cc/en/Reference/Tone
  2. Frequencies of Musical Notes: http://www.phy.mtu.edu/~suits/notefreqs.html

quarta-feira, 10 de fevereiro de 2016

Arduino Project #1: RGB LED Adjusted by Potentiometer


Overview

Arduino's digital pins 9, 10, and 11 have PWM (Pulse-Width Modulation), which means they are able to output a kind of analog signal. This given signal is 8-bits large, that is, integer values ranging from 0 to 255.

On the other hand, Arduino's analog pins can be used to read integer values of 10-bits (from 0 to 1023).

In this project, we'll use a single potentiometer to control bright of 3 Super LEDs at the same time!



Components

  • 3 Super LEDs (red, green, and blue)
  • 3 Current Resistors
  • 20 kOhm Potentiometer
  • Jumper wires
  • Breadboard
  • Arduino Uno compatible

Schematics

[https://github.com/hjort/arduino-toys/blob/master/rgb/RgbLedPot.fz]

Source Code

// RGB LED Adjusted by Potentiometer

const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;
const int potPin = 2; // select the input pin for the potentiometer

unsigned int potValue, lastValue; // 16-bit (0..16535)
byte red, green, blue, alpha; // 8-bit (0..255)

void setup() {
  Serial.begin(9600);
}

void loop() {

  // read the value from the pot (10 bits => 0 to 1023)
  potValue = analogRead(potPin);
  
  if (lastValue == potValue)
    return;
  else
    lastValue = potValue;
  
  // 16-bit placement:
  // ------ARRRGGGBBB
  
  alpha = (potValue >> 9);                  // 0..1
  red   = ((potValue << 7) >> 9) << alpha;  // 0..255
  green = ((potValue << 10) >> 9) << alpha; // 0..255
  blue  = ((potValue << 13) >> 9) << alpha; // 0..255
  
  Serial.print("potValue = ");
  Serial.print(potValue);
  Serial.print(", R = ");
  Serial.print(red);
  Serial.print(", G = ");
  Serial.print(green);
  Serial.print(", B = ");
  Serial.print(blue);
  Serial.print(", A = ");
  Serial.println(alpha);
  
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
  delay(100);
}

[https://github.com/hjort/arduino-toys/blob/master/rgb/RgbLedPot.ino]


Project in Action


[https://www.youtube.com/watch?v=To8oFhSXuy0]