Arduino Examples Main Program #include … // vars declaration void setup(){ //initialisation instructions } void loop(){ //main program } Read digital value from a PIN int inPin = 7; void setup(){ pinMode(inPin, INPUT); } void loop(){ boolean val = digitalRead(inPin); } Write digital value to a PIN Int outPin = 13; void setup(){ pinMode(outPin, OUTPUT); } void loop(){ digitalWrite(outPin, HIGH); delay(1000); digitalWrite(outPin, LOW); delay(1000); } Read analog value from a PIN Maps Input from 0V–5V to 0-1023 int analogPin = 7; int val; void setup(){ pinMode(inPin, INPUT); Serial.begin(9600); } void loop(){ val = analogRead(analogPin); Serial.println(”Val : ” + val); } PWM - Pulse Wave Modulation int ledPin = 9; void setup(){ pinMode(ledPin, OUTPUT); } void loop(){ // set duty cycle 0-255 @ 490 Hz analogWrite(ledPin, 128); } Receive bytes from Serial int incomingByte = 0; void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { incomingByte = Serial.read(); Serial.print("I received: "); Serial.println(incomingByte, DEC); } } Tone Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. tone(pin, frequency) tone(pin, frequency, duration) noTone() pin: the pin on which to generate the tone frequency: the frequency of the tone in hertz - unsigned int duration: the duration of the tone in milliseconds - unsigned long Metro #include <Metro.h> //Include Metro library #define LED 13 // Define the led's pin int state = HIGH; Metro ledMetro_1 = Metro(5000); Metro ledMetro_2 = Metro(10000); void setup(){ Serial.begin(9600); } void loop(){ if (ledMetro_1.check() == 1) { Serial.println("accendo led 1"); delay(200); Serial.println("spengo led 1"); } if (ledMetro_2.check() == 1) { Serial.println("accendo led 2"); delay(200); Serial.println("spengo led 2"); } } Frequent Conversions Convert long to char* char* buffer[12]; long value = 1234; itoa(value,buffer,10); Convert String to long String number = "1234"; Serial.println(number.toInt()); Software Serial #include <SoftwareSerial.h> #define rxPin 4 #define txPin 5 SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); void setup(){ pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); Serial.begin(9600); mySerial.begin(9600); } void loop(){ if(mySerial.available() != 0){ char someChar = mySerial.read(); Serial.println(someChar); } } Piezo Sensor Piezo Sensor const int ledPin = 13; const int sensorPin = 0; const int THRESHOLD = 100; void setup() { pinMode(ledPin, OUTPUT); } void loop() { int val = analogRead(sensorPin); if(val>=THRESHOLD) { digitalWrite(ledPin, HIGH); delay(100); } else digitalWrite(ledPin, LOW); } Temperature Sensor (LM35) Temperature Sensor (LM35) const int sensorPin = A0; void setup(){ Serial.begin(9600); } void loop(){ int value = analogRead(sensorPin); float millivolts = (value/1024.0) * 5000; float celsius = millivolts/10; Serial.print(celsius); Serial.println("°C"); delay(1000); } Analog Distance Sensor Photo Resistor Vout 5V A0 GND Arduino Sender/Receiver 4 5 Sender Receiver Sender Code //Sender #include <SoftwareSerial.h> SoftwareSerial mySerial(4,5); //Rx,Tx void setup(){ Serial.begin(9600); mySerial.begin(9600); } void loop(){ if(Serial.available() > 0) mySerial.write(Serial.read()); } Receiver Code //Receiver #include <SoftwareSerial.h> SoftwareSerial mySerial(4,5); //Rx,Tx void setup(){ Serial.begin(9600); mySerial.begin(9600); } void loop(){ if(mySerial.available() > 0) Serial.write(mySerial.read()); } Interrupts • interrupts() • noInterrupts() • attachInterrupt(interrupt, function, mode) – Interrupt: 0=PIN2, 1=PIN3 – Function: callback – Mode: – LOW – CHANGE – RISING – FALLING Debouncing const int inputPin = 2; // the number of the input pin const int ledPin = 13; // the number of the output pin const int debounceDelay = 10; // milliseconds to wait until stable boolean debounce(int pin) { boolean state; boolean previousState; previousState = digitalRead(pin); // store switch state for(int counter=0; counter < debounceDelay; counter++) { delay(1); // wait for 1 millisecond state = digitalRead(pin); // read the pin if( state != previousState){ counter = 0; // reset the counter if the state changes previousState = state; // and save the current state } } return state; } Ethernet Server 1/2 #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1, 177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { Serial.begin(9600); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } Ethernet Server 2/2 void loop() { EthernetClient client = server.available(); if (client) { Serial.println("new client"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); if (c == '\n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connnection: close"); client.println(); client.println(“Body...”); break; } if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; } } } delay(1); client.stop(); Serial.println("client disonnected"); } Ethernet Client #include <Ethernet.h> #include <SPI.h> byte arduino_mac[] byte arduino_ip[] byte server_ip[] int server_port = = = = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; { 192, 168, 1, 120 }; { 192, 168, 1, 130 }; 1984; EthernetClient client; String pattern = "GET /?temp=$temp HTTP/1.0"; String getTemp(){ return String(analogRead(A0)); } void setup(){ Serial.begin(9600); Ethernet.begin(arduino_mac, arduino_ip); } void loop(){ if (client.connect(server_ip, server_port)){ pattern.replace("$temp",getTemp()); client.println(pattern); client.println(); //required by the standard client.stop(); } delay(5000); } RFID Reader 1/3 RFID Reader 2/3 #include <SoftwareSerial.h> const const const const int int int int startByte = 10; // ASCII line feed precedes each tag endByte = 13; // ASCII carriage return terminates each tag tagLength = 10; // the number of digits in tag totalLength = tagLength + 2; //Inizializzare la SoftwareSerial sui PIN 4,5 char tag[tagLength + 1]; // holds the tag and a terminating null int bytesread = 0; void setup() { //Settare la velocità delle due seriali a 9600 pinMode(2,OUTPUT); // connected to the RFID ENABLE pin digitalWrite(2, LOW); // enable the RFID reader } RFID Reader 3/3 void loop() { if(mySerial.available() >= totalLength) { if(mySerial.read() == startByte) { bytesread = 0; // start of tag so reset count to 0 while(bytesread < tagLength) // read 10 digit code { int val = mySerial.read(); if((val == startByte)||(val == endByte)) break; tag[bytesread] = val; bytesread = bytesread + 1; // ready to read next digit } if(mySerial.read() == endByte) { tag[bytesread] = 0; // terminate the string Serial.print("RFID tag is: "); Serial.println(tag); } } } } Bluetooth Adapter TX GND RX 5V Bluetooth Adapter • SparkFun BlueSmirf Bluetooth Adapter – Serial transmitter/receiver – Baud rate: 115200 baud – Datasheet: http://www.sparkfun.com/datasheets/RF/BlueSMiRF_v 1.pdf • Android Test App – GetBlue (from Google Play) – Manual pair, passkey: 1234 Using Bluetooth Adapter Using Bluetooth Adapter #include <SoftwareSerial.h> const int rxpin = 2; Const int txpin = 3; SoftwareSerial bluetooth(rxpin, txpin); void setup(){ Serial.begin(9600); bluetooth.begin(9600); // initialize the software serial port bluetooth.println("Bluetooth ready"); } void loop(){ if (bluetooth.available()){ char c = (char)bluetooth.read(); Serial.write(c); } if (Serial.available()) { char c = (char)Serial.read(); bluetooth.write(c); } } Serial.println("Serial ready"); Lettore / Scrittore A1 Scrittore 2 10 5 4 10 2 A2 Lettore Scrittore Scrive su un buffer 5 byte Avvisa il lettore che il buffer è pronto per la lettura Attende che il lettore finisca di leggere tutti I byte Lettore Attende che lo scrittore finisca di scrivere tutti I byte Legge tutti I byte Avvisa lo scrittore dell’avvenuta lettura Scrittore 1/2 #include <SoftwareSerial.h> #define rxPin 4 #define txPin 5 int signalPIN = 10; byte array[5] = {123, 100, 34, 55, 18}; boolean canWriteBool = true; SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); void canWrite(){ Serial.println("Posso scrivere"); canWriteBool = true; } void setup(){ Serial.begin(9600); mySerial.begin(9600); pinMode(signalPIN, OUTPUT); digitalWrite(signalPIN, LOW); pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); attachInterrupt(0, canWrite, RISING); } Scrittore 2/2 void loop(){ while(!canWriteBool); for(int i=0; i<5; i++){ Serial.println("Scrivo "+(String)+array[i]); } mySerial.write(array, 5); canWriteBool = false; digitalWrite(signalPIN, LOW); digitalWrite(signalPIN, HIGH); digitalWrite(signalPIN, LOW); delay(3000); } Lettore 1/2 #include <SoftwareSerial.h> #define rxPin 4 #define txPin 5 int signalPIN = 10; SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin); void canRead(){ Serial.println("Interrupt"); while(mySerial.available() != 0) { Serial.println(mySerial.read()); } digitalWrite(signalPIN, LOW); digitalWrite(signalPIN, HIGH); digitalWrite(signalPIN, LOW); } Lettore 2/2 void setup(){ Serial.begin(9600); mySerial.begin(9600); pinMode(signalPIN, OUTPUT); digitalWrite(signalPIN, LOW); pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); attachInterrupt(0, canRead, RISING); } void loop(){ } References • Official site : www.arduino.cc • Book : Arduino cookbook – w2.homelinux.org/mediawiki/images/0/0 7/Arduino_cookbook.pdf