Category Archives: Microcontrollers

Arduino Programming Quick Reference

Structure
void setup()
void loop()

Control Structures
if (x<7){} else{}
switch (newvar) {
case 1:
break;
case 2:
break;
default:
}
for (int i=0; i <=255; i++){}
while (X<7){}
do {} while (x<7);
continue;                                              //Go to next in do, for, while loop
return x;                                               //Or return; for voids
goto                                                       //potentially harmful

More Syntax
// Single line comment
/* MultiLine comment */
#define COUPLE 2
#include <avr/pgmspace.h>

General Operators
= Assignment Operator
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulo
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less then or equal to
>= Greater than or equal to
&& And
|| Or
! Not

Pointer Access
& Reference operator
* Dereference operator

Bitwise Operators
& Bitwise and
| Bitwise or
^ Bitwise xor
~ Bitwise not
<< Bitshift left
>> Bitshift right

Compound Operators
++ Increment
— decrement
+= compound addition
-= compound subtraction
*= Compound multiplication
/= Compound division
&= Compound bitwise and
|= Compound bitwise or

Constants
HIGH | LOW
INPUT | OUTPUT
true | false
143                                                   //Decimal number
0173                                               //Octal number
0b11011111                            //Binary
0x7B                                              //Hex number
7U                                                  //Force unsigned
10L                                                //Force long
15UL                                            //Force long unsigned
10.0                                               //Forces floating point
2.4e5                                           //240000

Data Types
void
boolean                                                                                //0,1,false,true
char (‘a’ -128 to 127)
unsigned char (0 to 255)
byte (0 to 255)
int (-32,768 to 32,767)
unsigned int (0 to 65,535)
word (0 to 655word (0 to 65,535))
long (-2,147,483,648 to 2,147,483,647)
unsigned long                                                                 //0 to 4,294,967,295
float                                                                                    //-3.4028235E+38 to                                                                                                                   3.4028235E+38
double                                                                              //same as float)
sizeof(newint)                                                            //returns 2 bytes

Strings
char S1[15];
char S2[9]={‘s,’y,’s’,’r’,’e’,’c’,’o’,’n’};
char S#[9]={‘s,’y,’s’,’r’,’e’,’c’,’o’,’n’,’\0′};               //included \0 null termination
char S4[] = “sysrecon”;
char S5[9] = “sysrecon”;
char S6[15] = “sysrecon”;

Arrays
int newInts[6];
int newPins[] = {2,4,8,3,6};
int newSensVals[6] = {2,4,-8,3,2};

Conversions
byte()
char()
float()
int()
long()
word()

Qualifiers
static                                //persists between calls
volatile                           //use RAM
const                               //make read only
PROGMEM               //use flash

Digital I/O
pinMode(pin, [INPUT,OUTPUT])
digitalWrite(pin, value)
int digitalRead(pin)                                        //Write high to inputs to use                                                                                                 pull up res

Analog I/O
analogReference([DEFAULT, INTERNAL, EXTERNAL])
int analogRead(pin)                                    //call twice if switching pins from                                                                                       high Z source
analogWrite(pin, value)                          //PWM

Advanced I/O
tone(pin, freqhz)
tone(pin, feqhz, duration_ms)
noTone(pin)
shiftOut(dataPin, clockPin, [MSBFIRST, LSBFIRST], value)
unsigned long pulseIn(pin,[HIGH, LOW])

Time
unsigned long mills()                               //50 days overflow
unsigned long micros()                         //70 min overflow
delay(ms)
delayMicroseconds(us)

Math
min(x,y)
max(x,y)
abs(x)
constrain(x, minval, maxval)
map(val, fromL, fromH, toL, toH)
pow(base, exponent)
sqrt(x)
sin(rad)
cos(rad)
tan(rad)

Random Numbers
randomSeed(seed)                               //Long or int
long random(max)
long random(min,max)

Bits and Bytes
lowByte()
highByte()
bitRead(x,bitn)
bitWrite(x,bitn,bit)
bitSet(x,bitn)
bitClear(x,bitn)
bit(bitn)                                                   //bitn: 0-LSB 7-MSB

External Interrupts
attachInterrupt(interupt, function, [LOW,CHANGE,RISING,FALLING])
detachInterrupt(interrupt)
interrupts()
noInterrupts()

Libraries
Serial.
begin([300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200])
end()
int available()
int read()
flush()
print()
printIn()
write()

EEPROM (#include <EEPROM.h>)
byte read(intAddr)
write(intAddr, newByte)

Servo (#include <Servo.h>)
attach(pin, [min_uS, max_uS])
write(angle)                                                           //0-180
writeMicroseconds(uS)                                //1000 – 2000, 1500 is midpoint
read()                                                                       //0-180
attached()                                                            //Returns boolean
detach()

SoftwareSerial (#include <SoftwareSerial.h>)
SoftwareSerial(RxPin, TxPin)
begin(longSpeed)                                           //up to 9600
char read()                                                          //blocks till data
print(newData)                                              //or printLn(newData)

Wire (#include <Wire.h>) //For I2C
begin()                                                                //Join as master
begin(addr)                                                     //Join as slave @ addr
requestFrom(addr, count)
beginTransmission(addr)                       //Step 1
send(newByte)                                             //Step 2
send(char * newString)
send(byte * data, size)
endTransmission()                                    //Step 3
byte available()                                           //Num of bytes
byte receive()                                               //Return next byte
onReceive(handler)
onRequest(handler)

What Is An Arduino?

The Arduino was introduced in 2005 by founders Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, and David Mellis. The Arduino is not really the board itself or the microprocessor on the board. The Arduino is a hardware/software platform that was designed to provide an inexpensive and easy way for hobbyists, students and professionals to create devices that interact with their environment. The Arduino platform is completely open hardware and open source software.

Arduino boards can be purchased pre-assembled or as do-it-yourself kits. The hardware design information is available for those who would like to assemble an Arduino by hand. The current prices of Arduino boards run around $20-$30 on SparkFun and clones as low as $9 on Amazon

The Arduino platform is a single-board microcontroller designed around an 8-bit Atmel AVR microcontroller, or a 32-bit Atmel ARM. Official Arduinos have used the megaAVR series of chips, specifically the ATmega8, ATmega168, ATmega328, ATmega1280, and ATmega2560. Most boards include a 5 volt linear regulator and a 16 MHz crystal oscillator.

The Arduino platfrom feature a USB interface, 6 analog input pins, as well as 14 digital I/O pins (six of which can produce pulse-width modulated signals) which allows the user to attach various interchangeable add-on modules known as shields or basic breadboards. Many shields are individually addressable via an I²C serial bus, allowing many shields to be stacked and used in parallel. Shields or jumper wires to a breadboard are plugged into the top of the board, via female 0.10-inch (2.5 mm) headers. The Arduino has a number of facilities for communicating with a computer, another Arduino, or other microcontrollers

The Arduino’s microcontroller is also pre-programmed with a boot loader that simplifies uploading of programs to the on-chip flash memory, compared with other devices that typically need an external programmer. Arduino boards are programmed via USB over an RS-232 serial connection implemented using USB-to-serial adapter chips such as the FTDI FT232

The Arduino Platform comes with a simple down-loadable, cross-platform, integrated development environment (IDE) written in JAVA and allows users to write programs for Arduino using C or C++. The code you write is C/C++ syntax but not a valid C/C++ program. An extra include header at the top and a very simple main() function at the bottom, to make it a valid C++ program. The IDE is derived from the IDE for the Processing programming language and the Wiring development platform and contains a C/C++ library called “Wiring”. Wiring was based on the original work done on Processing project in MIT. The Wiring IDE uses the GNU toolchain and AVR Libc to compile programs, and uses avrdude to upload programs to the board. You can program the arduino in standard C using avrstudio and upload with avrdude.

Programming in the Arduino IDE is as simple as creating 2 functions()
1. setup() – a function run once at start up that can be used to define initial environment settings
2. loop() – a function called repeatedly until the board is powered off

A simple program to blink the Arduino LED

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}


We have hundreds of tutorials in mind for the Arduino Developemnt Platform. We will begin to produce them soon so keep checking back.

For more information right now, check out the offical Arduino page at https://arduino.cc be sure to check out the Arduino playground which has tons of information to get you started.