03 – class notes, 5 Sep 2017 — Arduino 1
Boring Stuff
If you are required to go to the Tue/Thu presentations for School of Art, email me with the dates you’ll miss. If it’s planned as a class lab day and there’s a presentation you want to attend, just let me know in advance.
School of Art party (7 Sep,2 017) is a class lecture/assignment day. I’d advise attending class as we’re introducing analog inputs and outputs and the first real physcomp assignment.
My office hours are noon to 3 this Friday (8 Sep) but I can come in at 11 if that works around a time conflict, just drop me email.
Class Notes
Introduction to Arduino and digital sketches.
The code we used in class with extra comments:
// -*-c++-*-
/*
The MIT License (MIT)
Copyright (c) 2014 J. Eric Townsend
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
digital_in_0
read and display status of two switches
0 == LOW == off
1 == HIGH == on
*/
// a line that starts with "//"
/* or text that starts and ends with slash-star and star-slash */
// are called "comment" lines and are ignored by the Arduino
// First, we set up some "variables" that we will use to talk to the
// external hardware.
// A variable is a way of storing information in the Arduino
// that is easy for humans to understand.
// In this sketch we will also use a "const" variable. A const can never
// be changed in the program, but can be changed before you build
// a sketch. A "const var" is used for pin assignments and other things that
// rarely need to change, but it makes it easy to change them all in one
// line of code
// left switch on the PDF
const int switchPin = 2;
const int led1Pin = 3;
// right switch on the PDF
const int momentarySwitchPin = 4;
const int led2Pin = 5;
// now we start the sketch with setup()
// setup() only runs once. it's where we put all the code we only
// want to run once when the sketch starts.
// a pin can be either "INPUT" or "OUTPUT" and we define that in
// setup() because it will never change while the sketch is running
// (you'll see complex scripts on the Internet that do this in other
// places, but those are written by experienced or crazy Arduino people.)
void setup() {
pinMode (switchPin, INPUT);
pinMode (led1Pin, OUTPUT);
pinMode (momentarySwitchPin, INPUT);
pinMode (led2Pin, OUTPUT);
}
// loop() is where our sketch actually does something. The loop()
// repeats itself from start to finish as long as the Arduino is
// turned on.
void loop() {
// use digitalRead(pin) to find out if the input pins are on
// or off. We use the terms HIGH and LOW because we're
// actually measuring the amount of electricity reaching
// the pin, but for digital, consider it the same as ON and OFF.
// HIGH: pin is ON
// a switch is ON or we want turn ON a LED
// LOW: pin is off
// a switch is OFF or we want to turn OFF a LED
digitalWrite(led2Pin, LOW);
if (digitalRead(momentarySwitchPin) == HIGH) {
digitalWrite(led2Pin, HIGH);
}
if (digitalRead(switchPin) == HIGH) {
digitalWrite(led1Pin, HIGH);
}
else {
digitalWrite(led1Pin, LOW);
}
}
/*
The MIT License (MIT)
Copyright (c) 2014 J. Eric Townsend
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
digital_in_0
read and display status of two switches
0 == LOW == off
1 == HIGH == on
*/
// a line that starts with "//"
/* or text that starts and ends with slash-star and star-slash */
// are called "comment" lines and are ignored by the Arduino
// First, we set up some "variables" that we will use to talk to the
// external hardware.
// A variable is a way of storing information in the Arduino
// that is easy for humans to understand.
// In this sketch we will also use a "const" variable. A const can never
// be changed in the program, but can be changed before you build
// a sketch. A "const var" is used for pin assignments and other things that
// rarely need to change, but it makes it easy to change them all in one
// line of code
// left switch on the PDF
const int switchPin = 2;
const int led1Pin = 3;
// right switch on the PDF
const int momentarySwitchPin = 4;
const int led2Pin = 5;
// now we start the sketch with setup()
// setup() only runs once. it's where we put all the code we only
// want to run once when the sketch starts.
// a pin can be either "INPUT" or "OUTPUT" and we define that in
// setup() because it will never change while the sketch is running
// (you'll see complex scripts on the Internet that do this in other
// places, but those are written by experienced or crazy Arduino people.)
void setup() {
pinMode (switchPin, INPUT);
pinMode (led1Pin, OUTPUT);
pinMode (momentarySwitchPin, INPUT);
pinMode (led2Pin, OUTPUT);
}
// loop() is where our sketch actually does something. The loop()
// repeats itself from start to finish as long as the Arduino is
// turned on.
void loop() {
// use digitalRead(pin) to find out if the input pins are on
// or off. We use the terms HIGH and LOW because we're
// actually measuring the amount of electricity reaching
// the pin, but for digital, consider it the same as ON and OFF.
// HIGH: pin is ON
// a switch is ON or we want turn ON a LED
// LOW: pin is off
// a switch is OFF or we want to turn OFF a LED
digitalWrite(led2Pin, LOW);
if (digitalRead(momentarySwitchPin) == HIGH) {
digitalWrite(led2Pin, HIGH);
}
if (digitalRead(switchPin) == HIGH) {
digitalWrite(led1Pin, HIGH);
}
else {
digitalWrite(led1Pin, LOW);
}
}