EasyManua.ls Logo

Arduino uno

Arduino uno
27 pages
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
27
Arduino Programming Basics
Command
Description
pinMode(n,INPUT)
Set pin n to act as an input. One-time command at top of program.
pinMode(n,OUTPUT)
Set pin n to act as an output
digitalWrite(n,HIGH)
Set pin n to 5V
digitalWrite(n,LOW)
Set pin n to 0V
delay(x)
Pause program for x millisec, x = 0 to 65,535
tone(n,f,d)
Play tone of frequency f Hz for d millisec on speaker attached to pin n
for()
Loop. Example: for (i=0;i<3;i++){} Do the instructions enclosed by {} three times
if (expr) {}
Conditional branch. If expr true, do instructions enclosed by {}
while (expr) {}
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()
{}

Other manuals for Arduino uno

Related product manuals