From e50f2cf8e7402ea56cd01835be9f88c53876bfd1 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 16 Mar 2015 17:53:11 +0100 Subject: [PATCH] operatori + analog --- README | 7 +- RGB_LED/ReadASCIIString/ReadASCIIString | 66 +++++++++++++++++++ basic/README | 12 ++++ .../analogInput_1/analogInput_1.ino | 52 +++++++++++++++ .../analogInput_2_serial.ino | 57 ++++++++++++++++ .../analogInput_with_range.ino | 0 .../analogInput_with_range_and_limits.ino | 0 .../blink_1_variabili/blink_1_variabili.ino | 6 +- .../button_presses_LED_6.ino | 31 --------- ...button_presses_LED_blinking_deBounce_8.ino | 50 -------------- .../button_presses_LED_deBounce_7.ino | 35 ---------- .../button_presses_count_down_5.ino | 2 +- .../button_presses_counter_AND_4.ino | 4 +- .../buttons/button_state_3/button_state_3.ino | 16 ++--- .../button_state_4_state.ino | 8 +-- .../button_state_4_state_and_condition.ino | 18 ++--- .../debounce_2_and_contratto.ino} | 0 basic/pwm/pwm_0_manuale/pwm_0_manuale.ino | 43 ++++++++++++ .../pwm_1_while_byte/pwm_1_while_byte.ino | 0 .../pwm_2_for_loop/pwm_2_for_loop.ino | 0 .../pwm_3_fade_reverser.ino | 40 +++++++++++ .../pwm_4_analog_input/pwm_4_analog_input.ino | 29 ++++++++ .../serial_hello_world/serial_hello_world.ino | 0 .../switch_test_serial/switch_test_serial.ino | 0 basic/switch_LED_2/switch_LED_2.ino | 24 ------- .../blink_with_functions.ino | 52 --------------- .../blink_with_functions.ino | 30 +++++++++ programming/blink_with_functions/funzioni.ino | 51 ++++++++++++++ .../head_tails_ino/head_tails_ino.ino | 0 ...ta_types_bin_hex_transformation_serial.ino | 0 .../leap_year_functions.ino | 0 .../loop_match_2_for_loop.ino | 5 +- .../loop_match_2_while_loop.ino | 5 +- .../loops/switch_loop_README | 0 .../operator_1_basic/operator_1_basic.ino | 46 +++++++++++++ .../operator_2_comparison.ino | 47 +++++++++++++ .../operator_3_logic/operator_3_logic.ino | 57 ++++++++++++++++ .../pointers_c/pointers/pointers.ino | 0 .../pointers_3_resetting_before_use.ino | 0 .../pointers_function_scope_break.ino | 0 .../structure_pointers_function_stack_4.ino | 0 41 files changed, 570 insertions(+), 223 deletions(-) create mode 100644 RGB_LED/ReadASCIIString/ReadASCIIString create mode 100644 basic/README create mode 100644 basic/analog_input/analogInput_1/analogInput_1.ino create mode 100644 basic/analog_input/analogInput_2_serial/analogInput_2_serial.ino rename {optimization => basic/analog_input/optimization}/analogInput_with_range/analogInput_with_range.ino (100%) rename {optimization => basic/analog_input/optimization}/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino (100%) delete mode 100644 basic/buttons/button_presses_LED_6/button_presses_LED_6.ino delete mode 100644 basic/buttons/button_presses_LED_blinking_deBounce_8/button_presses_LED_blinking_deBounce_8.ino delete mode 100644 basic/buttons/button_presses_LED_deBounce_7/button_presses_LED_deBounce_7.ino rename basic/buttons/{debounce_3_and_contratto/debounce_3_and_contratto.ino => debounce_2_and_contratto/debounce_2_and_contratto.ino} (100%) create mode 100644 basic/pwm/pwm_0_manuale/pwm_0_manuale.ino rename basic/{ => pwm}/pwm_1_while_byte/pwm_1_while_byte.ino (100%) rename basic/{ => pwm}/pwm_2_for_loop/pwm_2_for_loop.ino (100%) create mode 100644 basic/pwm/pwm_3_fade_reverser/pwm_3_fade_reverser.ino create mode 100644 basic/pwm/pwm_4_analog_input/pwm_4_analog_input.ino rename basic/{ => serial_debug}/serial_hello_world/serial_hello_world.ino (100%) rename basic/{ => serial_debug}/switch_test_serial/switch_test_serial.ino (100%) delete mode 100644 basic/switch_LED_2/switch_LED_2.ino delete mode 100644 basic_programming/blink_with_functions/blink_with_functions.ino create mode 100644 programming/blink_with_functions/blink_with_functions.ino create mode 100644 programming/blink_with_functions/funzioni.ino rename {basic_programming => programming}/conditional_test/head_tails_ino/head_tails_ino.ino (100%) rename {basic => programming}/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino (100%) rename {basic_programming => programming}/leap_year_functions/leap_year_functions.ino (100%) rename {basic_programming => programming}/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino (90%) rename {basic_programming => programming}/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino (90%) rename {basic_programming => programming}/loops/switch_loop_README (100%) create mode 100644 programming/operators/operator_1_basic/operator_1_basic.ino create mode 100644 programming/operators/operator_2_comparison/operator_2_comparison.ino create mode 100644 programming/operators/operator_3_logic/operator_3_logic.ino rename {basic_programming => programming}/pointers_c/pointers/pointers.ino (100%) rename {basic_programming => programming}/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino (100%) rename {basic_programming => programming}/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino (100%) rename {basic_programming => programming}/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino (100%) 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/optimization/analogInput_with_range/analogInput_with_range.ino b/basic/analog_input/optimization/analogInput_with_range/analogInput_with_range.ino similarity index 100% rename from optimization/analogInput_with_range/analogInput_with_range.ino rename to basic/analog_input/optimization/analogInput_with_range/analogInput_with_range.ino diff --git a/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 similarity index 100% rename from optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino rename to basic/analog_input/optimization/analogInput_with_range_and_limits/analogInput_with_range_and_limits.ino 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_3_and_contratto/debounce_3_and_contratto.ino b/basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino similarity index 100% rename from basic/buttons/debounce_3_and_contratto/debounce_3_and_contratto.ino rename to basic/buttons/debounce_2_and_contratto/debounce_2_and_contratto.ino 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_1_while_byte/pwm_1_while_byte.ino b/basic/pwm/pwm_1_while_byte/pwm_1_while_byte.ino similarity index 100% rename from basic/pwm_1_while_byte/pwm_1_while_byte.ino rename to basic/pwm/pwm_1_while_byte/pwm_1_while_byte.ino diff --git a/basic/pwm_2_for_loop/pwm_2_for_loop.ino b/basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino similarity index 100% rename from basic/pwm_2_for_loop/pwm_2_for_loop.ino rename to basic/pwm/pwm_2_for_loop/pwm_2_for_loop.ino 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/serial_hello_world/serial_hello_world.ino b/basic/serial_debug/serial_hello_world/serial_hello_world.ino similarity index 100% rename from basic/serial_hello_world/serial_hello_world.ino rename to basic/serial_debug/serial_hello_world/serial_hello_world.ino diff --git a/basic/switch_test_serial/switch_test_serial.ino b/basic/serial_debug/switch_test_serial/switch_test_serial.ino similarity index 100% rename from basic/switch_test_serial/switch_test_serial.ino rename to basic/serial_debug/switch_test_serial/switch_test_serial.ino 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_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/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/basic_programming/conditional_test/head_tails_ino/head_tails_ino.ino b/programming/conditional_test/head_tails_ino/head_tails_ino.ino similarity index 100% rename from basic_programming/conditional_test/head_tails_ino/head_tails_ino.ino rename to programming/conditional_test/head_tails_ino/head_tails_ino.ino diff --git a/basic/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 similarity index 100% rename from basic/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino rename to programming/data_types_bin_hex_transformation_serial/data_types_bin_hex_transformation_serial.ino diff --git a/basic_programming/leap_year_functions/leap_year_functions.ino b/programming/leap_year_functions/leap_year_functions.ino similarity index 100% rename from basic_programming/leap_year_functions/leap_year_functions.ino rename to programming/leap_year_functions/leap_year_functions.ino diff --git a/basic_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 similarity index 90% rename from basic_programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino rename to programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino index 22fe8bf..94b3e3a 100644 --- a/basic_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 @@ -1,5 +1,8 @@ /* Exercise 2, with a WHILE loop - Test a random number agains a value + + 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 diff --git a/basic_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 similarity index 90% rename from basic_programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino rename to programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino index 88c515c..c2df5b4 100644 --- a/basic_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 @@ -1,5 +1,8 @@ /* Exercise 2, with a WHILE loop - Test a random number agains a value + 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 diff --git a/basic_programming/loops/switch_loop_README b/programming/loops/switch_loop_README similarity index 100% rename from basic_programming/loops/switch_loop_README rename to programming/loops/switch_loop_README 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/basic_programming/pointers_c/pointers/pointers.ino b/programming/pointers_c/pointers/pointers.ino similarity index 100% rename from basic_programming/pointers_c/pointers/pointers.ino rename to programming/pointers_c/pointers/pointers.ino diff --git a/basic_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 similarity index 100% rename from basic_programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino rename to programming/pointers_c/pointers_3_resetting_before_use/pointers_3_resetting_before_use.ino diff --git a/basic_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 similarity index 100% rename from basic_programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino rename to programming/pointers_c/pointers_function_scope_break/pointers_function_scope_break.ino diff --git a/basic_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 similarity index 100% rename from basic_programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino rename to programming/pointers_c/structure_pointers_function_stack_4/structure_pointers_function_stack_4.ino -- 2.39.2