diff options
author | Luca Tringali <tringalinvent@libero.it> | 2019-03-11 22:38:22 +0100 |
---|---|---|
committer | Luca Tringali <tringalinvent@libero.it> | 2019-03-11 22:38:22 +0100 |
commit | 299ee1066f15506da34beeba6e90a44891780734 (patch) | |
tree | 3ae1e21c23b917707bba2fb5589d62d54fe034bc |
First commit
-rw-r--r-- | dissolvenza-led-distanza-ultrasuoni.ino | 41 | ||||
-rw-r--r-- | ethernet-servomotore.ino | 84 | ||||
-rw-r--r-- | scala-led-potenziometro.ino | 55 | ||||
-rw-r--r-- | temperatura-lm35.ino | 15 |
4 files changed, 195 insertions, 0 deletions
diff --git a/dissolvenza-led-distanza-ultrasuoni.ino b/dissolvenza-led-distanza-ultrasuoni.ino new file mode 100644 index 0000000..a7be908 --- /dev/null +++ b/dissolvenza-led-distanza-ultrasuoni.ino @@ -0,0 +1,41 @@ +//HC RS04 Sensore ultrasuoni +const int triggerPort = 9; +const int echoPort = 10; +const int led = 3; + +void setup() { + +pinMode(triggerPort, OUTPUT); +pinMode(echoPort, INPUT); +pinMode(led, OUTPUT); +Serial.begin(9600); +} + +void loop() { + + +digitalWrite( triggerPort, LOW ); +//invia un impulso di 10microsec su trigger +digitalWrite( triggerPort, HIGH ); +delayMicroseconds( 10 ); +digitalWrite( triggerPort, LOW ); + +long durata = pulseIn( echoPort, HIGH ); + +long distanza = 0.034 * durata / 2; + +Serial.print("distanza: "); + +//durata in millisecondi +if( durata > 38000 ){ +Serial.println("Fuori portata "); +} +else{ +Serial.print(distanza); +Serial.println(" cm"); +int fadeValue = map(durata, 0, 38000, 0, 255) +analogWrite(led, fadeValue); +} + +delay(1000); +} diff --git a/ethernet-servomotore.ino b/ethernet-servomotore.ino new file mode 100644 index 0000000..a4045e3 --- /dev/null +++ b/ethernet-servomotore.ino @@ -0,0 +1,84 @@ +#include <SPI.h> +#include <Ethernet.h> + + +#include <Servo.h> + +Servo myservo; // create servo object to control a servo +int servopin = 3; + +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address + +char serverName[] = "codice-sorgente.it"; +char serverPage[] = "/UTCTime.php?zone=Europe/Rome"; +EthernetClient client; + +////////////////////// + +void setup(){ + + if (Ethernet.begin(mac) == 0) { + Serial.println("Failed to configure Ethernet using DHCP"); + // no point in carrying on, so do nothing forevermore: + while(true); + } + + Serial.begin(9600); + myservo.attach(servopin); + Serial.println("Init"); +} + +void loop(){ + delay(3000); + sendGET(); +} + +////////////////////////// + +void sendGET() //client function to send/receive GET request data. +{ + if (client.connect(serverName, 80)) { //starts client connection, checks for connection + Serial.println("connected"); +// client.println("GET /ioprogrammo/UTCTime.php HTTP/1.0"); //download text + client.print("GET "); + client.print(serverPage); + client.println(" HTTP/1.0"); + client.print("Host: "); + client.println(serverName); + client.println(); //end of get request + } + else { + Serial.println("connection failed"); //error message if no client connect + Serial.println(); + return; + } + +String response = ""; + while(client.connected() && !client.available()) delay(1); //waits for data + while (client.connected() || client.available()) { //connected or data available + char c = client.read(); //gets byte from ethernet buffer + response += c; + } + + Serial.println(response); + int da = response.indexOf("<")+1; + int a = response.indexOf(">"); + String pageValue = response.substring(da,a); + + da = pageValue.indexOf(" ")+1; + a = pageValue.indexOf(":",da); + String hour = pageValue.substring(da,a); + int ora = hour.toInt(); + int rotazione = map(ora, 0, 23, 0, 180); //può essere 90 invece di 180 + Serial.print("Writing this value to servo: "); + Serial.println(rotazione); + myservo.write(rotazione); + + Serial.println(); + Serial.println("disconnecting."); + Serial.println("=================="); + Serial.println(); + client.stop(); //stop client + +} + diff --git a/scala-led-potenziometro.ino b/scala-led-potenziometro.ino new file mode 100644 index 0000000..62a2d45 --- /dev/null +++ b/scala-led-potenziometro.ino @@ -0,0 +1,55 @@ +int potPin = 2; // pin analogico potenziometro + +int ledPin1 = 3; //pin dei led +int ledPin2 = 4; +int ledPin3 = 5; +int ledPin4 = 6; +int ledPin5 = 7; + + +void setup() { + pinMode(ledPin1, OUTPUT); // dichiaro i ledPin come OUTPUT + pinMode(ledPin2, OUTPUT); + pinMode(ledPin3, OUTPUT); + pinMode(ledPin4, OUTPUT); + pinMode(ledPin5, OUTPUT); +} + +void loop() { + int val = 0; + val = analogRead(potPin); //inserisco in val il valore del potenziometro + int mappedval = 0; + mappedval = map(val, 0, 1023, 0, 5); //mappo il valore dall'intervallo 0-1023 a 0-5 + + if (mappedval>0) { + digitalWrite(ledPin1, HIGH); + } else { + digitalWrite(ledPin1, LOW); + } + + if (mappedval>1) { + digitalWrite(ledPin2, HIGH); + } else { + digitalWrite(ledPin2, LOW); + } + + if (mappedval>2) { + digitalWrite(ledPin3, HIGH); + } else { + digitalWrite(ledPin3, LOW); + } + + if (mappedval>3) { + digitalWrite(ledPin4, HIGH); + } else { + digitalWrite(ledPin4, LOW); + } + + if (mappedval>4) { + digitalWrite(ledPin5, HIGH); + } else { + digitalWrite(ledPin5, LOW); + } + +} + diff --git a/temperatura-lm35.ino b/temperatura-lm35.ino new file mode 100644 index 0000000..a75cb28 --- /dev/null +++ b/temperatura-lm35.ino @@ -0,0 +1,15 @@ +int pin = 1; +double temp = 0.0; + +void setup(){ + Serial.begin(9600); +} + +void loop(){ + temp = analogRead(pin)*(500/1023); + Serial.print ("Stanza 1: "); + Serial.print (temp); + Serial.print ("° C"); + Serial.println(); + delay(1000); +} |