0% found this document useful (0 votes)
176 views

Arduino Programming Summary PDF

This document provides an overview of basic Arduino programming commands including pinMode, digitalWrite, delay, tone, for loops, if/else statements, and while loops. It describes what each command does and provides examples of blinking an LED, flashing an LED at 1Hz, turning on a motor for 1 second, playing a tone for 1 second, and turning on an LED for 1 second when a switch is pressed. The setup() function contains initialization code run once and loop() contains the main program code run indefinitely.

Uploaded by

Andra Radu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

Arduino Programming Summary PDF

This document provides an overview of basic Arduino programming commands including pinMode, digitalWrite, delay, tone, for loops, if/else statements, and while loops. It describes what each command does and provides examples of blinking an LED, flashing an LED at 1Hz, turning on a motor for 1 second, playing a tone for 1 second, and turning on an LED for 1 second when a switch is pressed. The setup() function contains initialization code run once and loop() contains the main program code run indefinitely.

Uploaded by

Andra Radu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Arduino Programming Basics

Command pinMode(n,INPUT) pinMode(n,OUTPUT) digitalWrite(n,HIGH) digitalWrite(n,LOW) delay(x) tone(n,f,d) for() if (expr) {} while (expr) {} Description Set pin n to act as an input. One-time command at top of program. Set pin n to act as an output Set pin n to 5V Set pin n to 0V Pause program for x millisec, x = 0 to 65,535 Play tone of frequency f Hz for d millisec on speaker attached to pin n Loop. Example: for (i=0;i<3;i++){} Do the instructions enclosed by {} three times Conditional branch. If expr true, do instructions enclosed by {} While expr is true, repeat instructions in {} indefinitely

For more commands see the ME2011 Arduino Microcontroller Guide and the Language Reference section of the arduino web site. Instructions in the setup() function are executed once. Those in the loop() function are executed indefinitely.

Examples
1. Turn on LED connected to Pin 2 for 1 s. void setup() { pinMode(2,OUTPUT); digitalWrite(2,HIGH); delay(1000); digitalWrite(2,LOW); } void loop() {} 2. Flash LED connected to Pin 2 at 1 Hz forever. void setup() { pinMode(2,OUTPUT); } void loop() { digitalWrite(2,HIGH); delay(500); digitalWrite(2,LOW); delay(500); } 3. Turn on motor connected to Pin 4 for 1 s. void setup() { pinMode(4,OUTPUT); digitalWrite(4,HIGH); delay(1000); digitalWrite(4,LOW); } void loop() {} 4. Play 440 hz tone for one second on speaker connected to pin 5. Delay is needed because the program does not wait for the tone() command to finish but rather immediately goes to the command following tone(). void setup() { pinMode(5,OUTPUT); tone(5,440,1000); delay(1100); } void loop() {} 5. LED is on Pin 2 and switch is on Pin 6. Turns on the LED for one sec when switch is pressed. void setup() { pinMode(2,OUTPUT); pinMode(6,INPUT); while (digitalRead(6) == HIGH) ; digitalWrite(2,HIGH); delay(1000); digitalWrite(2,LOW); } void loop() {}

You might also like