From d19cffa69bc2d538de8b73f854f5c3163e231f21 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Mon, 20 Apr 2015 19:19:33 +0200 Subject: [PATCH] rgb --- RGB_LED/rgb_0/rgb_0.ino | 42 ++++++++++++ RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino | 66 +++++++++++++++++++ RGB_LED/rgb_1_all_color/rgb_1_all_color.ino | 8 +++ RGB_LED/rgb_2_pwm/rgb_2_pwm.ino | 13 +++- .../rgb_3_ReadASCIIString.ino | 23 ++++++- piezo/piezo_5_Knock/piezo_5_Knock.ino | 55 ++++++++++++++++ 6 files changed, 202 insertions(+), 5 deletions(-) create mode 100644 RGB_LED/rgb_0/rgb_0.ino create mode 100644 RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino create mode 100644 piezo/piezo_5_Knock/piezo_5_Knock.ino diff --git a/RGB_LED/rgb_0/rgb_0.ino b/RGB_LED/rgb_0/rgb_0.ino new file mode 100644 index 0000000..ea32ca6 --- /dev/null +++ b/RGB_LED/rgb_0/rgb_0.ino @@ -0,0 +1,42 @@ +/* + Adafruit Arduino - Lesson 3. RGB LED + + RGB LED: mpostare i colori per un LED RGB + common anode + + Schema: http://lab.piffa.net/schemi/rgb.jpg + */ + +int redPin = 11; +int greenPin = 10; +int bluePin = 9; + + + +void setup() +{ + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(bluePin, OUTPUT); +} + +void loop() +{ + analogWrite(redPin, 255); + analogWrite(greenPin,255); + analogWrite(bluePin, 255); +} + +/* Domande: + + 1. Accendere il LED nei vari colori + 2. Come scrivere le istruzioni analog Write in modo da sottrarre i valori? + 3. Scrivere una funzione che accetti 3 parametri per impostare i colori + 4. Scrivere una funzione che accetti i colori in esadecimale + - http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php + 5. Scrivere una funzione che accetti come parametro il nome del colore + es "blue" e imposti il LED. + */ + + + diff --git a/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino b/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino new file mode 100644 index 0000000..475de32 --- /dev/null +++ b/RGB_LED/rgb_0_soluzione/rgb_0_soluzione.ino @@ -0,0 +1,66 @@ +/* + Adafruit Arduino - Lesson 3. RGB LED + + RGB LED: mpostare i colori per un LED RGB + common anode + */ + +int redPin = 11; +int greenPin = 10; +int bluePin = 9; + + + +void setup() +{ + pinMode(redPin, OUTPUT); + pinMode(greenPin, OUTPUT); + pinMode(bluePin, OUTPUT); +} + +void loop() +{ + setColor(0xFF,0x00,0x00) ; // imposta il LED in rosso + + // setName("green") ; +} + +// Funzioni: +void setColor(int red, int green, int blue) +// Imposta i colori di un LED RGB Common Anodote +// in esadecimale +{ + analogWrite(redPin, 255 -red); + analogWrite(greenPin, 255 - green); + analogWrite(bluePin, 255 - blue); +} + +void setName(String colorName) +// Imposta i colori di un LED RGB Common Anodote +// tramite una stringa +{ + if (colorName == "red") { + analogWrite(redPin, 0 ); + analogWrite(greenPin, 255 ); + analogWrite(bluePin, 255 ); + } + else if (colorName == "green") { + analogWrite(redPin, 255 ); + analogWrite(greenPin, 0 ); + analogWrite(bluePin, 255 ); + } + // ... +} +/* Hints: + +1. Per usare un solo valore esadecimale per settare i colori: + - http://ardx.org/src/code/CIRC12-code-MB-SPAR.txt + + */ + + + + + + + diff --git a/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino b/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino index 29969ad..546d636 100644 --- a/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino +++ b/RGB_LED/rgb_1_all_color/rgb_1_all_color.ino @@ -1,5 +1,10 @@ /* Adafruit Arduino - Lesson 3. RGB LED + + RGB LED: rotazione tra tutti i colori. + + Schema: http://lab.piffa.net/schemi/rgb.jpg + */ int redPin = 11; @@ -43,3 +48,6 @@ analogWrite(greenPin, green); analogWrite(bluePin, blue); } + + + diff --git a/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino b/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino index 9563785..8e712fc 100644 --- a/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino +++ b/RGB_LED/rgb_2_pwm/rgb_2_pwm.ino @@ -1,8 +1,17 @@ +// RGB LED PWM transizione + +// Transizione di un LED RGB tra rosso - blue -verde +// tramite PWM + // This is meant for a Common Anodote RGB LED // See all those (255 - val). -#define GREEN 9 -#define BLUE 10 + +// Schema: http://lab.piffa.net/schemi/rgb.jpg + + +#define GREEN 10 +#define BLUE 9 #define RED 11 #define delayTime 20 diff --git a/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino b/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino index 9da6b1e..d1c2a3b 100644 --- a/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino +++ b/RGB_LED/rgb_3_ReadASCIIString/rgb_3_ReadASCIIString.ino @@ -1,5 +1,7 @@ /* Reading a serial ASCII-encoded string. + + Beware: set monitor to NL NewLine only This sketch demonstrates the Serial parseInt() function. It looks for an ASCII string of comma-separated values. @@ -11,16 +13,31 @@ * blue cathode: digital pin 6 * anode: +5V + Once you have programmed the Arduino, open your Serial minitor. + Make sure you have chosen to send a newline character when sending a message. + Enter values between 0-255 for the lights in the following format : + Red,Green,Blue. + + + Once you have sent the values to the Arduino, + the attached LED will turn the color you specified, + and you will receive the HEX values in the serial monitor. + created 13 Apr 2012 by Tom Igoe This example code is in the public domain. + + + + Schema: http://lab.piffa.net/schemi/rgb.jpg + */ // pins for the LEDs: -const int redPin = 3; -const int greenPin = 5; -const int bluePin = 6; +const int redPin = 11; +const int greenPin = 10; +const int bluePin = 9; void setup() { // initialize serial: diff --git a/piezo/piezo_5_Knock/piezo_5_Knock.ino b/piezo/piezo_5_Knock/piezo_5_Knock.ino new file mode 100644 index 0000000..6f8c2c5 --- /dev/null +++ b/piezo/piezo_5_Knock/piezo_5_Knock.ino @@ -0,0 +1,55 @@ +/* Knock Sensor + + This sketch reads a piezo element to detect a knocking sound. + It reads an analog pin and compares the result to a set threshold. + If the result is greater than the threshold, it writes + "knock" to the serial port, and toggles the LED on pin 13. + + The circuit: + * + connection of the piezo attached to analog in 0 + * - connection of the piezo attached to ground + * 1-megohm resistor attached from analog in 0 to ground + + http://www.arduino.cc/en/Tutorial/Knock + + created 25 Mar 2007 + by David Cuartielles + modified 30 Aug 2011 + by Tom Igoe + + This example code is in the public domain. + + */ + + +// these constants won't change: +const int ledPin = 13; // led connected to digital pin 13 +const int knockSensor = A0; // the piezo is connected to analog pin 0 +const int threshold = 100; // threshold value to decide when the detected sound is a knock or not + + +// these variables will change: +int sensorReading = 0; // variable to store the value read from the sensor pin +int ledState = LOW; // variable used to store the last LED status, to toggle the light + +void setup() { + pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT + Serial.begin(9600); // use the serial port +} + +void loop() { + // read the sensor and store it in the variable sensorReading: + sensorReading = analogRead(knockSensor); + + // if the sensor reading is greater than the threshold: + if (sensorReading >= threshold) { + // toggle the status of the ledPin: + ledState = !ledState; + // update the LED pin itself: + digitalWrite(ledPin, ledState); + // send the string "Knock!" back to the computer, followed by newline + Serial.println("Knock!"); + } + delay(100); // delay to avoid overloading the serial port buffer +} + -- 2.39.2