Review Prog Arduino

Telechargé par electro-ub ub
Review of Programming the Arduino
BJ Furman | ME 30 Computer Applications | Review of Programming the Arduino.doc | 14APR2010
Structure of a program for the Arduino
User-defined functions
à Functions called in loop
4
void loop( )
à Code and actions that
you want repeated
forever
3
void setup( )
à Set pin modes:
INPUTS
OUTPUTS
à Turn on pullup resistors
à Set up serial monitor
à Begin serial monitor
2
#include
#define
constants, etc.
1
/* Programmer’s Block
* 14APR2010 */
#include ir.h
#define USECS_PERSEC 1000000
const int DAH_TIME = 700;
const byte ledRGBRedPin = 3;
void setup()
{
pinMode(ledRGBRedPin, OUTPUT);
}
void loop()
{
lines of code to repeat;
}
void user_function_1( )
{
lines of code for user_function_1;
}
int user_function_2( int var1)
{
lines of code for user_function_2;
}
Page 1 of 2
Review of Programming the Arduino
/* SOS - blinks the Morse code SOS on red RGB LED
* on the Spartronics Experimenter board. Adapted from
http://www.uchobby.com/index.php/2009/02/01/mcu-programming-
intro-series-1-sos-on-the-arduino/
* 14APR2010 */
const byte ledRGBPin = 3; // red RGB LED on digital pin 3
const int DIT_TIME = 300;
const int DAH_TIME = 700;
const int WORD_DELAY = 500;
const int CHAR_SPACE = 200;
void setup()
{
// initialize the digital pin as an output:
pinMode(ledRGBPin, OUTPUT);
}
void loop()
{
morse_S( );
morse_O( );
morse_S( );
delay(WORD_DELAY);
}
void morse_S( )
{
for(int i=0; i<3; i++){
digitalWrite(ledRGBPin, HIGH);
delay(DIT_TIME);
digitalWrite(ledRGBPin, LOW);
delay(CHAR_SPACE);
}
delay(CHAR_SPACE);
}
void morse_O( )
{
for(int i=0; i<3; i++){
digitalWrite(ledRGBPin, HIGH);
delay(DAH_TIME);
digitalWrite(ledRGBPin, LOW);
delay(CHAR_SPACE);
}
delay(CHAR_SPACE);
}
A ‘sketch must have:
o setup( )
o loop( )
#include
#define
constants, etc.
In setup( ):
Setting pin modes:
o INPUT for pins that will be read
to take information IN to the
microcontroller
o Writing a HIGH to a pin
declared as an INPUT
turns on its pullup resistor
o OUTPUT for pins that will be
written to send information OUT
of the microcontroller
In loop( ):
What gets done continuously
User functions
BJ Furman | ME 30 Computer Applications | Review of Programming the Arduino.doc | 14APR2010 Page 2 of 2
1 / 2 100%

Review Prog Arduino

Telechargé par electro-ub ub
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !