A slower look at how Arduino programs control pins and report what they are doing
32-Lesson Course • Semester 1
pinMode() to set up a pin correctlydigitalWrite()Serial.print() and Serial.println() correctly++ and --Arduino pins are where code meets the real world. Some pins bring information in, and some pins send information out.
A pin does not automatically know whether it should behave as an input or an output. We usually set that job in setup() using pinMode().
pinMode(13, OUTPUT); means pin 13 will control somethingpinMode(2, INPUT); means pin 2 will read somethingpinMode(2, INPUT_PULLUP); is often used with buttonsint ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
} int ledPin = 13; stores the pin number in a variablepinMode(ledPin, OUTPUT); prepares the pin to send signals outdigitalWrite(ledPin, HIGH); turns the output ondigitalWrite(ledPin, LOW); turns the output offdelay(1000); waits 1 secondSerial.print() and Serial.println()?Serial.print() writes text and stays on the same lineSerial.println() writes text and then moves to the next linevoid setup() {
Serial.begin(9600);
Serial.print("Count = ");
Serial.println(5);
Serial.print("Ready");
Serial.print("...");
Serial.println(" go!");
}
void loop() {
} Variables are named storage boxes inside a program. Different variable types are used for different kinds of data.
Whole numbers like 0, 7, and 100
Decimal numbers like 3.14 or 22.5
A single character like 'A'
A true-or-false value
int blinkCount = 0;
float waitTime = 1.5;
char grade = 'A';
bool lightOn = false; count++; adds 1 to a variablecount--; subtracts 1 from a variableint count = 3;
void setup() {
Serial.begin(9600);
Serial.println(count);
count++;
Serial.println(count);
count--;
Serial.println(count);
}
void loop() {
} if and elseSometimes a program should only do something when a condition is true. That is what a conditional statement does.
if means “only do this when the condition is true”else means “otherwise, do this instead”==, <, or >= is not the same as ==x = 0 means “store 0 in x”x == 0 means “check whether x is equal to 0”if statement, you usually want == because you are testing a value() and do?() hold the condition or information being passed in hold the block of code that belongs togetherif (score == 7) { ... }, the parentheses contain the test, and the braces contain the code to run if the test is true; do?int score = 7; or Serial.begin(9600); need semicolonsif (...) or void setup(), do not use a semicolon before the opening brace//int score = 7;
void setup() {
// Start serial communication
Serial.begin(9600);
if (score == 7) {
Serial.println("Score is 7");
} else {
Serial.println("Score is not 7");
}
}
void loop() {
} score == 7 asks whether the value in score is equal to 7else message prints insteadif (score = 7) would mean something very different, so be careful// Start serial communication is a comment for the human reader, not for the computerThis example combines digital output, Serial Monitor messages, and a variable that changes each time the loop runs.
int ledPin = 13;
int blinkCount = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Blink program starting");
}
void loop() {
digitalWrite(ledPin, HIGH);
Serial.print("Blink number: ");
Serial.println(blinkCount);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
blinkCount++;
if (blinkCount == 10) {
Serial.println("Reached 10 blinks. Resetting to 0.");
blinkCount = 0;
}
} setup() prepares the pin and starts serial communicationloop() repeats the blinking pattern again and againblinkCount keeps track of how many times the loop has blinkedSerial.print() and Serial.println() make a readable messageif statement checks when the count reaches 10500 to 1000.10 to 5.Serial.println() message when the LED turns off.blinkCount++; with blinkCount = blinkCount + 1; and explain why both work.Add a second variable called flashTotal that keeps counting even when blinkCount resets.
Create a short countdown using count--; and print each number to the Serial Monitor.
pinMode() usually placed in setup()?pinMode() and digitalWrite() correctlySerial.print() and Serial.println()++ or --Serial.print() and one using Serial.println()Take your blink-and-count program from class and add these features:
Experiment with the Serial Monitor and document:
Serial.print()Serial.println()++--