-Andrea's Sketchbook
+Sketchbook di Andrea
=====================
Esempi per i corsi su Arduino.
- http://git.andreamanni.com/
- Interfaccia web: http://git.andreamanni.com/web
+Gestione
+--------------------
+
Per aggiornare il proprio archivio ::
cd sketchbook_andrea/ ; git fetch
rm -r sketchbook_andrea ; git clone --depth 1 git://git.andreamanni.com/sketchbook_andrea
Download via HTTP
-==================
+--------------------
L'archivio e' comunque disponibile per un download via HTTP:
http://git.andreamanni.com/web?p=sketchbook_andrea;a=snapshot;h=HEAD;sf=tgz
--- /dev/null
+/*
+ Reading a serial ASCII-encoded string.
+
+ This sketch demonstrates the Serial parseInt() function.
+ It looks for an ASCII string of comma-separated values.
+ It parses them into ints, and uses those to fade an RGB LED.
+
+ Circuit: Common-anode RGB LED wired like so:
+ * Red cathode: digital pin 3
+ * Green cathode: digital pin 5
+ * blue cathode: digital pin 6
+ * anode: +5V
+
+ created 13 Apr 2012
+ by Tom Igoe
+
+ This example code is in the public domain.
+ */
+
+// pins for the LEDs:
+const int redPin = 3;
+const int greenPin = 5;
+const int bluePin = 6;
+
+void setup() {
+ // initialize serial:
+ Serial.begin(9600);
+ // make the pins outputs:
+ pinMode(redPin, OUTPUT);
+ pinMode(greenPin, OUTPUT);
+ pinMode(bluePin, OUTPUT);
+
+}
+
+void loop() {
+ // if there's any serial available, read it:
+ while (Serial.available() > 0) {
+
+ // look for the next valid integer in the incoming serial stream:
+ int red = Serial.parseInt();
+ // do it again:
+ int green = Serial.parseInt();
+ // do it again:
+ int blue = Serial.parseInt();
+
+ // look for the newline. That's the end of your
+ // sentence:
+ if (Serial.read() == '\n') {
+ // constrain the values to 0 - 255 and invert
+ // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
+ red = 255 - constrain(red, 0, 255);
+ green = 255 - constrain(green, 0, 255);
+ blue = 255 - constrain(blue, 0, 255);
+
+ // fade the red, green, and blue legs of the LED:
+ analogWrite(redPin, red);
+ analogWrite(greenPin, green);
+ analogWrite(bluePin, blue);
+
+ // print the three numbers in one string as hexadecimal:
+ Serial.print(red, HEX);
+ Serial.print(green, HEX);
+ Serial.println(blue, HEX);
+ }
+ }
+}
--- /dev/null
+Esercizi di base
+================
+
+
+Gli esercizi di base sono divisi in argomenti:
+
+- Output digitali: Blinks
+- Input digitali: buttons
+- Input analogici: analog input
+- Output analogici: PWM
+
+In piu' c'e' una parte per le il debug seriale.
--- /dev/null
+/*
+ Analog Input
+ Demonstrates analog input by reading an analog sensor on analog pin 0 and
+ turning on and off a light emitting diode(LED) connected to digital pin 13.
+ The amount of time the LED will be on and off depends on
+ the value obtained by analogRead().
+
+ The circuit:
+ * Potentiometer attached to analog input 0
+ * center pin of the potentiometer to the analog pin
+ * one side pin (either one) to ground
+ * the other side pin to +5V
+ * LED anode (long leg) attached to digital output 13
+ * LED cathode (short leg) attached to ground
+
+ * Note: because most Arduinos have a built-in LED attached
+ to pin 13 on the board, the LED is optional.
+
+
+ Created by David Cuartielles
+ modified 30 Aug 2011
+ By Tom Igoe
+
+ This example code is in the public domain.
+
+ http://arduino.cc/en/Tutorial/AnalogInput
+
+ */
+
+int sensorPin = A0; // select the input pin for the potentiometer
+int ledPin = 13; // select the pin for the LED
+int sensorValue = 0; // variable to store the value coming from the sensor
+
+void setup() {
+ // declare the ledPin as an OUTPUT:
+ pinMode(ledPin, OUTPUT);
+ // Non e' necessario dichiarare un pin come input:
+ // tutti i pin di default sono input
+}
+
+void loop() {
+ // read the value from the sensor:
+ sensorValue = analogRead(sensorPin);
+ // turn the ledPin on
+ digitalWrite(ledPin, HIGH);
+ // stop the program for <sensorValue> milliseconds:
+ delay(sensorValue);
+ // turn the ledPin off:
+ digitalWrite(ledPin, LOW);
+ // stop the program for for <sensorValue> milliseconds:
+ delay(sensorValue);
+}
--- /dev/null
+/*
+ Analog Input
+ Demonstrates analog input by reading an analog sensor on analog pin 0 and
+ turning on and off a light emitting diode(LED) connected to digital pin 13.
+ The amount of time the LED will be on and off depends on
+ the value obtained by analogRead().
+
+ The circuit:
+ * Potentiometer attached to analog input 0
+ * center pin of the potentiometer to the analog pin
+ * one side pin (either one) to ground
+ * the other side pin to +5V
+ * LED anode (long leg) attached to digital output 13
+ * LED cathode (short leg) attached to ground
+
+ * Note: because most Arduinos have a built-in LED attached
+ to pin 13 on the board, the LED is optional.
+
+
+ Created by David Cuartielles
+ modified 30 Aug 2011
+ By Tom Igoe
+
+ This example code is in the public domain.
+
+ http://arduino.cc/en/Tutorial/AnalogInput
+
+ */
+
+int sensorPin = A0; // select the input pin for the potentiometer
+int ledPin = 13; // select the pin for the LED
+int sensorValue = 0; // variable to store the value coming from the sensor
+
+void setup() {
+ // declare the ledPin as an OUTPUT:
+ pinMode(ledPin, OUTPUT);
+ // initialize serial communications at 9600 bps:
+ Serial.begin(9600);
+}
+
+void loop() {
+ // read the value from the sensor:
+ sensorValue = analogRead(sensorPin);
+ // turn the ledPin on
+ digitalWrite(ledPin, HIGH);
+ // stop the program for <sensorValue> milliseconds:
+ delay(sensorValue);
+ // turn the ledPin off:
+ digitalWrite(ledPin, LOW);
+ // stop the program for for <sensorValue> milliseconds:
+ // print the results to the serial monitor:
+ Serial.print("sensor = " );
+ Serial.print(sensorValue);
+ Serial.print("\t delay = ");
+ Serial.println(sensorValue);
+ delay(sensorValue);
+}
--- /dev/null
+/*
+ Analog Input
+ Demonstrates analog input by reading an analog sensor on analog pin 0 and
+ turning on and off a light emitting diode(LED) connected to digital pin 13.
+ The amount of time the LED will be on and off depends on
+ the value obtained by analogRead().
+
+ The circuit:
+ * Potentiometer attached to analog input 0
+ * center pin of the potentiometer to the analog pin
+ * one side pin (either one) to ground
+ * the other side pin to +5V
+ * LED anode (long leg) attached to digital output 13
+ * LED cathode (short leg) attached to ground
+
+ * Note: because most Arduinos have a built-in LED attached
+ to pin 13 on the board, the LED is optional.
+
+
+ Created by David Cuartielles
+ modified 30 Aug 2011
+ By Tom Igoe
+
+ This example code is in the public domain.
+
+ http://arduino.cc/en/Tutorial/AnalogInput
+
+ Modified by A.Manni for using a 2.4k Pot with 2 5k resistors
+ Range = (1024 - offset) * 1024 / (1024 - offset)
+ With serial debugging.
+ */
+
+int sensorPin = A0; // select the input pin for the potentiometer
+int ledPin = 13; // select the pin for the LED
+int sensorValue = 0; // variable to store the value coming from the sensor
+
+void setup() {
+ // declare the ledPin as an OUTPUT:
+ pinMode(ledPin, OUTPUT);
+ Serial.begin(9600);
+}
+
+void loop() {
+ // read the value from the sensor:
+ sensorValue = analogRead(sensorPin);
+ // turn the ledPin on
+ digitalWrite(ledPin, HIGH);
+ // stop the program for <sensorValue> milliseconds:
+ delay(sensorValue);
+ // turn the ledPin off:
+ digitalWrite(ledPin, LOW);
+ // stop the program for for <sensorValue> milliseconds:
+ // Range = (1024 - offset) * 1024 / (1024 - offset)
+ delay((sensorValue -723 ) * 3.4); // Range = 723 - 1024
+ Serial.print("Sensor Value: ") ;
+ Serial.println(sensorValue);
+ Serial.print("Adjusted value: ") ;
+ Serial.println((sensorValue -723 ) * 3.4);
+}
--- /dev/null
+/*
+ Analog Input
+ Demonstrates analog input by reading an analog sensor on analog pin 0 and
+ turning on and off a light emitting diode(LED) connected to digital pin 13.
+ The amount of time the LED will be on and off depends on
+ the value obtained by analogRead().
+
+ The circuit:
+ * Potentiometer attached to analog input 0
+ * center pin of the potentiometer to the analog pin
+ * one side pin (either one) to ground
+ * the other side pin to +5V
+ * LED anode (long leg) attached to digital output 13
+ * LED cathode (short leg) attached to ground
+
+ * Note: because most Arduinos have a built-in LED attached
+ to pin 13 on the board, the LED is optional.
+
+
+ Created by David Cuartielles
+ modified 30 Aug 2011
+ By Tom Igoe
+
+ This example code is in the public domain.
+
+ http://arduino.cc/en/Tutorial/AnalogInput
+
+ Modified by A.Manni for using a 2.4k Pot with 2 5k resistors
+ Range = (1024 - offset) * 1024 / (1024 - offset)
+ Range can be defined with map(sensorValue, 724, 0124, 0, 1024)
+ Note: range is inverted
+ With serial debugging.
+ - Added initial limit for delay
+
+ */
+
+int sensorPin = A0; // select the input pin for the potentiometer
+int ledPin = 13; // select the pin for the LED
+int sensorValue = 0; // variable to store the value coming from the sensor
+int calValue = map(sensorValue, 723, 1024, 0, 1024) ; // variable to store calibrated value for the Pot
+// This could be done with a map()
+void setup() {
+
+ pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT:
+ Serial.begin(9600);
+
+}
+
+void loop() {
+ // read the value from the sensor:
+ sensorValue = analogRead(sensorPin);
+
+ // turn the ledPin on
+ digitalWrite(ledPin, HIGH);
+ // stop the program for <sensorValue> milliseconds:
+ delay(sensorValue);
+ // turn the ledPin off:
+ digitalWrite(ledPin, LOW);
+ // stop the program for for <sensorValue> milliseconds:
+ // Range = (1024 - offset) * 1024 / (1024 - offset)
+ // calValue = (sensorValue -723 ) * 3.4 ;
+ delay(calValue);
+ Serial.print("Sensor Value: ") ;
+ Serial.println(sensorValue);
+ Serial.print("Adjusted value: ") ;
+ Serial.println(calValue);
+
+}
+
+
+
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
-int led = 13;
+int led = 12;
int breve = 200; // Variabile richiambile nel corso dell'esecuzione
int lunga = 1000;
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(breve); // wait for a second
- digitalWrite(red, HIGH); // turn the LED on (HIGH is the voltage level)
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(lunga); // wait for a second
- digitalWrite(red, LOW); // turn the LED off by making the voltage LOW
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(lunga);
}
+++ /dev/null
-/*
- * Alternating switch
- */
-
-int switchPin = 2; // switch is connected to pin 2
-int val; // variable for reading the pin status
-int buttonState; // variable to hold the last button state
-int lightMode = 0; // State of the light
-int LED = 12;
-
-void setup() {
- pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
- pinMode(LED, OUTPUT);
-
- buttonState = digitalRead(switchPin); // read the initial state
-
-}
-
-
-void loop(){
- val = digitalRead(switchPin); // read input value and store it in val
- delay(100); // Debounce
- if ((val != buttonState) && (val == HIGH)) { // check if the button is pressed
- lightMode = 1 -lightMode ;
- }
- digitalWrite(LED,lightMode);
- buttonState = val; // save the new state in our variable
-}
-
-
-
+++ /dev/null
-/*
- * Turn on / off LED with a switch.
- When the lightmode is on the LED Blinks
- */
-
-int switchPin = 2; // switch is connected to pin 2
-int val; // variable for reading the pin status
-int valBounce ; // variable for debouncing
-int buttonState; // variable to hold the last button state
-int lightMode = 0; // State of the light
-int LED = 12;
-
-void setup() {
- pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
- pinMode(LED, OUTPUT);
-
- buttonState = digitalRead(switchPin); // read the initial state
-
-}
-
-
-void loop(){
- val = digitalRead(switchPin); // read input value and store it in val
- delay(10); // Debounce
- valBounce = digitalRead(switchPin); // read input value and store it in val
-
- if ((val == valBounce) && (val != buttonState) && (val == HIGH)) { // check if the button is pressed
- lightMode = 1 -lightMode ; // Now with DeBounce
- }
- if (lightMode) { // Check if light mode is TRUE == 1 or FALSE == 0
- delay(50); // Keep the LED LOW for 50ms
- digitalWrite(LED,HIGH); // Blink the LED
- delay(50); // Keep the LED HIGH for 50ms
- // digitalWrite(LED,LOW); // We don't need to turn it LOW
- // It will go off anyway later
- }
-
- digitalWrite(LED,LOW); // Almayes turn off the LED
- // As lightMode is FALSE == 0 turn the LED off
- // Turn it off
- buttonState = val; // save the new state in our variable
-}
-
-
-
-
-
-
-
-
+++ /dev/null
-/*
- * Alternating switch
- */
-
-int switchPin = 2; // switch is connected to pin 2
-int val; // variable for reading the pin status
-int valBounce ; // variable for debouncing
-int buttonState; // variable to hold the last button state
-int lightMode = 0; // State of the light
-int LED = 12;
-
-void setup() {
- pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
- pinMode(LED, OUTPUT);
-
- buttonState = digitalRead(switchPin); // read the initial state
-
-}
-
-
-void loop(){
- val = digitalRead(switchPin); // read input value and store it in val
- delay(10); // Debounce
- valBounce = digitalRead(switchPin); // read input value and store it in val
-
- if ((val == valBounce) && (val != buttonState) && (val == HIGH)) { // check if the button is pressed
- lightMode = 1 -lightMode ; // Now with DeBounce
- }
- digitalWrite(LED,lightMode);
- buttonState = val; // save the new state in our variable
-}
-
-
-
-
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the last button state
-int buttonPresses = 10; // Counter for the button
+int buttonPresses = 10; // Counter for the button
void setup() {
pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
int buttonPresses = 0; // Counter for the button
void setup() {
- pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input
+ pinMode(switchPin, INPUT); // Set the switch pin as input
Serial.begin(9600); // Set up serial communication at 9600bps
ultimoStato = digitalRead(switchPin); // read the initial state
void loop(){
statoAttuale = digitalRead(switchPin); // read input value and store it in val
- delay(100); // Debounce, sort of...
+ delay(20); // Debounce, sort of...
if ((statoAttuale != ultimoStato) && (statoAttuale == HIGH)) { // check if the button is pressed
buttonPresses++ ;
Serial.print("Button has been pressed ");
*/
-int switchPin = 2; // switch is connected to pin 2
-int statoAttuale; // variable for reading the pin status
-int ultimoStato; // variable to hold the last button state
+int switchPin = 2; // switch connesso al pin 2
+int statoAttuale; // Variabile per leggere lo stato del bottone
+int ultimoStato; // Variabile per registrare l'ultimo stato del bottone
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
void loop(){
- statoAttuale = digitalRead(switchPin); // read input value and store it in val
- // delay(20) // riduce leffetto bounce
- if (statoAttuale != ultimoStato) { // the button state has changed!
- if (statoAttuale == HIGH) { // check if the button is pressed
+ statoAttuale = digitalRead(switchPin); // Legge lo stato del bottone e lo resistra in val
+ // delay(20) // riduce l'effetto bounce
+ if (statoAttuale != ultimoStato) { // lo stato del bottone e' cambiato
+ if (statoAttuale == HIGH) { // il bottone e' stato premuto
Serial.println("Bottone premuto");
}
else { // the button is -not- pressed...
}
}
- ultimoStato = statoAttuale; // save the new state in our variable
+ ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale
}
void setup() {
pinMode(buttonPin, INPUT);
pinMode(led, OUTPUT);
- Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps
+ Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps
ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone
ledStatus = 0; // Il LED viene inpostato come spento
}
void loop(){
- statoAttuale = digitalRead(buttonPin); // read input value and store it in var
+ statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo resistra in val
delay(20); // riduce l'effetto bounce
if (statoAttuale != ultimoStato) { // lo stato del bottone e' cambiato
- if (statoAttuale == HIGH) { // il bottone e' stato provato
+ if (statoAttuale == HIGH) { // il bottone e' stato premuto
Serial.println("Button premuto");
ledStatus = !ledStatus ; // Inverte lo stato del LED
}
}
- ultimoStato = statoAttuale; // save the new state in our variable
+ ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale
digitalWrite(led, ledStatus); // setta il led allo stato richiesto
}
*/
int led = 13;
int buttonPin = 2;
-int statoAttuale; // variable for reading the pin status
-int ultimoStato; // variable to hold the last button state
+int statoAttuale; // Variabile per leggere lo stato del bottone
+int ultimoStato; // Variabile per registrare l'ultimo stato del bottone
int ledStatus; // varabile per mantenere lo stato del led
void setup() {
pinMode(buttonPin, INPUT); // Set the switch pin as input
pinMode(led, OUTPUT);
- Serial.begin(9600); // Set up serial communication at 9600bps
- ultimoStato = digitalRead(buttonPin); // read the initial state
- ledStatus = 0;
+ Serial.begin(9600); // Attiva la comunicazione seriale a 9600bps
+ ultimoStato = digitalRead(buttonPin); // Prima lettura del bottone
+ ledStatus = 0; // Il LED viene inpostato come spento
}
void loop(){
- statoAttuale = digitalRead(buttonPin); // read input value and store it in var
+ statoAttuale = digitalRead(buttonPin); // Legge lo stato del bottone e lo resistra in val
delay(20); // riduce l'effetto bounce
- if (statoAttuale != ultimoStato && statoAttuale == HIGH) {
- // the button state has changed AND the button is pressed
+ if (statoAttuale != ultimoStato && statoAttuale == HIGH) { // due condizione contemporanee
+ // lo stato del bottone e' camabiato AND lo stato attuale e' HIGH
Serial.println("Button premuto");
ledStatus = !ledStatus ; // Inverte lo stato del LED
Serial.println(ledStatus) ;
}
- ultimoStato = statoAttuale; // save the new state in our variable
+ ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale
digitalWrite(led, ledStatus); // setta il led allo stato richiesto
}
--- /dev/null
+/*
+ Stato di un bottone
+
+ Legge lo stato di un input
+
+ */
+int led = 13;
+int buttonPin = 2;
+int statoAttuale; // variable for reading the pin status
+int ultimoStato; // variable to hold the last button state
+int ledStatus = 0; // varabile per mantenere lo stato del led
+
+long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input
+long debounceDelay = 100; // Tempo di debounce
+
+void setup() {
+ pinMode(buttonPin, INPUT); // Set the switch pin as input
+ pinMode(led, OUTPUT);
+ Serial.begin(9600); // Set up serial communication at 9600bps
+ ultimoStato = digitalRead(buttonPin); // read the initial state
+}
+
+void loop(){
+ statoAttuale = digitalRead(buttonPin); // read input value and store it in var
+
+ if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
+ //
+ Serial.println("Button premuto");
+
+ ledStatus = !ledStatus ; // Inverte lo stato del LED
+ Serial.print("Stato del LED: "); // DEBUG
+ Serial.println(ledStatus) ;
+ ultimoCambio = millis() ; // Registra il tempo attuale
+ }
+
+ // Serial.print("statoAttuale ");
+ // Serial.println(statoAttuale);
+ // Serial.println(ultimoStato);
+ //delay(400);
+ ultimoStato = statoAttuale; // save the new state in our variable
+ digitalWrite(led, ledStatus); // setta il led allo stato richiesto
+
+
+}
+
+
+
+
+
+
+
+
+++ /dev/null
-/*
- Stato di un bottone
-
- Legge lo stato di un input
-
- */
-int led = 13;
-int buttonPin = 2;
-int statoAttuale; // variable for reading the pin status
-int ultimoStato; // variable to hold the last button state
-int ledStatus = 0; // varabile per mantenere lo stato del led
-
-long ultimoCambio = 0; // Momento in cui e' stato attivato il PIN input
-long debounceDelay = 100; // Tempo di debounce
-
-void setup() {
- pinMode(buttonPin, INPUT); // Set the switch pin as input
- pinMode(led, OUTPUT);
- Serial.begin(9600); // Set up serial communication at 9600bps
- ultimoStato = digitalRead(buttonPin); // read the initial state
-}
-
-void loop(){
- statoAttuale = digitalRead(buttonPin); // read input value and store it in var
-
- if (statoAttuale == HIGH && statoAttuale != ultimoStato && millis() - ultimoCambio > debounceDelay ) {
- //
- Serial.println("Button premuto");
-
- ledStatus = !ledStatus ; // Inverte lo stato del LED
- Serial.print("Stato del LED: "); // DEBUG
- Serial.println(ledStatus) ;
- ultimoCambio = millis() ; // Registra il tempo attuale
- }
-
- // Serial.print("statoAttuale ");
- // Serial.println(statoAttuale);
- // Serial.println(ultimoStato);
- //delay(400);
- ultimoStato = statoAttuale; // save the new state in our variable
- digitalWrite(led, ledStatus); // setta il led allo stato richiesto
-
-
-}
-
-
-
-
-
-
-
-
+++ /dev/null
-void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
-
-}
-
-void loop() {
-
- transforma(5); // Leggete i risultati con [CTR]+[SHIFT]+M
- transforma(255);
-
- Serial.flush() ;
- exit(0); // Termina l'esecuzione
-}
-
-// Ignorate pure il resto del listato!
-
-/* Transforma
-
- Scrive su seriale il valore della variabile a
- trasformandolo in binario e esadecimale
- */
-
-void transforma(int var) {
- Serial.print("Valore in decimanle = ");
- Serial.println(var); // Serial.println(a, DEC);
-
- Serial.print("Valore in binario = ");
- Serial.println(var,BIN);
-
- Serial.print("Valore in esadecimanle = ");
- Serial.println(var,HEX);
-
- Serial.println();
-}
-
-
-
-
-
--- /dev/null
+/*
+ Manual PWM
+
+ Gestire la modulazione della pulsazione tramite un delay,
+ questa tecnica puo' essere usata su qualunque PIN.
+
+ Altre info: http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM
+ */
+
+
+int pausa = 5 ; // 100 e' Circa 10% del duty cicle @ 1KHz
+int microPausa = 100 ; // 100 e' Circa 10% del duty cicle @ 1KHz
+
+void setup()
+{
+ pinMode(13, OUTPUT); // il PIN 13 non ha il PWM in hardware
+}
+
+void loop()
+{
+ brilla();
+// microBrilla();
+}
+/////////////////////////////
+// Funzioni personali
+
+void brilla() {
+ // lampeggia usando delay(): millesimi di secondo
+
+ digitalWrite(13, HIGH);
+ delay(pausa);
+ digitalWrite(13, LOW);
+ delay(pausa * 5);
+}
+
+void microBrilla() {
+ // lampeggia usando delayMicroseconds(): millionesimi di secondo
+
+ digitalWrite(13, HIGH);
+ delayMicroseconds(microPausa);
+ digitalWrite(13, LOW);
+ delayMicroseconds(1000 - microPausa);
+}
--- /dev/null
+/*pwm_
+ Fade
+
+ PWM per un LED: aumentare progressivamente la luminosita'.
+ */
+
+byte led = 9 ; // Il pin ~9 e' abilitato al PWM
+byte brightness = 0; // Valore iniziale per il PWM del LED
+
+// the setup routine runs once when you press reset:
+void setup() {
+ pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT
+}
+
+void loop() {
+ analogWrite(led, brightness++); // La funziona analogWrite utilizza il PWM
+ // a 8 bit integrato nel MCU: simula un serie di valori intermedi
+ // nell'intervallo discreto con minimo 0 (spento) e massimo 255 (acceso).
+ delay(10);
+}
+
+/* Domande:
+
+1. Come fare a invertire la dissolvenza diminuendo la luminosita'?
+2. Provare a far salire e poi scendere la luminosita'
+
+
--- /dev/null
+/*
+ LED for PWM
+
+ PWM per un LED: aumentare progressivamente la luminosita'.
+ Utilizzo di un ciclo iterativo: for loop
+
+ */
+
+int led = 9; // Pin per il PWM
+
+void setup()
+{
+ pinMode(led, OUTPUT);
+
+}
+
+void loop()
+{
+ for ( i = 0; i < 255 ; i++) { // Operatore ternario, 3 argomenti:
+ /* 1. definizione iteratore
+ 2. limite iteratore
+ 3. incremento operatore
+ */
+ analogWrite(led, i) ;
+ delay(5 );
+ }
+ // Ora l'inverso
+ for ( c = 255; c > 0 ; c--) {
+ analogWrite(led, c) ;
+ delay(5 );
+ }
+}
+
+
+
+
--- /dev/null
+/*
+Fade
+
+ This example shows how to fade an LED on pin 9
+ using the analogWrite() function.
+ This example code is in the public domain.
+ From Arduino for dummies.
+ */
+
+int led = 9;
+int brightness = 0;
+int fadeAmount = 5;
+// the pin that the LED is attached to
+// how bright the LED is
+// how many points to fade the LED by
+// the setup routine runs once when you press reset:
+
+void setup() {
+ // declare pin 9 to be an output:
+ pinMode(led, OUTPUT);
+}
+// the loop routine runs over and over again forever:
+void loop() {
+ // set the brightness of pin 9:
+ analogWrite(led, brightness);
+ // change the brightness for next time through the loop:
+ brightness = brightness + fadeAmount;
+ // reverse the direction of the fading at the ends of the fade:
+ if (brightness == 0 || brightness == 255) {
+ fadeAmount = -fadeAmount ;
+ }
+ // wait for 30 milliseconds to see the dimming effect
+ delay(30);
+}
+
+
+
+
+
+
--- /dev/null
+/*
+ Analog PWM
+
+ Impostare la frequenza del PWM tramite un input analogico.
+
+ */
+
+int inputPin = A0; // set input pin for the potentiometer
+int inputValue = 0; // potentiometer input variable
+int ledPin = 3; // output pin, deve avere il PWM
+
+void setup() {
+ // declare the ledPin as an OUTPUT:
+ pinMode(ledPin, OUTPUT);
+}
+
+void loop() {
+ // read the value from the potentiometer:
+ inputValue = analogRead(inputPin);
+
+ // send the square wave signal to the LED:
+ analogWrite(ledPin, inputValue/4);
+ // la lettura analogica e' a 10 bit (0-1024)
+ // Il PWM invece e' a 8 bit (0-255)
+ // Circa 1024 / 4 ~= 255
+
+ // Domanda: dovrebbe esserci un delay()?
+}
+
+++ /dev/null
-/*pwm_
- Fade
-
- PWM per un LED: aumentare progressivamente la luminosita'.
- */
-
-byte led = 9 ; // Il pin ~9 e' abilitato al PWM
-byte brightness = 0; // Valore iniziale per il PWM del LED
-
-// the setup routine runs once when you press reset:
-void setup() {
- pinMode(led, OUTPUT); // Il PIN nove va dichiarato come un OUTPUT
-}
-
-void loop() {
- analogWrite(led, brightness++); // La funziona analogWrite utilizza il PWM
- // a 8 bit integrato nel MCU: simula un serie di valori intermedi
- // nell'intervallo discreto con minimo 0 (spento) e massimo 255 (acceso).
- delay(10);
-}
-
-/* Domande:
-
-1. Come fare a invertire la dissolvenza diminuendo la luminosita'?
-2. Provare a far salire e poi scendere la luminosita'
-
-
+++ /dev/null
-/*
- LED for PWM
-
- PWM per un LED: aumentare progressivamente la luminosita'.
- Utilizzo di un ciclo iterativo: for loop
-
- */
-
-int led = 9; // Pin per il PWM
-
-void setup()
-{
- pinMode(led, OUTPUT);
-
-}
-
-void loop()
-{
- for ( i = 0; i < 255 ; i++) { // Operatore ternario, 3 argomenti:
- /* 1. definizione iteratore
- 2. limite iteratore
- 3. incremento operatore
- */
- analogWrite(led, i) ;
- delay(5 );
- }
- // Ora l'inverso
- for ( c = 255; c > 0 ; c--) {
- analogWrite(led, c) ;
- delay(5 );
- }
-}
-
-
-
-
--- /dev/null
+/*
+ * Hello World!
+ *
+ * This is the Hello World! for Arduino.
+ * It shows how to send data to the computer trough the serial connection
+ */
+
+void setup()
+{
+ Serial.begin(9600); // Inposta la seriale a 9600 bps
+ // Se Arduino manda 9600 bit per secondo e devi manandare 12 Bytes di dati
+ // quanto tempo serve per mandare i dati?
+
+ // Try to change bond rate in the monitor
+
+ Serial.println("Hello World!"); // scrive hello world e ritorna a capo
+
+
+// Serial.print("Il mio nome e': "); // Scrive senza ritorno a capo
+// Serial.println("Andrea"); // Scrive con ritorno a capo
+// Serial.flush(); // Assicura che tutto il testo venga scritto
+
+}
+
+void loop()
+{
+ // Provate a mettere i Serial.printl() qui, magari con un delay()
+}
+
+
+
+
--- /dev/null
+/*
+ * Switch test program
+ */
+
+int switchPin = 2; // Switch connected to digital pin 2
+
+void setup() // run once, when the sketch starts
+{
+ Serial.begin(9600); // set up Serial library at 9600 bps
+ pinMode(switchPin, INPUT); // sets the digital pin as input to read switch
+}
+
+
+void loop() // run over and over again
+{
+ Serial.print("Read switch input: ");
+ Serial.println(digitalRead(switchPin)); // Read the pin and display the value
+ delay(200);
+}
+++ /dev/null
-/*
- * Hello World!
- *
- * This is the Hello World! for Arduino.
- * It shows how to send data to the computer trough the serial connection
- */
-
-void setup()
-{
- Serial.begin(9600); // Inposta la seriale a 9600 bps
- // Se Arduino manda 9600 bit per secondo e devi manandare 12 Bytes di dati
- // quanto tempo serve per mandare i dati?
-
- // Try to change bond rate in the monitor
-
- Serial.println("Hello World!"); // scrive hello world e ritorna a capo
-
-
-// Serial.print("Il mio nome e': "); // Scrive senza ritorno a capo
-// Serial.println("Andrea"); // Scrive con ritorno a capo
-// Serial.flush(); // Assicura che tutto il testo venga scritto
-
-}
-
-void loop()
-{
- // Provate a mettere i Serial.printl() qui, magari con un delay()
-}
-
-
-
-
+++ /dev/null
-/*
- * Switch and LED test program
- */
-
-int ledPin = 12; // LED is connected to pin 12
-int switchPin = 2; // switch is connected to pin 2
-int val; // variable for reading the pin status
-
-
-void setup() {
- pinMode(ledPin, OUTPUT); // Set the LED pin as output
- pinMode(switchPin, INPUT); // Set the switch pin as input
-}
-
-
-void loop(){
- val = digitalRead(switchPin); // read input value and store it in val
- if (val == LOW) { // check if the button is pressed
- digitalWrite(ledPin, HIGH); // turn LED on
- }
- if (val == HIGH) { // check if the button is not pressed
- digitalWrite(ledPin, LOW); // turn LED off
- }
-}
+++ /dev/null
-/*
- * Switch test program
- */
-
-int switchPin = 2; // Switch connected to digital pin 2
-
-void setup() // run once, when the sketch starts
-{
- Serial.begin(9600); // set up Serial library at 9600 bps
- pinMode(switchPin, INPUT); // sets the digital pin as input to read switch
-}
-
-
-void loop() // run over and over again
-{
- Serial.print("Read switch input: ");
- Serial.println(digitalRead(switchPin)); // Read the pin and display the value
- delay(200);
-}
+++ /dev/null
-/*
- Blink con funzioni.
-
- Le funzioni sono una sequenza di istruzione raggruppate appunto in un a funzione.
- Le funzioni possono accettare argomenti e avere questi pre-impostati a valori di default se omessi.
- Le funzioni possono limitarsi a svolgere istruzionioppure elaborare valori restituendo un risultato.
-
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-int led = 13;
-void lunga() {
- 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
-}
-
-void breve() {
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
-
-}
-
-void varia(int a = 1000) { // Varia has a default value, the user can override it with an argument
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(a); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
-
-}
-
-// 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() {
- lunga() ;
- lunga() ;
- breve();
- breve();
- varia(3000);
-
-
-}
-
+++ /dev/null
-/*
- Head tails
- Generates a random number in order to simulate a coin toss.
-
-
- Phisical LEDS and serial debug.
-
- This example code is in the public domain.
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-const int head = 13 ; // LED for HEAD
-const int tail = 12 ; // LEAD for Tails
-const int PAUSE = 1000 ;
-const int REST = 50 ;
-long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
-int hCount = 0;
-int tCount = 0;
-int runs = 0 ;
-
-// the setup routine runs once when you press reset:
-void setup() {
- // initialize the digital pin as an output.
- pinMode(head, OUTPUT);
- pinMode(tail, OUTPUT);
- randomSeed(analogRead(0)); // Random initializer
- Serial.begin(9600);
- Serial.println("Initializing random sequence, please wait for results.");
-}
-
-// the loop routine runs over and over again forever:
-void loop() {
- randomNumber = random();
- digitalWrite(head, LOW);
- digitalWrite(tail, LOW);
- delay(REST); // wait a bit
- if (randomNumber % 2 == 1) {
- digitalWrite(head, HIGH);// turn the LED on ON
- hCount++ ;
- }
- else {
- digitalWrite(tail, HIGH);// turn the LED ON
- tCount++ ;
- }
-
- delay(PAUSE); // Long pause
- runs++;
-
- if (runs % 10 == 0) { // Each 10 runs print a summary to serial
- Serial.print("Results after more 10 runs, for a total of: ");
- Serial.println(runs);
- Serial.print("Tails: \t") ;
- Serial.println(tCount);
- Serial.print("Heads: \t");
- Serial.println(hCount);
- }
-}
-
-
-
+++ /dev/null
-/**
- * Program: find out is the user typed in a leap year. The code assumes
- * the user is not an idiot and only types in numbers that are a valid
- * year.
- * Author: Dr. Purdum, Aug. 7, 2012
- **/
-void setup()
-{
- Serial.begin(9600);
-}
-void loop()
-{
- if (Serial.available() > 0) {
- int bufferCount;
- int year;
- char myData[20];
- bufferCount = ReadLine(myData);
- year = atoi(myData);
- Serial.print("Year: ");
- Serial.print(year);
- if (IsLeapYear(year)) {
- Serial.print(" is ");
- }
- else {
- Serial.print(" is not ");
- }
- Serial.println("a leap year");
- }
-}
-// Convert to int
-/*****
- * Purpose: Determine if a given year is a leap year
- * Parameters:
- * int yr
- * The year to test
- * Return value:
- * int
- * 1 if the year is a leap year, 0 otherwise
- *****/
-int IsLeapYear(int yr)
-{
- if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) {
- return 1; // It is a leap year
- }
- else {
- return 0;
- // not a leap year
- }
-}
-/*****
- * Purpose: Read data from serial port until a newline character is read ('\n')
- * Parameters:
- * char str[]
- * character array that will be treated as a nul-terminated string
- * Return value:
- * int
- * the number of characters read for the string
- * CAUTION: This method will sit here forever if no input is read from the serial
- * port and no newline character is entered.
- ****/
-int ReadLine(char str[])
-{
- char c;
- int index = 0;
- while (true) {
- if (Serial.available() > 0) {
- c = Serial.read();
- if (c != '\n') {
- str[index++] = c;
- }
- else {
- str[index] = '\0'; // null termination character
- break;
- }
- }
- }
- return index;
-}
-
-
+++ /dev/null
-/* Exercise 2, with a WHILE loop
- Test a random number agains a value
- Light a led in case
- Light the other LED if a run of 255 test has gone
- Log the results (if success) trough serialport
- */
-
-// Data structure
-
-const byte GREEN = 13 ; // LED for found value
-const byte RED = 12 ; // LEAD for restart
-
-const int TARGET = 200 ;
-long randomNumber = 0L;
-
-// Staff
-const int WAIT = 1000 ;
-const int REST = 10 ;
-byte count = 0 ;
-const byte MAXRUN = 10 ;
-byte totalRun = 0 ;
-
-void setup() {
- pinMode(RED,OUTPUT);
- pinMode(GREEN,OUTPUT);
- // Serial stuff
- Serial.begin(9600);
- Serial.println("Initializing random sequence, please wait for results.");
-
- // Random stuff
- randomSeed(analogRead(0)); // Random initializer
-
-}
-
-void loop() { // put your main code here, to run repeatedly:
- digitalWrite(GREEN, LOW);
- digitalWrite(RED, LOW);
- // Serial.println(count);
-
- for (count == 0; count < 255; count++ , delay(REST)) {
- randomNumber = random(0,255); //Randoom value generated
- if (randomNumber == TARGET) { // When we catch the value
- Serial.print("--> Match found! Counter was at: "); // serial message
- Serial.println(count);
- digitalWrite(GREEN, HIGH);
- delay(WAIT * 5);
- digitalWrite(GREEN, LOW);
- count++ ;
- }
- }
-
- Serial.println("Counter resetted."); // serial staff
- digitalWrite(RED, HIGH);
- delay(WAIT);
- count++ ;
- totalRun++ ;
- if (totalRun == MAXRUN) {
- Serial.println("10 runs done, exit program.");
- digitalWrite(RED, HIGH);
- delay(WAIT);
- exit(0);
- }
-}
-
+++ /dev/null
-/* Exercise 2, with a WHILE loop
- Test a random number agains a value
- Light a led in case
- Light the other LED if a run of 255 test has gone
- Log the results (if success) trough serialport
- */
-
-// Data structure
-
-const byte GREEN = 13 ; // LED for found value
-const byte RED = 12 ; // LEAD for restart
-
-const int TARGET = 200 ;
-long randomNumber = 0L;
-
-// Staff
-const int WAIT = 1000 ;
-const int REST = 10 ;
-byte count = 0 ;
-const byte MAXRUN = 10 ;
-byte totalRun = 0 ;
-
-void setup() {
- pinMode(RED,OUTPUT);
- pinMode(GREEN,OUTPUT);
- // Serial stuff
- Serial.begin(9600);
- Serial.println("Initializing random sequence, please wait for results.");
-
- // Random stuff
- randomSeed(analogRead(0)); // Random initializer
-
-}
-
-void loop() { // put your main code here, to run repeatedly:
- digitalWrite(GREEN, LOW);
- digitalWrite(RED, LOW);
- // Serial.println(count);
-
- while (count < 255) {
- randomNumber = random(0,255); //Randoom value generated
- if (randomNumber == TARGET) { // When we catch the value
- Serial.print("--> Match found! Counter was at: "); // serial message
- Serial.println(count);
- digitalWrite(GREEN, HIGH);
- delay(WAIT);
- count++ ;
- }
- //Serial.println(count);
- count++ ;
- delay(REST);
- }
-
-
- Serial.println("Counter resetted."); // serial staff
- digitalWrite(RED, HIGH);
- delay(WAIT);
- count++ ;
- totalRun++ ;
- if (totalRun == MAXRUN) {
- Serial.println("10 runs done, exit program.");
- digitalWrite(RED, HIGH);
- delay(WAIT);
- exit(0);
- }
-}
-
+++ /dev/null
-Use examples provided with arduino:
-
-EDIT -> Example -> Control -> ...
-
-- SwitchCase: check the range of a (mapped) analog input with a switch loop.
-- SwitchCase2: SerialInput to light up a bunch of LEDS.
+++ /dev/null
-
-int *ptrNumber ;
-void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
-
-}
-
-void loop() {
- int number = 5;
-
-
- Serial.print("number is ");
- Serial.println(number);
- Serial.print("The lvalue for number is: ");
- Serial.println((long) &number, DEC);
-
- Serial.print("---- Pointer was ");
- Serial.println(*ptrNumber);
- Serial.print("The lvalue for ptrNumber is: ");
- Serial.println((long) &ptrNumber, DEC);
- Serial.print(" and the rvalue is ");
- Serial.println((long) ptrNumber, DEC);
-
- ptrNumber = &number ;
- Serial.println("Assigned!");
-
- Serial.print("===== Pointer was ");
- Serial.println(*ptrNumber);
- Serial.print("The lvalue for ptrNumber is: ");
- Serial.println((long) &ptrNumber, DEC);
- Serial.print(" and the rvalue is ");
- Serial.println((long) ptrNumber, DEC);
-
- *ptrNumber = 6 ;
- Serial.print("**** Pointer value is: ");
- Serial.println(*ptrNumber);
- Serial.println(number);
-
- Serial.flush();
- exit(0);
-
-}
-
-
+++ /dev/null
-/*
-Purpose: Illustrate pointer arithmetic
- Dr. Purdum, August 20, 2012
- */
-#include <string.h>
-void setup() {
- Serial.begin(9600);
-}
-void loop() {
- char buffer[50];
- char *ptr;
- int i;
- int length;
-
- strcpy(buffer, "When in the course of human events");
- ptr = buffer;
- length = strlen(buffer);
- Serial.print("The lvalue for ptr is: ");
- Serial.print((unsigned int)&ptr);
- Serial.print(" and the rvalue is ");
- Serial.println((unsigned int)ptr);
- while (*ptr) {
- Serial.print(*ptr++); // This actually incrementa ptr* + 34
- }
- Serial.println("");
-
- Serial.println("Lenght of the string is: ");
- Serial.println(length);
- Serial.println("");
-
-
- // ptr = ptr - length ; // Whis would roll back ptr
- for (i = 0; i < length; i++) {
- Serial.print(*(ptr + i));
- // Serial.print(*(ptr + i- lenght)); // ptr is one lenght up ahead
- }
- // ptr = buffer ; // Right thing to do: reset the pointer before use
- // for (i = 0; i < length; i++) {
- // Serial.print(*(ptr + i))
-
-
- Serial.flush();
- // Make sure all the data is sent...
- exit(0);
-}
-
-
-
-
+++ /dev/null
-int *ptr; // no rvalue
-
-void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
-
-}
-
-void loop() {
- int num =5;
- ptr = &num ;
-
- transforma(num); // Leggete i risultati con [CTR]+[SHIFT]+M
- Serial.println(num);
-
- Serial.flush() ;
- exit(0); // Termina l'esecuzione
-}
-
-// Ignorate pure il resto del listato!
-
-/* Transforma
-
- Scrive su seriale il valore della variabile a
- trasformandolo in binario e esadecimale
- */
-
-void transforma(int var) {
- Serial.print("Valore della variabile = ");
- Serial.print(var);
- *ptr = 12 ; // Num is outside the scope of this function
- // but a pointer can get there
-
-
- Serial.println();
-}
-
-
-
-
-
+++ /dev/null
-
-
-struct RGB {
- byte r;
- byte g;
- byte b;
-}
-myLED ;
-
-void setup() {
- Serial.begin(9600);
- // Serial link to PC
-}
-void loop() {
- myLED = {
- 100,200,255 }
- ;
-
- // Serial.println(myLED.r) ;
-
- illumina(&myLED);
- rossa(&myLED);
- Serial.println();
- illumina(&myLED);
-
- Serial.flush();
- exit(0);
-}
-
-void illumina(struct RGB *tempLED) { // Function with a pointer
- Serial.println((*tempLED).r) ; // This does not waste STACK space
- Serial.println((*tempLED).g) ; // Note: indirect operator * has lower priority
- Serial.println(tempLED->b) ; // than dot . operator , so the parentheses
- // That is the dereference -> operator
-}
-
-struct RGB rossa(struct RGB *temp) { // Function with a pointer:
- // This can change directly the original value without the need
- // to reassign as in: myLED = rossa(myLED)
- (*temp).r = 255 ;
- //return *temp;
-}
-
-
-
-
-
+++ /dev/null
-/*
- Analog Input
- Demonstrates analog input by reading an analog sensor on analog pin 0 and
- turning on and off a light emitting diode(LED) connected to digital pin 13.
- The amount of time the LED will be on and off depends on
- the value obtained by analogRead().
-
- The circuit:
- * Potentiometer attached to analog input 0
- * center pin of the potentiometer to the analog pin
- * one side pin (either one) to ground
- * the other side pin to +5V
- * LED anode (long leg) attached to digital output 13
- * LED cathode (short leg) attached to ground
-
- * Note: because most Arduinos have a built-in LED attached
- to pin 13 on the board, the LED is optional.
-
-
- Created by David Cuartielles
- modified 30 Aug 2011
- By Tom Igoe
-
- This example code is in the public domain.
-
- http://arduino.cc/en/Tutorial/AnalogInput
-
- Modified by A.Manni for using a 2.4k Pot with 2 5k resistors
- Range = (1024 - offset) * 1024 / (1024 - offset)
- With serial debugging.
- */
-
-int sensorPin = A0; // select the input pin for the potentiometer
-int ledPin = 13; // select the pin for the LED
-int sensorValue = 0; // variable to store the value coming from the sensor
-
-void setup() {
- // declare the ledPin as an OUTPUT:
- pinMode(ledPin, OUTPUT);
- Serial.begin(9600);
-}
-
-void loop() {
- // read the value from the sensor:
- sensorValue = analogRead(sensorPin);
- // turn the ledPin on
- digitalWrite(ledPin, HIGH);
- // stop the program for <sensorValue> milliseconds:
- delay(sensorValue);
- // turn the ledPin off:
- digitalWrite(ledPin, LOW);
- // stop the program for for <sensorValue> milliseconds:
- // Range = (1024 - offset) * 1024 / (1024 - offset)
- delay((sensorValue -723 ) * 3.4); // Range = 723 - 1024
- Serial.print("Sensor Value: ") ;
- Serial.println(sensorValue);
- Serial.print("Adjusted value: ") ;
- Serial.println((sensorValue -723 ) * 3.4);
-}
+++ /dev/null
-/*
- Analog Input
- Demonstrates analog input by reading an analog sensor on analog pin 0 and
- turning on and off a light emitting diode(LED) connected to digital pin 13.
- The amount of time the LED will be on and off depends on
- the value obtained by analogRead().
-
- The circuit:
- * Potentiometer attached to analog input 0
- * center pin of the potentiometer to the analog pin
- * one side pin (either one) to ground
- * the other side pin to +5V
- * LED anode (long leg) attached to digital output 13
- * LED cathode (short leg) attached to ground
-
- * Note: because most Arduinos have a built-in LED attached
- to pin 13 on the board, the LED is optional.
-
-
- Created by David Cuartielles
- modified 30 Aug 2011
- By Tom Igoe
-
- This example code is in the public domain.
-
- http://arduino.cc/en/Tutorial/AnalogInput
-
- Modified by A.Manni for using a 2.4k Pot with 2 5k resistors
- Range = (1024 - offset) * 1024 / (1024 - offset)
- Range can be defined with map(sensorValue, 724, 0124, 0, 1024)
- Note: range is inverted
- With serial debugging.
- - Added initial limit for delay
-
- */
-
-int sensorPin = A0; // select the input pin for the potentiometer
-int ledPin = 13; // select the pin for the LED
-int sensorValue = 0; // variable to store the value coming from the sensor
-int calValue = map(sensorValue, 723, 1024, 0, 1024) ; // variable to store calibrated value for the Pot
-// This could be done with a map()
-void setup() {
-
- pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT:
- Serial.begin(9600);
-
-}
-
-void loop() {
- // read the value from the sensor:
- sensorValue = analogRead(sensorPin);
-
- // turn the ledPin on
- digitalWrite(ledPin, HIGH);
- // stop the program for <sensorValue> milliseconds:
- delay(sensorValue);
- // turn the ledPin off:
- digitalWrite(ledPin, LOW);
- // stop the program for for <sensorValue> milliseconds:
- // Range = (1024 - offset) * 1024 / (1024 - offset)
- // calValue = (sensorValue -723 ) * 3.4 ;
- delay(calValue);
- Serial.print("Sensor Value: ") ;
- Serial.println(sensorValue);
- Serial.print("Adjusted value: ") ;
- Serial.println(calValue);
-
-}
-
-
-
--- /dev/null
+/*
+ Blink con funzioni.
+
+ Le funzioni sono una sequenza di istruzione raggruppate appunto in un a funzione.
+ Le funzioni possono accettare argomenti e avere questi pre-impostati a valori di default se omessi.
+ Le funzioni possono limitarsi a svolgere istruzionioppure elaborare valori restituendo un risultato.
+
+ */
+
+// 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() {
+ lunga() ;
+ lunga() ;
+ breve();
+ breve();
+ varia(3000);
+}
+
+// Funzioni personalizzate: nella scheda funzioni.
--- /dev/null
+// Funzioni personalizzate
+// Un scheda e' un documento che viene concatenato allo sketch originale
+
+void lunga() {
+ // Blink con pausa lunga
+
+ 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
+}
+
+void breve() {
+ // Blink con pausa breve
+
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(200); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(1000); // wait for a second
+}
+
+void varia(int a = 300) { // Varia has a default value, the user can override it with an argument
+ // Lampeggia per un tempo impostato dall'utente,
+ // il default e' 300ms
+
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(a); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(a); // wait for a second
+
+}
+
+void lampeggia(int ripetizioni) {
+ // Accende un LED per un numero stabilito di volte
+
+ // Questa funziona accetta un parametro: ripetizioni
+ int i = 0;
+ while (i < ripetizioni) {
+ rapido(); // accende e spegne rapidamente il LED
+ i = i + 1 ; // incrementa l'iteratore
+ // i++ ; // equivalente
+ }
+}
+
+int area(int latoA, int latoB) {
+ // Calcola l'area di un rettangolo
+ // e ritorna il valore calcolato: questa funzione ha un valore di ritorno
+ // dichiarato come int
+
+ return(latoA * latoB);
+}
--- /dev/null
+/*
+ Head tails
+ Generates a random number in order to simulate a coin toss.
+
+
+ Phisical LEDS and serial debug.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+const int head = 13 ; // LED for HEAD
+const int tail = 12 ; // LEAD for Tails
+const int PAUSE = 1000 ;
+const int REST = 50 ;
+long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
+int hCount = 0;
+int tCount = 0;
+int runs = 0 ;
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(head, OUTPUT);
+ pinMode(tail, OUTPUT);
+ randomSeed(analogRead(0)); // Random initializer
+ Serial.begin(9600);
+ Serial.println("Initializing random sequence, please wait for results.");
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ randomNumber = random();
+ digitalWrite(head, LOW);
+ digitalWrite(tail, LOW);
+ delay(REST); // wait a bit
+ if (randomNumber % 2 == 1) {
+ digitalWrite(head, HIGH);// turn the LED on ON
+ hCount++ ;
+ }
+ else {
+ digitalWrite(tail, HIGH);// turn the LED ON
+ tCount++ ;
+ }
+
+ delay(PAUSE); // Long pause
+ runs++;
+
+ if (runs % 10 == 0) { // Each 10 runs print a summary to serial
+ Serial.print("Results after more 10 runs, for a total of: ");
+ Serial.println(runs);
+ Serial.print("Tails: \t") ;
+ Serial.println(tCount);
+ Serial.print("Heads: \t");
+ Serial.println(hCount);
+ }
+}
+
+
+
--- /dev/null
+void setup() {
+ // put your setup code here, to run once:
+ Serial.begin(9600);
+
+}
+
+void loop() {
+
+ transforma(5); // Leggete i risultati con [CTR]+[SHIFT]+M
+ transforma(255);
+
+ Serial.flush() ;
+ exit(0); // Termina l'esecuzione
+}
+
+// Ignorate pure il resto del listato!
+
+/* Transforma
+
+ Scrive su seriale il valore della variabile a
+ trasformandolo in binario e esadecimale
+ */
+
+void transforma(int var) {
+ Serial.print("Valore in decimanle = ");
+ Serial.println(var); // Serial.println(a, DEC);
+
+ Serial.print("Valore in binario = ");
+ Serial.println(var,BIN);
+
+ Serial.print("Valore in esadecimanle = ");
+ Serial.println(var,HEX);
+
+ Serial.println();
+}
+
+
+
+
+
--- /dev/null
+/**
+ * Program: find out is the user typed in a leap year. The code assumes
+ * the user is not an idiot and only types in numbers that are a valid
+ * year.
+ * Author: Dr. Purdum, Aug. 7, 2012
+ **/
+void setup()
+{
+ Serial.begin(9600);
+}
+void loop()
+{
+ if (Serial.available() > 0) {
+ int bufferCount;
+ int year;
+ char myData[20];
+ bufferCount = ReadLine(myData);
+ year = atoi(myData);
+ Serial.print("Year: ");
+ Serial.print(year);
+ if (IsLeapYear(year)) {
+ Serial.print(" is ");
+ }
+ else {
+ Serial.print(" is not ");
+ }
+ Serial.println("a leap year");
+ }
+}
+// Convert to int
+/*****
+ * Purpose: Determine if a given year is a leap year
+ * Parameters:
+ * int yr
+ * The year to test
+ * Return value:
+ * int
+ * 1 if the year is a leap year, 0 otherwise
+ *****/
+int IsLeapYear(int yr)
+{
+ if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) {
+ return 1; // It is a leap year
+ }
+ else {
+ return 0;
+ // not a leap year
+ }
+}
+/*****
+ * Purpose: Read data from serial port until a newline character is read ('\n')
+ * Parameters:
+ * char str[]
+ * character array that will be treated as a nul-terminated string
+ * Return value:
+ * int
+ * the number of characters read for the string
+ * CAUTION: This method will sit here forever if no input is read from the serial
+ * port and no newline character is entered.
+ ****/
+int ReadLine(char str[])
+{
+ char c;
+ int index = 0;
+ while (true) {
+ if (Serial.available() > 0) {
+ c = Serial.read();
+ if (c != '\n') {
+ str[index++] = c;
+ }
+ else {
+ str[index] = '\0'; // null termination character
+ break;
+ }
+ }
+ }
+ return index;
+}
+
+
--- /dev/null
+/* Exercise 2, with a WHILE loop
+
+ Test a random number agains a value:
+ a iteretive loop perform 255 runs to see if a random number in range 0-255
+ is equal tothe target value of 200
+ Light a led in case
+ Light the other LED if a run of 255 test has gone
+ Log the results (if success) trough serialport
+ */
+
+// Data structure
+
+const byte GREEN = 13 ; // LED for found value
+const byte RED = 12 ; // LEAD for restart
+
+const int TARGET = 200 ;
+long randomNumber = 0L;
+
+// Staff
+const int WAIT = 1000 ;
+const int REST = 10 ;
+byte count = 0 ;
+const byte MAXRUN = 10 ;
+byte totalRun = 0 ;
+
+void setup() {
+ pinMode(RED,OUTPUT);
+ pinMode(GREEN,OUTPUT);
+ // Serial stuff
+ Serial.begin(9600);
+ Serial.println("Initializing random sequence, please wait for results.");
+
+ // Random stuff
+ randomSeed(analogRead(0)); // Random initializer
+
+}
+
+void loop() { // put your main code here, to run repeatedly:
+ digitalWrite(GREEN, LOW);
+ digitalWrite(RED, LOW);
+ // Serial.println(count);
+
+ for (count == 0; count < 255; count++ , delay(REST)) {
+ randomNumber = random(0,255); //Randoom value generated
+ if (randomNumber == TARGET) { // When we catch the value
+ Serial.print("--> Match found! Counter was at: "); // serial message
+ Serial.println(count);
+ digitalWrite(GREEN, HIGH);
+ delay(WAIT * 5);
+ digitalWrite(GREEN, LOW);
+ count++ ;
+ }
+ }
+
+ Serial.println("Counter resetted."); // serial staff
+ digitalWrite(RED, HIGH);
+ delay(WAIT);
+ count++ ;
+ totalRun++ ;
+ if (totalRun == MAXRUN) {
+ Serial.println("10 runs done, exit program.");
+ digitalWrite(RED, HIGH);
+ delay(WAIT);
+ exit(0);
+ }
+}
+
--- /dev/null
+/* Exercise 2, with a WHILE loop
+ Test a random number agains a value:
+ a iteretive loop perform 255 runs to see if a random number in range 0-255
+ is equal tothe target value of 200.
+
+ Light a led in case
+ Light the other LED if a run of 255 test has gone
+ Log the results (if success) trough serialport
+ */
+
+// Data structure
+
+const byte GREEN = 13 ; // LED for found value
+const byte RED = 12 ; // LEAD for restart
+
+const int TARGET = 200 ;
+long randomNumber = 0L;
+
+// Staff
+const int WAIT = 1000 ;
+const int REST = 10 ;
+byte count = 0 ;
+const byte MAXRUN = 10 ;
+byte totalRun = 0 ;
+
+void setup() {
+ pinMode(RED,OUTPUT);
+ pinMode(GREEN,OUTPUT);
+ // Serial stuff
+ Serial.begin(9600);
+ Serial.println("Initializing random sequence, please wait for results.");
+
+ // Random stuff
+ randomSeed(analogRead(0)); // Random initializer
+
+}
+
+void loop() { // put your main code here, to run repeatedly:
+ digitalWrite(GREEN, LOW);
+ digitalWrite(RED, LOW);
+ // Serial.println(count);
+
+ while (count < 255) {
+ randomNumber = random(0,255); //Randoom value generated
+ if (randomNumber == TARGET) { // When we catch the value
+ Serial.print("--> Match found! Counter was at: "); // serial message
+ Serial.println(count);
+ digitalWrite(GREEN, HIGH);
+ delay(WAIT);
+ count++ ;
+ }
+ //Serial.println(count);
+ count++ ;
+ delay(REST);
+ }
+
+
+ Serial.println("Counter resetted."); // serial staff
+ digitalWrite(RED, HIGH);
+ delay(WAIT);
+ count++ ;
+ totalRun++ ;
+ if (totalRun == MAXRUN) {
+ Serial.println("10 runs done, exit program.");
+ digitalWrite(RED, HIGH);
+ delay(WAIT);
+ exit(0);
+ }
+}
+
--- /dev/null
+Use examples provided with arduino:
+
+EDIT -> Example -> Control -> ...
+
+- SwitchCase: check the range of a (mapped) analog input with a switch loop.
+- SwitchCase2: SerialInput to light up a bunch of LEDS.
--- /dev/null
+/*
+ Operatori base
+
+ */
+
+int a = 5;
+int b = 10;
+int c = 20;
+
+void setup() // run once, when the sketch starts
+{
+ Serial.begin(9600); // set up Serial library at 9600 bps
+
+ Serial.println("Here is some math: ");
+
+ Serial.print("a = ");
+ Serial.println(a);
+ Serial.print("b = ");
+ Serial.println(b);
+ Serial.print("c = ");
+ Serial.println(c);
+
+ Serial.print("a + b = "); // add
+ Serial.println(a + b);
+
+ Serial.print("a * c = "); // multiply
+ Serial.println(a * c);
+
+ Serial.print("c / b = "); // divide
+ Serial.println(c / b);
+
+ Serial.print("b - c = "); // subtract
+ Serial.println(b - c);
+
+ Serial.print("b modulo a = "); // Calculates the remainder when one integer is divided by another.
+ Serial.println(b % a);
+
+ Serial.print("8 modulo 3 = "); // Calculates the remainder when one integer is divided by another.
+ Serial.println(8 % 3);
+}
+
+void loop() // we need this to be here even though its empty
+{
+}
+
+
--- /dev/null
+/*
+ Operatori comparativi binari
+ Comparison operators, binary
+
+ */
+
+int a = 5;
+int b = 10;
+int c = 20;
+
+void setup() // run once, when the sketch starts
+{
+ Serial.begin(9600); // set up Serial library at 9600 bps
+
+ Serial.println("Here is some math: ");
+
+ Serial.print("a = ");
+ Serial.println(a);
+ Serial.print("b = ");
+ Serial.println(b);
+ Serial.print("c = ");
+ Serial.println(c);
+
+ Serial.print("a > b = "); // maggiore
+ Serial.println(a > b);
+
+ Serial.print("a < b = "); // minore
+ Serial.println(a < c);
+
+ Serial.print("a == b -> "); // stesso valore
+ Serial.println(a == b);
+
+ Serial.print("a != b -> "); // valore diverso
+ Serial.println(a != b);
+
+ Serial.print("a <= b ->"); // minore uguale
+ Serial.println(b <= a);
+
+ Serial.print("a >= b -> "); // maggiore uguale
+ Serial.println(b >= a);
+}
+
+void loop() // we need this to be here even though its empty
+{
+}
+
+
--- /dev/null
+/*
+ Operatori boleani binari, operatori logici
+ Comparison operators, binary
+
+ */
+
+int a = 5;
+int b = 10;
+int c = 20;
+
+
+void setup() // run once, when the sketch starts
+{
+ Serial.begin(9600); // set up Serial library at 9600 bps
+
+ Serial.println("Here is some math: ");
+
+ Serial.print("a = ");
+ Serial.println(a);
+ Serial.print("b = ");
+ Serial.println(b);
+ Serial.print("c = ");
+ Serial.println(c);
+
+ Serial.print("a < b AND a < c : "); // And logico
+ Serial.println(a < b && a < c);
+
+ Serial.print("b < a OR c < a : "); // Or logico
+ Serial.println(b < a || c < a);
+
+ Serial.print("a == b : "); // stesso valore
+ Serial.println(a == b);
+
+ Serial.print("a != b : "); // valore diverso
+ Serial.println(a != b);
+
+// Testiamo direttamente le singole entita':
+
+ Serial.print("true AND true: "); // And logico
+ Serial.println(true && true);
+
+ Serial.print("true AND false: "); // And logico
+ Serial.println(true && false);
+
+ Serial.print("true OR false: "); // Or logico
+ Serial.println(true || false);
+
+ Serial.print("false OR false: "); // Or logico
+ Serial.println(false || false);
+}
+
+void loop() // we need this to be here even though its empty
+{
+}
+
+
+
--- /dev/null
+
+int *ptrNumber ;
+void setup() {
+ // put your setup code here, to run once:
+ Serial.begin(9600);
+
+}
+
+void loop() {
+ int number = 5;
+
+
+ Serial.print("number is ");
+ Serial.println(number);
+ Serial.print("The lvalue for number is: ");
+ Serial.println((long) &number, DEC);
+
+ Serial.print("---- Pointer was ");
+ Serial.println(*ptrNumber);
+ Serial.print("The lvalue for ptrNumber is: ");
+ Serial.println((long) &ptrNumber, DEC);
+ Serial.print(" and the rvalue is ");
+ Serial.println((long) ptrNumber, DEC);
+
+ ptrNumber = &number ;
+ Serial.println("Assigned!");
+
+ Serial.print("===== Pointer was ");
+ Serial.println(*ptrNumber);
+ Serial.print("The lvalue for ptrNumber is: ");
+ Serial.println((long) &ptrNumber, DEC);
+ Serial.print(" and the rvalue is ");
+ Serial.println((long) ptrNumber, DEC);
+
+ *ptrNumber = 6 ;
+ Serial.print("**** Pointer value is: ");
+ Serial.println(*ptrNumber);
+ Serial.println(number);
+
+ Serial.flush();
+ exit(0);
+
+}
+
+
--- /dev/null
+/*
+Purpose: Illustrate pointer arithmetic
+ Dr. Purdum, August 20, 2012
+ */
+#include <string.h>
+void setup() {
+ Serial.begin(9600);
+}
+void loop() {
+ char buffer[50];
+ char *ptr;
+ int i;
+ int length;
+
+ strcpy(buffer, "When in the course of human events");
+ ptr = buffer;
+ length = strlen(buffer);
+ Serial.print("The lvalue for ptr is: ");
+ Serial.print((unsigned int)&ptr);
+ Serial.print(" and the rvalue is ");
+ Serial.println((unsigned int)ptr);
+ while (*ptr) {
+ Serial.print(*ptr++); // This actually incrementa ptr* + 34
+ }
+ Serial.println("");
+
+ Serial.println("Lenght of the string is: ");
+ Serial.println(length);
+ Serial.println("");
+
+
+ // ptr = ptr - length ; // Whis would roll back ptr
+ for (i = 0; i < length; i++) {
+ Serial.print(*(ptr + i));
+ // Serial.print(*(ptr + i- lenght)); // ptr is one lenght up ahead
+ }
+ // ptr = buffer ; // Right thing to do: reset the pointer before use
+ // for (i = 0; i < length; i++) {
+ // Serial.print(*(ptr + i))
+
+
+ Serial.flush();
+ // Make sure all the data is sent...
+ exit(0);
+}
+
+
+
+
--- /dev/null
+int *ptr; // no rvalue
+
+void setup() {
+ // put your setup code here, to run once:
+ Serial.begin(9600);
+
+}
+
+void loop() {
+ int num =5;
+ ptr = &num ;
+
+ transforma(num); // Leggete i risultati con [CTR]+[SHIFT]+M
+ Serial.println(num);
+
+ Serial.flush() ;
+ exit(0); // Termina l'esecuzione
+}
+
+// Ignorate pure il resto del listato!
+
+/* Transforma
+
+ Scrive su seriale il valore della variabile a
+ trasformandolo in binario e esadecimale
+ */
+
+void transforma(int var) {
+ Serial.print("Valore della variabile = ");
+ Serial.print(var);
+ *ptr = 12 ; // Num is outside the scope of this function
+ // but a pointer can get there
+
+
+ Serial.println();
+}
+
+
+
+
+
--- /dev/null
+
+
+struct RGB {
+ byte r;
+ byte g;
+ byte b;
+}
+myLED ;
+
+void setup() {
+ Serial.begin(9600);
+ // Serial link to PC
+}
+void loop() {
+ myLED = {
+ 100,200,255 }
+ ;
+
+ // Serial.println(myLED.r) ;
+
+ illumina(&myLED);
+ rossa(&myLED);
+ Serial.println();
+ illumina(&myLED);
+
+ Serial.flush();
+ exit(0);
+}
+
+void illumina(struct RGB *tempLED) { // Function with a pointer
+ Serial.println((*tempLED).r) ; // This does not waste STACK space
+ Serial.println((*tempLED).g) ; // Note: indirect operator * has lower priority
+ Serial.println(tempLED->b) ; // than dot . operator , so the parentheses
+ // That is the dereference -> operator
+}
+
+struct RGB rossa(struct RGB *temp) { // Function with a pointer:
+ // This can change directly the original value without the need
+ // to reassign as in: myLED = rossa(myLED)
+ (*temp).r = 255 ;
+ //return *temp;
+}
+
+
+
+
+