|
- Setting up I/O -
Set pins as inputs or outputs individually,
pinMode(7, OUTPUT); // set digital pin 7 as output
pinMode(5, INPUT); // set digital pin 5 as input
pinMode(5, INPUT_PULLUP); // set pin 5 as input, using pull-up resistor
|
- Setting Pin voltages -
digitalWrite(7, HIGH); // 5 volts to pin 7
digitalWrite(6, LOW); // 0 volts to pin 6
| |
- Test a switch input -
if(digitalRead(5) == 0) {
// Do something if pin low
}
else { // else if pin is high
// Do something else
}
Note: the structure If( ) is testing for boolean 'not zero'//-- Another example (2 switches):
if(digitalRead(5) == 0 && digitalRead(6) == 1)
{
//-- Do something if 1st switch is closed and 2nd is open
}
- Delay - delay(milliseconds);
delayMicroseconds(microseconds);
Note: arguments should be unsigned integers (unsigned int)
|
- Analog Input -
For reading 0-5volts (variable) input. -This usually involves setting up a voltage divider between +5v, ground, and the input pin. Usage:
// To read 10-bit analog value on pin 5, and store in "a_in":
int a_in = analogRead(A5);
Example:
void loop() { // repeat forever
lcd_clear(); // clear LCD screen
a_in = analogRead(A5); // get 10 bit analog value, store it
a_in = map(a_in, 0, 1023, 0, 255); // re-map 10-bit number to 8-bit
lcd_decimal(FIRST_LINE,a_in); // write value to LCD
delay(50); // let image stay on screen
}
| |
|
- LCD Display -
For Parallel (standard) LCD screen: #include <LiquidCrystal.h>
// initialize with the numbers of the interface pins to use
// (RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(14, 13, 9, 10, 11, 12);
void setup() {
// set up the LCD's number of columns and rows
lcd.begin(16, 2);
// set the cursor to column 0, line 0 (default)
lcd.setCursor(0, 0); // column, row
}
//Example usage in loop():
lcd.clear();
lcd_print(analog_data, 10, 3, 4);
For Serial LCD screen: #include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<- Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
lcd.begin (16,2); // <-- set LCD columns and rows
// LCD Backlight ON
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home on LCD
lcd.print("Hello, world.");
}
void loop() {
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("1st Line");
lcd.setCursor (0,1); // go 1st position, second line
lcd.print("Second line");
}
|
- Looping Structures -
"while" loop: while(r < 12) { // Repeats, assigning
r = analogRead(A5); // the value on analog pin 5
} // to a variable called "r".
while(analogRead(A5) > 69) { // As long as analog pin 5 > 69,
digitalWrite(7, HIGH); // set digital pin 7 high
}
"for" loop: for(int i = 0; i < 100; i++) { // Repeats 100 times
digitalWrite(6, HIGH); // 5 volts to digital pin 6
delayMicroseconds(200); // delay for 200 microseconds
digitalWrite(6, LOW); // 0 volts to pin D6
delayMicroseconds(200); // delay for 200 microseconds
}
- Booleans - (a == 1) // true if a is equal to 1
(a != 1) // true if a is NOT equal to 1
(a <= 1) // true if a is less than or equal to 1
(a >= 1) // true if a is greater than or equal to 1
(a < 10 && a > 5) // true if a < 10 AND a > 5
(b < 10 || b > 5) // true if b < 10 OR b > 5
((a > 5) && !(b == 10)) // true if a is greater than 5 and
// b is not equal to 10
|