Download presentation
Presentation is loading. Please wait.
1
Programming 2: The Arduino IDE & First Sketches
EGR 328 January 31, 2019
2
Overview The Arduino development environment Uploading sketches
The swap function Using global variables Using local variables with values passed in Using Functions Using Functions with Circuits A first look at using switches Photoresistor Binary data – sending and receiving
3
Recap: Arduino Programming
/* Header comments should include the file/sketch name, a short description, your (author) name and the date */ void setup() { pinMode(13, OUTPUT); } void loop() digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); This setup() function is executed only once
4
Recap: Arduino Programming
/* Header comments should include the file/sketch name, a short description, your (author) name and the date */ void setup() { pinMode(13, OUTPUT); } void loop() digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); The loop() function is executed over, and over, forever
5
Constant Declaration // blink.pde with constant declared for LEDpin const int LEDpin = 13; void setup() { pinMode(LEDpin, OUTPUT); } void loop() digitalWrite(LEDpin, HIGH); delay(500); digitalWrite(LEDpin, LOW);
6
Constant Declaration // blink.pde with compiler directive for LEDpin #define LEDpin 13 void setup() { pinMode(LEDpin, OUTPUT); } void loop() digitalWrite(LEDpin, HIGH); delay(500); digitalWrite(LEDpin, LOW);
7
The Arduino IDE
8
Uploading Sketches …
9
The Serial Monitor
10
Using the Serial Monitor for Strings
// Strings and Serial Monitor void setup() { Serial.begin(9600); Serial.println("Hello World!"); } void loop() {
11
Using the Serial Monitor for Strings
int counter = 1; void setup(){ Serial.begin(9600); } void loop(){ Serial.println("Hello World"); Serial.print("Counter = "); Serial.println(counter); delay(1000); counter++;
12
Using the Serial Monitor for Strings
// Strings and Serial Monitor char message[ ] = "Hello World”; void setup() { Serial.begin(9600); // Initialize serial monitor rate } void loop() Serial.println(message); delay(1000);
13
// Example 2: Turn on LED while button is pressed
const int LED = 13; // Connect the LED to pin 13 const int BUTTON = 7; // Connect the power lead for the // pushbutton to pin 7 int val = 0; // store the state of the button void setup() { pinMode(LED, OUTPUT); // tell Arduino LED pin is an output pinMode(BUTTON, INPUT); // tell Arduino BUTTON is an input } void loop(){ val = digitalRead(BUTTON); // read voltage at the button // check whether the input is HIGH (button pressed) if (val == HIGH) { digitalWrite(LED, HIGH); // turn LED ON (5V at output) else { digitalWrite(LED, LOW); // turn LED off (0V at output)
14
Other colors for other connections
What is the purpose of the resistor? Other colors for other connections Color Code Wires Black for Ground Red for Power
15
Ohm’s Law Voltage Current Resistance
16
pinMode() Use for every pin using, to let Arduino know how to configure the pin to use with your electronics, your circuit Define as either INPUT or OUTPUT Use all caps pinMode( outPin, OUTPUT); pinMode( inPin, INPUT);
17
Examples from “Getting Started with Arduino”
Example 01: Simple LED Blink Example 02: Push button control of LED Example 03a: Keep LED on after button released Example 03b: improved from 03a Example 03c: improved from 03a and 03b
18
Compare this sketch to the following one: What is the
int x; int y; void setup() { Serial.begin(9600); } void loop() { x = random(10); y = random(10); Serial.print("The value of x and y before swapping are: "); Serial.print(x); Serial.print(","); Serial.println(y); swap(); delay(1000); Serial.print("The value of x and y after swapping are: "); Serial.print(x); Serial.print(","); Serial.println(y);Serial.println(); delay(1000); void swap(){ int temp; temp = x; x = y; y = temp; Compare this sketch to the following one: What is the difference in the declaration and use of the variables that are swapped?
19
void swap(int &value1, int &value2); void setup() {
Serial.begin(9600); } void loop() { int x = random(10); int y = random(10); Serial.print("The value of x and y before swapping are: "); Serial.print(x); Serial.print(","); Serial.println(y); swap(x,y); delay (1000); Serial.print("The value of x and y after swapping are: "); Serial.print(x); Serial.print(","); Serial.println(y); Serial.println(); delay (1000); void swap(int &value1, int &value2) { int temp; temp = value1; value1 = value2; value2 = temp;
20
Write a Program for this Function
int sensorPercent(int pin) { int percent; val = analogRead(pin); percent = map(val,0,1023,0,100); return percent; }
21
Note the use of the function, parameter passed and the return value.
void loop() { int count = blink3(250); Serial.print("The number of times ____________ "); Serial.println(count); } int blink3(int period) { int result = 0; int switchVal = HIGH; while(switchVal == HIGH){ digitalWrite(13,HIGH); delay(period); digitalWrite(13,LOW); result++; switchVal = digitalRead(inputPin); return result; Note the use of the function, parameter passed and the return value.
22
Arduino Libraries: Ex 1 Serial Communication Very Useful for Debugging
Serial.begin(9600) Serial.print() Serial.println() Serial.available()
23
Photoresistor Circuit
Use a photoresistor, in a circuit and a sketch For a sensor circuit… That prints the sensor data value and... The voltage level induced at the sensor input pin
24
Photoresistor Circuit
5 V 10 kΩ photoresistor analog input pin
25
Photoresistor Sketch int sensorVal; const int sensorPin = 4; float voltage; const float input2volts = 5.0/1023.0; // 5V, 10 bits void setup() { Serial.begin(9600); } void loop() { sensorVal = analogRead( sensorPin ); voltage = float(sensorVal)*input2volts; Serial.print("SensorVal, Voltage = "); Serial.print(sensorVal); Serial.print(" "); Serial.println(voltage); delay(500);
26
Serial.write() function
Description from arduino.cc Writes binary data to the serial port. The data is sent as a byte or series of bytes To send the characters representing the digits of a number use the .print() function instead. Syntax Serial.write(val) Serial.write(str) Serial.write(buf, len) Parameters val: a value to send as a single byte str: a string to send as a series of bytes buf: an array to send as a series of bytes len: the length of the buffer Returns the number of bytes written, though reading that number is optional
27
Serial.print() Prints data to the serial port as human-readable ASCII text (description from arduino.cc) Numbers are printed using an ASCII character for each digit. Bytes are sent as a single character. Characters and strings are sent as is. Serial.print(78) sends "78" Serial.print("Hello world.") sends "Hello world." Serial.print(78, BIN) sends " " To send a single byte, use Serial.write() This information is from
28
Integer Math – Beware! // sketch to practice with the serial monitor; & math void setup() { Serial.begin(9600); // Setup communication int degC = 20; int degF; degF = degC * 9 / ; // integer math Serial.println(degF); // Print to monitor } void loop() {}
29
Floating Point Math // sketch to practice with the serial monitor; & math void setup() { Serial.begin(9600); // Setup communication float degC = 20; float degF; degF = degC * 9.0 / ; // floating point math Serial.println(degF); // Print to monitor } void loop() {}
30
Your Programs Begin writing pseudocode for your first sensor programs
Comment everything clearly and concisely What tasks, aka functions, will you use What variables do you need in each What is the input/output for each Write the main() [loop() ] function
31
-- On Moodle --
32
(Good reference for C) -- On Moodle –
33
-- On Moodle --
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.