Lecture notes

advertisement

Class Notes

Week 4

More Programming Protocols

Arrays

The array contains one or more variables in a list. This is how an array is declared in Arduino: int someArray[] = {1, 2, 3};

“int” signifies the datatype that the array contains;

“someArray” is the name that is given to the array variable.

Accessing an array

Lets look at another array: int someArray[] = {9, 3, 2, 4, 3, 2, 7, 8, 17, 11};

To access the number “17” in the array, you need to supply the complier with the index number of the variable “17.” Arrays are ZERO INDEXED, meaning that the first element of an array is indexed at 0 instead of 1. Hence: someArray[8] // contains the variable “17”

Operators

Operators are the symbols that a complier use to perform commands and calculations in your program.

1. Arithmetic Operators

+ function: adds two values example: int apples = 5 int moreApples = apples + 3; // moreApples is now 8.

“ - ” “ * ” and “ / ” functions similarly.

2. Compound Operators

+= (compound addition) function: adds the value on the right to the value on the left. example: int height = 6; height += 1; // height is now 7 height += 10 // height is now 17

-=, *= and /= functions similarly.

++ and – function: add one or subtract one from the variable on the left.

example: int height = 2; height++; // height is now 3 height--; // height is now 2 height--; // height is now 1.

3. Comparison Operators

Comparisons are very important particularly in using control statements (we will get onto this a little later today). Comparisons allow you to determine whether variables are the same, different, greater than or less than one another.

== (equal to) functions: compare two things to see whether they are equal.

!= (not equal to)

> (greater than)

< (less than)

>= (greater than or equal to)

<= (less than or equal to)

&& function: evaluates the value on the left and the statement on the right, and returns TRUE if they are both true. example:

(4 > 3) && ((6-3) != 4) // this will return a “true”

Control Statements for Loop

The for statement let us do something over and over again, for a specified number of repetition, until certain conditions are met.

First element inside of the for loop: initialize the counter

Second element: continue until this statement is false

Third element: What to do on each pass through of the loop example: int i; for(i = 0; I < 10; i++) { print(“i is” + i); // this will print i is 0, i is 1, i is 2... if/else

This is a conditional logic statement, and it works just like it does in English: if(condition) { do this if the condition is true

} else { do this if the condition is false

}

You must include a true/false expression in the brackets next to the if. example:

int myWeight = 70; if (myWeight > 100) { print(“you're getting fat!”);

} else { print(“you're still beautiful”);

}

While Loop

While loop will loop continuously until the expression inside of the bracket becomes false. example: int Samson = 0; while (Samson < 100) { print (“you are less than 100!” +Samson);

Samson++;

When Samson is no longer less than 100 the loop is exited.

Switches (excerpted from Adafruit’s tutorial)

NO = normally open

NC = normally closed

Push button controlling current

Before you try to turn a 100W lightbulb on and off using a pushbutton switch, be aware that switches have ratings that will tell you the maximum amount of current and voltage they can switch. The little switches are only rated for a few volts and milliAmps. Big switches such as wall light switches are rated for 120V and many Amperes.

Push button as digital input

Wire as switch

/*

* Switch test program

*/ int switchPin = 2; // Switch connected to digital pin 2 void setup() // run once, when the sketch starts

{

}

Serial.begin(9600); // set up Serial library at 9600 bps

pinMode(switchPin, INPUT); // sets the digital pin as input to read switch void loop() // run over and over again

{

Serial.print("Read switch input: ");

Serial.println(digitalRead(switchPin)); // Read the pin and display the value

delay(100);

}

Whats this 100Ω resistor all about?

There's a 100Ω resistor we use to connect the input pin to either HIGH or LOW voltage. Why is it there? Well, lets say you accidentally set P2 to be an OUTPUT type pin, but then you connected it to 5V. If you write a LOW to the pin (0V) but its connected to HIGH (5V), you've basically caused a short circuit at that pin. This isn't very good for the pin and could damage it! The 100Ω resistor acts as a buffer, to protect the pin from short circuits.

Invalid input

For example, in these schematics we can connect and disconnect pin 2 to 5V, or we can connect and disconnect pin 2 to ground. In both cases, as long as the button is held down, the pin is connected to a valid input voltage. When the button is released, though, pin 2 is not connected to anything.

This is called a floating input voltage. Basically, it's invalid input!

Try building up one of these schematics, and trying out the switch testing sketch above. When the button is held down you should definately get the right printout. When its released, it may keep the old value, or it may change, but its certainly not reliable!

One solution is this:

Or we use what is known as a pull down resistor!

The pull-down resistor here is the 10K resistor. When the switch is held down, the 100Ω resistor is

connected directly to 5V. When the switch is released, the 100Ω resistor is connected to the 10K resistor which pulls it down to ground.

Here's how to think of it: When you press the button and connect the 100Ω resistor to 5V, the button has a very small resistance (less than 1 Ω!), so it provides a strong pull to 5V.

The 10KΩ resistor is also connecting the 100Ω resistor to ground, but since the 10KΩ resistor has

10000 times more resistance than the button, its a very weak pull to ground and can't compete.

The strong 5V connection overpowers the weak ground connection and the input pin reads HIGH.

However, when the switch is disconnected, there is no longer a strong pull to 5V. In fact, its let go completely. But there is still weak pull to ground. Despite being a weak connection, it's better than nothing and so the resistor pulls the input pin to LOW.

You can do the same with the 5V connection and use a resistor as a pull up.

Wrap this inside of an “if” statement…

Alternating Switch

NB: test: val == HIGH

Counting Presses

NB: test val == HIGH

Tilt Sensor

Try this:

Turn an LED on with the tilt – on when in position A.

Blip: Physical Computing and Sound

You have four options:

Option1. Arduino Tone Function tone() is a function that allows you to play a tune on the Arduino.

The syntax is: tone(pin, frequency, duration).

Frequency is defined in hertz, while duration is defined in milliseconds.

#define NOTE_B0 31

#define NOTE_C1 33

#define NOTE_CS1 35

#define NOTE_D1 37

#define NOTE_DS1 39

#define NOTE_E1 41

#define NOTE_F1 44

#define NOTE_FS1 46

#define NOTE_G1 49

#define NOTE_GS1 52

#define NOTE_A1 55

#define NOTE_AS1 58

#define NOTE_B1 62

#define NOTE_C2 65

#define NOTE_CS2 69

#define NOTE_D2 73

#define NOTE_DS2 78

#define NOTE_E2 82

#define NOTE_F2 87

#define NOTE_FS2 93

#define NOTE_G2 98

#define NOTE_GS2 104

#define NOTE_A2 110

#define NOTE_AS2 117

#define NOTE_B2 123

#define NOTE_C3 131

#define NOTE_CS3 139

#define NOTE_D3 147

#define NOTE_DS3 156

#define NOTE_E3 165

#define NOTE_F3 175

#define NOTE_FS3 185

#define NOTE_G3 196

#define NOTE_GS3 208

#define NOTE_A3 220

#define NOTE_AS3 233

#define NOTE_B3 247

#define NOTE_C4 262

#define NOTE_CS4 277

#define NOTE_D4 294

#define NOTE_DS4 311

#define NOTE_E4 330

#define NOTE_F4 349

#define NOTE_FS4 370

#define NOTE_G4 392

#define NOTE_GS4 415

#define NOTE_A4 440

#define NOTE_AS4 466

#define NOTE_B4 494

#define NOTE_C5 523

#define NOTE_CS5 554

#define NOTE_D5 587

#define NOTE_DS5 622

#define NOTE_E5 659

#define NOTE_F5 698

#define NOTE_FS5 740

#define NOTE_G5 784

#define NOTE_GS5 831

#define NOTE_A5 880

#define NOTE_AS5 932

#define NOTE_B5 988

#define NOTE_C6 1047

#define NOTE_CS6 1109

#define NOTE_D6 1175

#define NOTE_DS6 1245

#define NOTE_E6 1319

#define NOTE_F6 1397

#define NOTE_FS6 1480

#define NOTE_G6 1568

#define NOTE_GS6 1661

#define NOTE_A6 1760

#define NOTE_AS6 1865

#define NOTE_B6 1976

#define NOTE_C7 2093

#define NOTE_CS7 2217

#define NOTE_D7 2349

#define NOTE_DS7 2489

#define NOTE_E7 2637

#define NOTE_F7 2794

#define NOTE_FS7 2960

#define NOTE_G7 3136

#define NOTE_GS7 3322

#define NOTE_A7 3520

#define NOTE_AS7 3729

#define NOTE_B7 3951

#define NOTE_C8 4186

#define NOTE_CS8 4435

#define NOTE_D8 4699

#define NOTE_DS8 497

Note that some notes sound better on certain speakers, others don’t sound so nice.

You will need to experiment with different speakers.

Insert the following into your sketch.

The above code is missing something: the notes as stated in our melody array is not directly playable by the Arduino. We need to define our notes in hertz and supply

Arduino with a number:

Go ahead and replace your notes with a number. Play around with the duration as well as pitch and see what happens.

Option 2. Sending MIDI signals from the Arduino

/*

MIDI note player

by Tom Igoe

*/ void setup() {

// Set MIDI baud rate:

Serial.begin(31250);

} void loop() {

// play notes from F#-0 (0x1E) to F#-5 (0x5A):

for (int note = 0x1E; note < 0x5A; note ++) {

//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):

noteOn(0x90, note, 0x45);

delay(100);

//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):

noteOn(0x90, note, 0x00);

delay(100);

}

}

// plays a MIDI note. Doesn't check to see that

// cmd is greater than 127, or that data values are less than 127: void noteOn(int cmd, int pitch, int velocity) {

Serial.write(cmd);

Serial.write(pitch);

Serial.write(velocity);

}

Serial communication must be initated at 31250 baud rate

Option 3. Using wave / mp3 player shields

Option 4. Using the music instrument shield

Using hairless MIDI

midi library

MIDI communication data rate = 115200

Install hairless MIDI

Make sure IAC Driver is online

Download