User Tools

Site Tools


hstarwiki:cust:gen:arduino

Arduino Setup

Things you need:

  • Arduino
  • Printer USB cable (Type B)
  • Arduino Software

For linux you can run the software from bash or make a script. It is good to do sudo so you have all permissions for ports

  • $arduino

After running the program, you can make or upload scripts. then comile and load

This document

many examples in this document show examples of code. Unless in a code box, the examples will start with $ and follow some of hte principals in the linux page on this wiki.

Coding Notes

IDEA!!!!!: Can write examples of if then (etc) operations of what would hapen if, and so on in the comments.

Basic scripts:

Minimum for a script:

void setup() {                   
 }
void loop() {
 }

Basic Sytax

No Code
/*
 you can write notes that will not be part of the scripte with forward slash star
then star forward slash to end
*/
  • // You can also do just forward slash, forward slash space for a single line

End of line

  • Use semicolen at the end of each line, ex:
  • $pinMode(13, OUTPUT);
Pins:

Unless othersiwse stated, pins are just indicated by numbers such as this indicated pin 13 on the actual board:

  • pinMode(13, OUTPUT);
Other:
  • $delay([milliseconds]);
  • This sets a delay

Advanced Syntax

Cast

Cast is a way of converting one type of data, to another type,

  • such as float to int
  • $([type])[var]
  • type is things like float or int
  • var is any variable or constant
  • Example (to change a float brightness to a int brightness
float bright = sensorValue * (255.0 / 1023.0);
int bint; // a way to define an int name, but give no value
float bflo; // define float name, but give no value
bflo = bright; // sets what value the float is
bint = (int)bflo; // this is the cast directive 
analogWrite(led, bint); // for this example, it would cast the light as an int through a pwm pin and allow it to be an integer between 0-255

Script setup:

Before setup you can define things such as int

void setup

More:http://arduino.cc/en/Reference/Setup. “The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board. ”

Within the void setup brackets {}

Numbers and more

int
  • $int [var] = [x]:
  • This will define an integer with
  • [var]
  • name of integer
  • x
  • Any 16 bit number which is between -32,768 and 32,768.
  • If larger see:
  • Can also make x a sensor value or other things. ex:
  • int sensorvalue = analogRead(A0);
float
  • float [var] = [val];
  • [var]
  • name of float
  • [val]
  • 32bit number meant for decimal point value
long
  • long [var] = [val][L]
  • for ding larger numbers that are 32bit but integers.
  • If doing math with integers, need to have L at end of number
  • ex:
  • long speedOfLight - 186000L

Before operators

const

  • $const [type] [name] = [var]
  • A constant will not change, so you can not use operations to change the int or float

unsigned

  • $unsigned [type] [name] = [var]
  • Unsigned will use the bit available per type in only positive.
  • for example for int, it will used:0 to 65,535
  • instead of -32,768 and 32,768

Modes

  • $pinMode([pin], [configuration]);
  • [pin] an be a number, or if defined int earlier, that int definition
  • [configuration]
  • INPUT
  • Requires very little impedence to take an input
  • OUTPUT
  • Send 40mA of curent
  • Note: it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1k resistors, unless maximum current draw from the pins is required for a particular application. “
  • INPUT_PULLUP
  • This needs more explanation but basically changes the ohm resistance for better? or Varied? inputs
  • $digitalWrite([pin], [value])
  • [pin]
  • number of pin, or constant
  • [value]
  • HIGH
  • sends 5V (3.3V on 3.3V Boards)
  • Low
  • sends 0V

read

  • $digitalRead([pin])
  • will read whatever value is active on [pin], often set is INPUT
  • may need to

read analog

  • $analogRead([pin])
  • Will give an analog read between 0-1023 (10bit)

if else

if ([if formula])
{
 // then do this
}
else {
// then do this
}
  • Do not need else forumla as it will follow after if
  • can do:
else if ([if then formula)
{
// Then do this
}
  • and chain together other paremiters, but be sure to end with basic else
  • Do not need else, but watch out

Operators

  • x == y (x is equal to y) !!! important to use 2 equals
  • x != y (x is not equal to y)
  • x < y (x is less than y)
  • x > y (x is greater than y)
  • x ⇐ y (x is less than or equal to y)
  • x >= y (x is greater than or equal to y)
  • [equation] || [equation] (|| equals OR)
  • ! (equals NOT)
  • && (equals AND)

Serial

  • Some pins are auto serial (kinda like the auto 5V or GND)
  • But you can define a pin with:
  • $Serial.[options]([var]);
  • [options]
  • .begin
  • Will beging sending output via the serial port at whatever baud rate. ex:
  • $Serial.begin(9600)
  • Note: This only initializes the serial communication so you most likely want to put it in the setup area.
  • .println
  • This will send the serial the variablable. Such as:
  • Serial.println([varialbe])
  • Note: the variable here is not a pin, it needs to be something like an actual number, letter (ASCII) or an int defined somewhere else.
  • .print(”[text]“)
  • in this option, it will print text within the parenthesis
  • [baud]
  • The baud rate of the signal. Usually 9600

Readouts

  • $millis()
  • will return the ammount of millisconds since the prgrams has started

Glossery:

  • PWM = Pulse width modulation. Basically an analog modulation variable that sends an analog signal
  • It can set the modulation at 8bit or 0-255. ex:
  • analogWrite(127) which is about half.

last code

hstarwiki/cust/gen/arduino.txt · Last modified: 2017-03-03 T 17:14 (external edit)