Tuesday, 15 November 2016

Arduino Binary Counters & MIDI - Super basic program

Though this is not directly related to music, but I think its good to know a little about bytes & bits.
Arduino's are digital devices and only understand computer language.
When you attach an analog voltage to one of the pins, it's translated into zeros and ones by a ADC.
 
Midi notes are also all about zeroes and ones. They are digital signals.


Here is a Decimal/Binary/Octal/Hex conversion table 

Binary numbers are zeros and ones. You can think of them like switches being on or off.
MIDI notes take the form of these binary numbers.
They can be divided into two types: command bytes and data bytes. 

Command bytes are always 128 or greater, or 0x80 to 0xFF in hexadecimal. 
If you convert these numbers to binary, you will see they range between
 10000000 to 11111111..... that is, the first number (MSB or most significant bit) is always a one.
This is how it knows its a Command byte.
These include things like note on, note off, pitch bend, aftertouch, continuous controller, channel pressure, .

Data bytes are always less than 127, or 0x00 to 0x7F in hex. 
If you convert these numbers to binary, you will see they range between
 00000000 to 01111111..... that is, the first number (MSB or most significant bit) is always a zero.
This is how it knows its a Data byte.
These include things like Pitch, Velocity , pitch bend amount & loudness.

MIDI commands are further broken down by the following system:

The first half of the MIDI command byte (the three bits following the MSB) sets the type of command. More info about the meaning on each of these commands is here.
10000000 = note off
10010000 = note on
10100000 = aftertouch
10110000 = continuous controller
11000000 = patch change
11010000 = channel pressure
11100000 = pitch bend
11110000 = non-musical commands

The last half of the command byte sets the MIDI channel. All the bytes listed above would be in channel 0, command bytes ending in 0001 would be for MIDI channel 1, and so on.

All MIDI messages start with a command byte, some messages contain one data byte, others contain two or more (see image above). For example, a note on command byte is followed by two data bytes: note and velocity.

--------------------------------------------------------------
Getting back to building the counter.
 This is a simple binary counter.
Just 4 LEDS., 4 resistors(330 ohms).
We use 4 digital pins.
Pretty basic, so a great learning tool.
 
Make sure that the longer lead (positive) of each LED is the one you connect to the Arduino pin and that the resistors connect the shorter lead (negative) to the GND rail.
 
I'll use digital pins 2, 3, 4, 5 
 
--------------------------------------------------------------

This is the initial code to test that the circuit works:
All 4 LEDs should light up

// variables
int pin2=2;
int pin3=3;
int pin4=4;
int pin5=5;  

void setup()
{
  // commands
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
}

void loop()
{
  // I want to step through each number from zero to 15
  // then I want to repeat
  digitalWrite(pin2, HIGH);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, HIGH);
 
}
 

 
 --------------------------------------------------------------------------
 
The final code
Each number is written individually
This is really just an basic exercise on how to turn on/off LEDs in sequence.
A simple delay is added between each number
 
 
 
 // variables
int pin2=2;
int pin3=3;
int pin4=4;
int pin5=5;  
int waitTime=100;

void setup()
{
  // commands
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
}

void loop()
{
  // I want to step through each number from zero to 15
  // then I want to repeat
 
  //0
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //1
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //2
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //3
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //4
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //5
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //6
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //7
    digitalWrite(pin2, LOW);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //8
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //9
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //10
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //11
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //12
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //13
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
 
  //14
    digitalWrite(pin2, HIGH);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, LOW);
  delay(waitTime);
 
  //15
  digitalWrite(pin2, HIGH);
  digitalWrite(pin3, HIGH);
  digitalWrite(pin4, HIGH);
  digitalWrite(pin5, HIGH);
  delay(waitTime);
}




Link
+ http://www.multiwingspan.co.uk/arduino.php?page=led5
 
 ---------------------------------
------------------------------------- 

No comments:

Post a Comment