From: Andrea Manni Date: Mon, 16 Mar 2015 16:53:11 +0000 (+0100) Subject: operatori + analog X-Git-Url: http://git.piffa.net/web?a=commitdiff_plain;h=e50f2cf8e7402ea56cd01835be9f88c53876bfd1;p=sketchbook_andrea operatori + analog --- diff --git a/README b/README index 1b29d5d..492f5a4 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Andrea's Sketchbook +Sketchbook di Andrea ===================== Esempi per i corsi su Arduino. @@ -6,6 +6,9 @@ Le ultime versioni sono disponibili su: git.andreamanni.com - http://git.andreamanni.com/ - Interfaccia web: http://git.andreamanni.com/web +Gestione +-------------------- + Per aggiornare il proprio archivio :: cd sketchbook_andrea/ ; git fetch @@ -16,7 +19,7 @@ Per cancellare completamente il vecchio e reinstanziarlo :: 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 diff --git a/RGB_LED/ReadASCIIString/ReadASCIIString b/RGB_LED/ReadASCIIString/ReadASCIIString new file mode 100644 index 0000000..9da6b1e --- /dev/null +++ b/RGB_LED/ReadASCIIString/ReadASCIIString @@ -0,0 +1,66 @@ +/* + 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); + } + } +} diff --git a/basic/README b/basic/README new file mode 100644 index 0000000..bd1b845 --- /dev/null +++ b/basic/README @@ -0,0 +1,12 @@ +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. diff --git a/basic/analog_input/analogInput_1/analogInput_1.ino b/basic/analog_input/analogInput_1/analogInput_1.ino new file mode 100644 index 0000000..28547d5 --- /dev/null +++ b/basic/analog_input/analogInput_1/analogInput_1.ino @@ -0,0 +1,52 @@ +/* + 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 milliseconds: + delay(sensorValue); + // turn the ledPin off: + digitalWrite(ledPin, LOW); + // stop the program for for milliseconds: + delay(sensorValue); +} diff --git a/basic/analog_input/analogInput_2_serial/analogInput_2_serial.ino b/basic/analog_input/analogInput_2_serial/analogInput_2_serial.ino new file mode 100644 index 0000000..2dbb328 --- /dev/null +++ b/basic/analog_input/analogInput_2_serial/analogInput_2_serial.ino @@ -0,0 +1,57 @@ +/* + 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 milliseconds: + delay(sensorValue); + // turn the ledPin off: + digitalWrite(ledPin, LOW); + // stop the program for for milliseconds: + // print the results to the serial monitor: + Serial.print("sensor = " ); + Serial.print(sensorValue); + Serial.print("\t delay = "); + Serial.println(sensorValue); + delay(sensorValue); +} diff --git a/basic/analog_input/optimization/analogInput_with_range/analogInput_with_range.ino b/basic/analog_input/optimization/analogInput_with_range/analogInput_with_range.ino new file mode 100644 index 0000000..715cc79 --- /dev/null +++ b/basic/analog_input/optimization/analogInput_with_range/analogInput_with_range.ino @@ -0,0 +1,59 @@ +/* + 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 milliseconds: + delay(sensorValue); + // turn the ledPin off: + digitalWrite(ledPin, LOW); + // stop the program for for 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); +} diff --git a/basic/analog_input/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino b/basic/analog_input/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino new file mode 100644 index 0000000..aa18a9e --- /dev/null +++ b/basic/analog_input/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino @@ -0,0 +1,71 @@ +/* + 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 milliseconds: + delay(sensorValue); + // turn the ledPin off: + digitalWrite(ledPin, LOW); + // stop the program for for 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); + +} + + + diff --git a/basic/blinks/blink_1_variabili/blink_1_variabili.ino b/basic/blinks/blink_1_variabili/blink_1_variabili.ino index 7a5a46a..8bb3fb7 100644 --- a/basic/blinks/blink_1_variabili/blink_1_variabili.ino +++ b/basic/blinks/blink_1_variabili/blink_1_variabili.ino @@ -16,7 +16,7 @@ // 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; @@ -36,8 +36,8 @@ void loop() { 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); } diff --git a/basic/buttons/button_presses_LED_6/button_presses_LED_6.ino b/basic/buttons/button_presses_LED_6/button_presses_LED_6.ino deleted file mode 100644 index 771bbcc..0000000 --- a/basic/buttons/button_presses_LED_6/button_presses_LED_6.ino +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 -} - - - diff --git a/basic/buttons/button_presses_LED_blinking_deBounce_8/button_presses_LED_blinking_deBounce_8.ino b/basic/buttons/button_presses_LED_blinking_deBounce_8/button_presses_LED_blinking_deBounce_8.ino deleted file mode 100644 index 3116b42..0000000 --- a/basic/buttons/button_presses_LED_blinking_deBounce_8/button_presses_LED_blinking_deBounce_8.ino +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 -} - - - - - - - - diff --git a/basic/buttons/button_presses_LED_deBounce_7/button_presses_LED_deBounce_7.ino b/basic/buttons/button_presses_LED_deBounce_7/button_presses_LED_deBounce_7.ino deleted file mode 100644 index 1e0e203..0000000 --- a/basic/buttons/button_presses_LED_deBounce_7/button_presses_LED_deBounce_7.ino +++ /dev/null @@ -1,35 +0,0 @@ -/* - * 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 -} - - - - diff --git a/basic/buttons/button_presses_count_down_5/button_presses_count_down_5.ino b/basic/buttons/button_presses_count_down_5/button_presses_count_down_5.ino index 7e43837..1d60d73 100644 --- a/basic/buttons/button_presses_count_down_5/button_presses_count_down_5.ino +++ b/basic/buttons/button_presses_count_down_5/button_presses_count_down_5.ino @@ -5,7 +5,7 @@ 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 diff --git a/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino b/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino index 20d15d0..dac48cb 100644 --- a/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino +++ b/basic/buttons/button_presses_counter_AND_4/button_presses_counter_AND_4.ino @@ -8,7 +8,7 @@ int ultimoStato; // variable to hold the last button state 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 @@ -17,7 +17,7 @@ void setup() { 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 "); diff --git a/basic/buttons/button_state_3/button_state_3.ino b/basic/buttons/button_state_3/button_state_3.ino index 2bc683b..c008ff1 100644 --- a/basic/buttons/button_state_3/button_state_3.ino +++ b/basic/buttons/button_state_3/button_state_3.ino @@ -5,9 +5,9 @@ */ -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 @@ -18,10 +18,10 @@ void setup() { 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... @@ -29,6 +29,6 @@ void loop(){ } } - ultimoStato = statoAttuale; // save the new state in our variable + ultimoStato = statoAttuale; // Aggiorna lo stato finale al valore attuale } diff --git a/basic/buttons/button_state_4_state/button_state_4_state.ino b/basic/buttons/button_state_4_state/button_state_4_state.ino index ef6ae05..a6a4e05 100644 --- a/basic/buttons/button_state_4_state/button_state_4_state.ino +++ b/basic/buttons/button_state_4_state/button_state_4_state.ino @@ -14,16 +14,16 @@ int ledStatus; // varabile per mantenere lo stato del led 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 @@ -34,7 +34,7 @@ void loop(){ } } - 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 } diff --git a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino index 42fd3a0..3cbc869 100644 --- a/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino +++ b/basic/buttons/button_state_4_state_and_condition/button_state_4_state_and_condition.ino @@ -6,23 +6,23 @@ */ 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 @@ -32,7 +32,7 @@ void loop(){ 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 } diff --git a/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino b/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino new file mode 100644 index 0000000..83230a9 --- /dev/null +++ b/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino @@ -0,0 +1,52 @@ +/* + 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 + + +} + + + + + + + + diff --git a/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino b/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino deleted file mode 100644 index 83230a9..0000000 --- a/basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino +++ /dev/null @@ -1,52 +0,0 @@ -/* - 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 - - -} - - - - - - - - diff --git a/basic/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino b/basic/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino deleted file mode 100644 index a02701e..0000000 --- a/basic/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino +++ /dev/null @@ -1,40 +0,0 @@ -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(); -} - - - - - diff --git a/basic/pwm/pwm_0_manuale/pwm_0_manuale.ino b/basic/pwm/pwm_0_manuale/pwm_0_manuale.ino new file mode 100644 index 0000000..12adae7 --- /dev/null +++ b/basic/pwm/pwm_0_manuale/pwm_0_manuale.ino @@ -0,0 +1,43 @@ +/* + 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); +} diff --git a/basic/pwm/pwm_1_while_byte/pwm_1_while_byte.ino b/basic/pwm/pwm_1_while_byte/pwm_1_while_byte.ino new file mode 100644 index 0000000..de1a994 --- /dev/null +++ b/basic/pwm/pwm_1_while_byte/pwm_1_while_byte.ino @@ -0,0 +1,27 @@ +/*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' + + diff --git a/basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino b/basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino new file mode 100644 index 0000000..12e6e78 --- /dev/null +++ b/basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino @@ -0,0 +1,36 @@ +/* + 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 ); + } +} + + + + diff --git a/basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino b/basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino new file mode 100644 index 0000000..54cbb43 --- /dev/null +++ b/basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino @@ -0,0 +1,40 @@ +/* +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); +} + + + + + + diff --git a/basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino b/basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino new file mode 100644 index 0000000..e31396e --- /dev/null +++ b/basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino @@ -0,0 +1,29 @@ +/* + 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()? +} + diff --git a/basic/pwm_1_while_byte/pwm_1_while_byte.ino b/basic/pwm_1_while_byte/pwm_1_while_byte.ino deleted file mode 100644 index de1a994..0000000 --- a/basic/pwm_1_while_byte/pwm_1_while_byte.ino +++ /dev/null @@ -1,27 +0,0 @@ -/*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' - - diff --git a/basic/pwm_2_for_loop/pwm_2_for_loop.ino b/basic/pwm_2_for_loop/pwm_2_for_loop.ino deleted file mode 100644 index 12e6e78..0000000 --- a/basic/pwm_2_for_loop/pwm_2_for_loop.ino +++ /dev/null @@ -1,36 +0,0 @@ -/* - 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 ); - } -} - - - - diff --git a/basic/serial_debug/serial_hello_world/serial_hello_world.ino b/basic/serial_debug/serial_hello_world/serial_hello_world.ino new file mode 100644 index 0000000..7f8e814 --- /dev/null +++ b/basic/serial_debug/serial_hello_world/serial_hello_world.ino @@ -0,0 +1,32 @@ +/* + * 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() +} + + + + diff --git a/basic/serial_debug/switch_test_serial/switch_test_serial.ino b/basic/serial_debug/switch_test_serial/switch_test_serial.ino new file mode 100644 index 0000000..9fa2b7d --- /dev/null +++ b/basic/serial_debug/switch_test_serial/switch_test_serial.ino @@ -0,0 +1,19 @@ +/* + * 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); +} diff --git a/basic/serial_hello_world/serial_hello_world.ino b/basic/serial_hello_world/serial_hello_world.ino deleted file mode 100644 index 7f8e814..0000000 --- a/basic/serial_hello_world/serial_hello_world.ino +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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() -} - - - - diff --git a/basic/switch_LED_2/switch_LED_2.ino b/basic/switch_LED_2/switch_LED_2.ino deleted file mode 100644 index 8141ad0..0000000 --- a/basic/switch_LED_2/switch_LED_2.ino +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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 - } -} diff --git a/basic/switch_test_serial/switch_test_serial.ino b/basic/switch_test_serial/switch_test_serial.ino deleted file mode 100644 index 9fa2b7d..0000000 --- a/basic/switch_test_serial/switch_test_serial.ino +++ /dev/null @@ -1,19 +0,0 @@ -/* - * 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); -} diff --git a/basic_programming/blink_with_functions/blink_with_functions.ino b/basic_programming/blink_with_functions/blink_with_functions.ino deleted file mode 100644 index f798b46..0000000 --- a/basic_programming/blink_with_functions/blink_with_functions.ino +++ /dev/null @@ -1,52 +0,0 @@ -/* - 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); - - -} - diff --git a/basic_programming/conditional_test/head_tails_ino/head_tails_ino.ino b/basic_programming/conditional_test/head_tails_ino/head_tails_ino.ino deleted file mode 100644 index 7587967..0000000 --- a/basic_programming/conditional_test/head_tails_ino/head_tails_ino.ino +++ /dev/null @@ -1,61 +0,0 @@ -/* - 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); - } -} - - - diff --git a/basic_programming/leap_year_functions/leap_year_functions.ino b/basic_programming/leap_year_functions/leap_year_functions.ino deleted file mode 100644 index befac46..0000000 --- a/basic_programming/leap_year_functions/leap_year_functions.ino +++ /dev/null @@ -1,80 +0,0 @@ -/** - * 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; -} - - diff --git a/basic_programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino b/basic_programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino deleted file mode 100644 index 22fe8bf..0000000 --- a/basic_programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino +++ /dev/null @@ -1,64 +0,0 @@ -/* 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); - } -} - diff --git a/basic_programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino b/basic_programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino deleted file mode 100644 index 88c515c..0000000 --- a/basic_programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino +++ /dev/null @@ -1,67 +0,0 @@ -/* 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); - } -} - diff --git a/basic_programming/loops/switch_loop_README b/basic_programming/loops/switch_loop_README deleted file mode 100644 index 8143081..0000000 --- a/basic_programming/loops/switch_loop_README +++ /dev/null @@ -1,6 +0,0 @@ -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. diff --git a/basic_programming/pointers_c/pointers/pointers.ino b/basic_programming/pointers_c/pointers/pointers.ino deleted file mode 100644 index b148b70..0000000 --- a/basic_programming/pointers_c/pointers/pointers.ino +++ /dev/null @@ -1,45 +0,0 @@ - -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); - -} - - diff --git a/basic_programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino b/basic_programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino deleted file mode 100644 index 02642eb..0000000 --- a/basic_programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino +++ /dev/null @@ -1,49 +0,0 @@ -/* -Purpose: Illustrate pointer arithmetic - Dr. Purdum, August 20, 2012 - */ -#include -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); -} - - - - diff --git a/basic_programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino b/basic_programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino deleted file mode 100644 index 1a0ad0a..0000000 --- a/basic_programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino +++ /dev/null @@ -1,41 +0,0 @@ -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(); -} - - - - - diff --git a/basic_programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino b/basic_programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino deleted file mode 100644 index 2ebc909..0000000 --- a/basic_programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino +++ /dev/null @@ -1,47 +0,0 @@ - - -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; -} - - - - - diff --git a/optimization/analogInput_with_range/analogInput_with_range.ino b/optimization/analogInput_with_range/analogInput_with_range.ino deleted file mode 100644 index 715cc79..0000000 --- a/optimization/analogInput_with_range/analogInput_with_range.ino +++ /dev/null @@ -1,59 +0,0 @@ -/* - 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 milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for 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); -} diff --git a/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino b/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino deleted file mode 100644 index aa18a9e..0000000 --- a/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino +++ /dev/null @@ -1,71 +0,0 @@ -/* - 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 milliseconds: - delay(sensorValue); - // turn the ledPin off: - digitalWrite(ledPin, LOW); - // stop the program for for 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); - -} - - - diff --git a/programming/blink_with_functions/blink_with_functions.ino b/programming/blink_with_functions/blink_with_functions.ino new file mode 100644 index 0000000..3d8a6a9 --- /dev/null +++ b/programming/blink_with_functions/blink_with_functions.ino @@ -0,0 +1,30 @@ +/* + 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. diff --git a/programming/blink_with_functions/funzioni.ino b/programming/blink_with_functions/funzioni.ino new file mode 100644 index 0000000..25568f1 --- /dev/null +++ b/programming/blink_with_functions/funzioni.ino @@ -0,0 +1,51 @@ +// 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); +} diff --git a/programming/conditional_test/head_tails_ino/head_tails_ino.ino b/programming/conditional_test/head_tails_ino/head_tails_ino.ino new file mode 100644 index 0000000..7587967 --- /dev/null +++ b/programming/conditional_test/head_tails_ino/head_tails_ino.ino @@ -0,0 +1,61 @@ +/* + 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); + } +} + + + diff --git a/programming/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino b/programming/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino new file mode 100644 index 0000000..a02701e --- /dev/null +++ b/programming/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino @@ -0,0 +1,40 @@ +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(); +} + + + + + diff --git a/programming/leap_year_functions/leap_year_functions.ino b/programming/leap_year_functions/leap_year_functions.ino new file mode 100644 index 0000000..befac46 --- /dev/null +++ b/programming/leap_year_functions/leap_year_functions.ino @@ -0,0 +1,80 @@ +/** + * 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; +} + + diff --git a/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino b/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino new file mode 100644 index 0000000..94b3e3a --- /dev/null +++ b/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino @@ -0,0 +1,67 @@ +/* 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); + } +} + diff --git a/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino b/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino new file mode 100644 index 0000000..c2df5b4 --- /dev/null +++ b/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino @@ -0,0 +1,70 @@ +/* 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); + } +} + diff --git a/programming/loops/switch_loop_README b/programming/loops/switch_loop_README new file mode 100644 index 0000000..8143081 --- /dev/null +++ b/programming/loops/switch_loop_README @@ -0,0 +1,6 @@ +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. diff --git a/programming/operators/operator_1_basic/operator_1_basic.ino b/programming/operators/operator_1_basic/operator_1_basic.ino new file mode 100644 index 0000000..2d9a403 --- /dev/null +++ b/programming/operators/operator_1_basic/operator_1_basic.ino @@ -0,0 +1,46 @@ +/* + 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 +{ +} + + diff --git a/programming/operators/operator_2_comparison/operator_2_comparison.ino b/programming/operators/operator_2_comparison/operator_2_comparison.ino new file mode 100644 index 0000000..62243f9 --- /dev/null +++ b/programming/operators/operator_2_comparison/operator_2_comparison.ino @@ -0,0 +1,47 @@ +/* + 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 +{ +} + + diff --git a/programming/operators/operator_3_logic/operator_3_logic.ino b/programming/operators/operator_3_logic/operator_3_logic.ino new file mode 100644 index 0000000..c59017b --- /dev/null +++ b/programming/operators/operator_3_logic/operator_3_logic.ino @@ -0,0 +1,57 @@ +/* + 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 +{ +} + + + diff --git a/programming/pointers_c/pointers/pointers.ino b/programming/pointers_c/pointers/pointers.ino new file mode 100644 index 0000000..b148b70 --- /dev/null +++ b/programming/pointers_c/pointers/pointers.ino @@ -0,0 +1,45 @@ + +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); + +} + + diff --git a/programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino b/programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino new file mode 100644 index 0000000..02642eb --- /dev/null +++ b/programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino @@ -0,0 +1,49 @@ +/* +Purpose: Illustrate pointer arithmetic + Dr. Purdum, August 20, 2012 + */ +#include +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); +} + + + + diff --git a/programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino b/programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino new file mode 100644 index 0000000..1a0ad0a --- /dev/null +++ b/programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino @@ -0,0 +1,41 @@ +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(); +} + + + + + diff --git a/programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino b/programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino new file mode 100644 index 0000000..2ebc909 --- /dev/null +++ b/programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino @@ -0,0 +1,47 @@ + + +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; +} + + + + +