+ All Categories
Home > Documents > Proiecte Arduino

Proiecte Arduino

Date post: 28-Dec-2015
Category:
Upload: syad-aly
View: 67 times
Download: 2 times
Share this document with a friend
Description:
colectie de programe arduino
48
PIR alarm // Uses a PIR sensor to detect movement, buzzes a buzzer // more info here: http://blog.makezine.com/projects/pir-sensor-arduino- alarm/ // email me, John Park, at [email protected] // based upon: // PIR sensor tester by Limor Fried of Adafruit // tone code by [email protected] int ledPin = 13; // choose the pin for the LED int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11) void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input pinMode(pinSpeaker, OUTPUT); Serial.begin(9600); } void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON playTone(300, 160); delay(150); if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { digitalWrite(ledPin, LOW); // turn LED OFF playTone(0, 0); delay(300); if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; }
Transcript
Page 1: Proiecte Arduino

PIR alarm

// Uses a PIR sensor to detect movement, buzzes a buzzer// more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/// email me, John Park, at [email protected]// based upon:// PIR sensor tester by Limor Fried of Adafruit// tone code by [email protected]

int ledPin = 13; // choose the pin for the LEDint inputPin = 2; // choose the input pin (for PIR sensor)int pirState = LOW; // we start, assuming no motion detectedint val = 0; // variable for reading the pin statusint pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input pinMode(pinSpeaker, OUTPUT); Serial.begin(9600);}

void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON playTone(300, 160); delay(150);

if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { digitalWrite(ledPin, LOW); // turn LED OFF playTone(0, 0); delay(300); if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } }}// duration in mSecs, frequency in hertzvoid playTone(long duration, int freq) { duration *= 1000; int period = (1.0 / freq) * 1000000; long elapsed_time = 0; while (elapsed_time < duration) { digitalWrite(pinSpeaker,HIGH); delayMicroseconds(period / 2); digitalWrite(pinSpeaker, LOW);

Page 2: Proiecte Arduino

delayMicroseconds(period / 2); elapsed_time += (period); }}

Charlieplexing 12 LED s

#define A 12#define B 11#define C 10#define D 9 #define PIN_CONFIG 0#define PIN_STATE 1 #define LED_COUNT 12 int matrix[LED_COUNT][2][4] = { // PIN_CONFIG PIN_STATE // A B C D A B C D { { OUTPUT, OUTPUT, INPUT, INPUT }, { HIGH, LOW, LOW, LOW } }, // AB 0 { { OUTPUT, OUTPUT, INPUT, INPUT }, { LOW, HIGH, LOW, LOW } }, // BA 1 { { INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, HIGH, LOW, LOW } }, // BC 2 { { INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, LOW, HIGH, LOW } }, // CB 3 { { OUTPUT, INPUT, OUTPUT, INPUT }, { HIGH, LOW, LOW, LOW } }, // AC 4 { { OUTPUT, INPUT, OUTPUT, INPUT }, { LOW, LOW, HIGH, LOW } }, // CA 5 { { OUTPUT, INPUT, INPUT, OUTPUT }, { HIGH, LOW, LOW, LOW } }, // AD 6 { { OUTPUT, INPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } }, // DA 7 { { INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, HIGH, LOW, LOW } }, // BD 8 { { INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } }, // DB 9 { { INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, HIGH, LOW } }, // CD 10

Page 3: Proiecte Arduino

{ { INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, LOW, HIGH } } // DC 11}; void turnOn( int led ) { pinMode( A, matrix[led][PIN_CONFIG][0] ); pinMode( B, matrix[led][PIN_CONFIG][1] ); pinMode( C, matrix[led][PIN_CONFIG][2] ); pinMode( D, matrix[led][PIN_CONFIG][3] ); digitalWrite( A, matrix[led][PIN_STATE][0] ); digitalWrite( B, matrix[led][PIN_STATE][1] ); digitalWrite( C, matrix[led][PIN_STATE][2] ); digitalWrite( D, matrix[led][PIN_STATE][3] );} void setup() {} void loop() { for( int l = 0; l < LED_COUNT; l++ ) { turnOn( l ); delay( 1000 / LED_COUNT ); }}

#define A 12#define B 11#define C 10#define D 9 #define LED_COUNT 12#define DDR_BYTE 0#define PORT_BYTE 1 byte matrix[LED_COUNT][2] = { // DDR_BYTE PORT_BYTE // ABCD ABCD { 0b00011000, 0b00010000 }, // AB 0 { 0b00011000, 0b00001000 }, // BA 1 { 0b00001100, 0b00001000 }, // BC 2 { 0b00011100, 0b00000100 }, // CB 3

Page 4: Proiecte Arduino

{ 0b00010100, 0b00010000 }, // AC 4 { 0b00010100, 0b00000100 }, // CA 5 { 0b00010010, 0b00010000 }, // AD 6 { 0b00010010, 0b00000010 }, // DA 7 { 0b00001010, 0b00001000 }, // BD 8 { 0b00001010, 0b00000010 }, // DB 9 { 0b00000110, 0b00000100 }, // CD 10 { 0b00000110, 0b00000010 } // DC 11}; void turnOn( byte led ) { DDRB = matrix[led][DDR_BYTE]; PORTB = matrix[led][PORT_BYTE];} void setup() {} void loop() { for( byte l = 0; l < LED_COUNT; l++ ) { turnOn( l ); delay( 10 ); }}

Aquaponics – Online Temperature

and   Humidity

Control funduino 8 relay channel module with arduino and ethernet shield

/* Network Relay System

Page 5: Proiecte Arduino

*/

#include "etherShield.h"#include "ETHER_28J60.h"

int i, rsize, relay[] = {8, 7, 6, 5, 3, 2, 1, 0};

static uint8_t mac[6] = {0x10, 0x10, 0x10, 0x02, 0x00, 0x00};static uint8_t ip[4] = {10, 10, 10, 200};static uint16_t port = 80;unsigned long runtime, starttime;

ETHER_28J60 ether;

void setup() { pinMode(4, OUTPUT); // REQUIRED for eKitsZone.com ENC28J60 shield!!! digitalWrite(4, HIGH); // Same as above.

// Serial.begin(57600); ether.setup(mac, ip, port); rsize = sizeof(relay) / 2; for (i = 0; i < rsize; i = i + 1) { pinMode(relay[i], OUTPUT); } runtime = 0; starttime = millis();}

void loop() { int c; char * param; char * params;

if (params = ether.serviceRequest()) { if (strstr(params, "?status")) { for (i = 0; i < rsize; i = i + 1) { ether.print(digitalRead(relay[i])); } } else { if((strlen(params) > 3) && (!(strstr(params, "favicon.ico")))) { runtime = 0; param = strtok(params, "?&"); while (param != NULL) { if (strstr(param, "p=")) { i = atoi(param + 2) - 1; } else if (strstr(param, "c=")) { c = atoi(param + 2); if (c == 1) { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } digitalWrite(relay[i], HIGH); } else { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } } } else if (strstr(param, "t=")) { runtime = (unsigned long)(atoi(param + 2)) * 1000; if (runtime < 15000) { runtime = 15000; } starttime = millis(); } param = strtok(NULL, "& ");

Page 6: Proiecte Arduino

} if (runtime == 0) { runtime = 60000; // 60 sec. default run time (takes 10 seconds for valve to open). starttime = millis(); } }

ether.print("<tt>Network Relay System\n<br>\n"); for (i = 0; i < rsize; i = i + 1) { ether.print("<br>Port #"); ether.print(i+1); ether.print(": <a href='?p="); ether.print(i+1); if (digitalRead(relay[i]) == 1) { ether.print("&c=0'>Off</a> in "); ether.print((runtime - (millis() - starttime))/1000); ether.print(" seconds.\n"); } else { ether.print("&c=1'>On</a>\n"); } } ether.print("<p><a href='/'>Refresh</a>,<a href='/?status'>Status</a>\n"); } ether.respond(); }

if ((unsigned long)(millis() - starttime) > runtime) { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } }}

Web Control Relay - Arduino

Using the same hardware and an ethernet sheild I found and modified an ethernet sketch to control the relays. This sketh controls each rely with a specific URL. I have the IP set to 192.168.1.111. The URL for pin 2 is 192.168.1.111/$1 and 192.168.1.111/$2 to toggle it on or off.pin3 is $3 and $4pin4 is $5 and $6pin5 is $7 and $8pin6 is $9 and $0

Page 7: Proiecte Arduino

pin7 is $A and $Bpin8 is $C and $Dpin9 is $E and $F

I didn't write this sketch. I just made a few mods. All credit goes to the original author. 

/*  Web Server Demo  thrown together by Randy Sarafan

Allows you to turn on and off an LED by entering different urls.

To turn it on:http://your-IP-address/$1

To turn it off:http://your-IP-address/$2

Circuit:* Ethernet shield attached to pins 10, 11, 12, 13* Connect an LED to pin D2 and put it in series with a 220 ohm resistor to ground

Based almost entirely upon Web Server by Tom Igoe and David Mellis

Edit history:created 18 Dec 2009by David A. Mellismodified 4 Sep 2010by Tom Igoe

*/

#include <SPI.h>#include <Ethernet.h>

boolean incoming = 0;

// Enter a MAC address and IP address for your controller below.// The IP address will be dependent on your local network:byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };IPAddress ip(192,168,1,111); //<<< ENTER YOUR IP ADDRESS HERE!!!

// Initialize the Ethernet server library// with the IP address and port you want to use// (port 80 is default for HTTP):EthernetServer server(80);

void setup()

Page 8: Proiecte Arduino

{  pinMode(2, OUTPUT);  pinMode(3, OUTPUT);  pinMode(4, OUTPUT);  pinMode(5, OUTPUT);  pinMode(6, OUTPUT);  pinMode(7, OUTPUT);  pinMode(8, OUTPUT);  pinMode(9, OUTPUT); 

  // start the Ethernet connection and the server:  Ethernet.begin(mac, ip);  server.begin();  Serial.begin(9600);}

void loop(){  // listen for incoming clients  EthernetClient client = server.available();  if (client) {    // an http request ends with a blank line    boolean currentLineIsBlank = true;    while (client.connected()) {      if (client.available()) {        char c = client.read();        // if you've gotten to the end of the line (received a newline        // character) and the line is blank, the http request has ended,        // so you can send a reply               //reads URL string from $ to first blank space        if(incoming && c == ' '){          incoming = 0;        }        if(c == '$'){          incoming = 1;        }               //Checks for the URL string $1 or $2        if(incoming == 1){          Serial.println(c);                   if(c == '1'){            Serial.println("ON");            digitalWrite(2, HIGH);          }          if(c == '2'){            Serial.println("OFF");

Page 9: Proiecte Arduino

            digitalWrite(2, LOW);          }          if(c == '3'){            Serial.println("ON");            digitalWrite(3, HIGH);          }            if(c == '4'){            Serial.println("OFF");            digitalWrite(3, LOW);          }                 if(c == '5'){            Serial.println("ON");            digitalWrite(4, HIGH);          }                   if(c == '6'){            Serial.println("OFF");            digitalWrite(4, LOW);          }                   if(c == '7'){            Serial.println("ON");            digitalWrite(5, HIGH);          }                   if(c == '8'){            Serial.println("OFF");            digitalWrite(5, LOW);          }                   if(c == '9'){            Serial.println("ON");            digitalWrite(6, HIGH);          }                   if(c == '0'){            Serial.println("OFF");            digitalWrite(6, LOW);          }                   if(c == 'A'){            Serial.println("ON");            digitalWrite(7, HIGH);          }                   if(c == 'B'){            Serial.println("OFF");            digitalWrite(7, LOW);          }                             if(c == 'C'){            Serial.println("ON");            digitalWrite(8, HIGH);          }                   if(c == 'D'){            Serial.println("OFF");

Page 10: Proiecte Arduino

            digitalWrite(8, LOW);          }                             if(c == 'E'){            Serial.println("ON");            digitalWrite(9, HIGH);          }                   if(c == 'F'){            Serial.println("OFF");            digitalWrite(9, LOW);          }                                    }

        if (c == '\n') {          // you're starting a new line          currentLineIsBlank = true;        }        else if (c != '\r') {          // you've gotten a character on the current line          currentLineIsBlank = false;        }      }    }    // give the web browser time to receive the data    delay(1);    // close the connection:    client.stop();  }}

Program de test modul 8 relee

int Relay1 = 13;

int Relay2 = 12;

int Relay3 = 11;

int Relay4 = 10;

int Relay5 = 9;

int Relay6 = 8;

Page 11: Proiecte Arduino

int Relay7 = 7;

int Relay8 = 6;

void setup()

{

pinMode(Relay1, OUTPUT); //Set Pin12 as output

pinMode(Relay2, OUTPUT);

pinMode(Relay3, OUTPUT);

pinMode(Relay4, OUTPUT);

pinMode(Relay5, OUTPUT);

pinMode(Relay6, OUTPUT);

pinMode(Relay7, OUTPUT);

pinMode(Relay8, OUTPUT);

}

void loop()

{

digitalWrite(Relay1, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay1, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay2, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay2, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay3, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay3, LOW); //Turn on relay

delay(2000);

Page 12: Proiecte Arduino

digitalWrite(Relay4, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay4, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay5, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay5, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay6, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay6, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay7, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay7, LOW); //Turn on relay

delay(2000);

digitalWrite(Relay8, HIGH); //Turn off relay

delay(2000);

digitalWrite(Relay8, LOW); //Turn on relay

delay(2000);

}

Control relay module with ethernet shield and some nice html shit

Page 13: Proiecte Arduino

//for use with IDE 1.0

//open serial monitor to see what the arduino receives

//use the \ slash to escape the " in the html

//for use with W5100 based ethernet shields

// this project is hosted at

// http://code.google.com/p/arduino-autohome/

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xE0 }; // <------- PUT YOUR MAC Address Here

byte ip[] = { 192, 168, 1, 102 }; // <------- PUT YOUR IP Address Here

byte gateway[] = { 192, 168, 1, 254 }; // <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here

byte subnet[] = { 255, 255, 255, 0 }; // <------- It will be as it is in most of the cases

Page 14: Proiecte Arduino

EthernetServer server(80); // <------- It's Defaulf Server Port for Ethernet Shield

String readString;

//////////////////////

void setup()

{

  pinMode(6, OUTPUT); // Pin Assignment through which relay will be controlled

  pinMode(7, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);

  

  

  //start Ethernet

  

  Ethernet.begin(mac, ip, gateway, subnet);

Page 15: Proiecte Arduino

  server.begin();

  

  //enable serial data print

  

  Serial.begin(9600);

  Serial.println("server LED test 1.0"); // so that we can know what is getting loaded

}

void loop()

{

   

  

  // Create a client connection

  EthernetClient client = server.available();

  if (client) {

    while (client.connected()) {

Page 16: Proiecte Arduino

      if (client.available()) {

        char c = client.read();

        

        //read char by char HTTP request

        if (readString.length() < 100) {

          //store characters to string

          readString += c;

          //Serial.print(c);

        }

        //if HTTP request has ended

        if (c == '\n') {

          ///////////////

          Serial.println(readString); //print to serial monitor for debuging

Page 17: Proiecte Arduino

          /* Start OF HTML Section. Here Keep everything as it is unless you understands its working */

          

          client.println("HTTP/1.1 200 OK"); //send new page

          client.println("Content-Type: text/html");

          client.println();

          //client.println("<meta http-equiv=\"refresh\" content=\"5\">");

          client.println("<HTML>");

          client.println("<HEAD>");

          client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");

          client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");

          client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://arduino-autohome.googlecode.com/svn/trunk/autohome.css\" />");

          

          

          client.println("</HEAD>");

          client.println("<body bgcolor=\"#D0D0D0\">");

Page 18: Proiecte Arduino

          

          client.println("<hr/>");

          client.println("<hr/>");

          client.println("<h4><center><img border=\"2\" src=\"https://lh3.googleusercontent.com/-C6BoJrRUFko/UEUFeCwkvdI/AAAAAAAAAOc/E7gcYvPV6r4/s960/Logo.jpg\" /></center></h4>");

          client.println("<hr/>");

          client.println("<hr/>");

          

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

         

         // Relay Control Code

          client.println("<a href=\"/?relay1on\"\">Turn On Light 1</a>");

          client.println("<a href=\"/?relay1off\"\">Turn Off Light 1</a><br />");

Page 19: Proiecte Arduino

          

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          

          client.println("<a href=\"/?relay2on\"\">Turn On Light 2</a>");

          client.println("<a href=\"/?relay2off\"\">Turn Off Light 2</a><br />");

          

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          

          client.println("<a href=\"/?relay3on\"\">Turn On Light 3</a>");

          client.println("<a href=\"/?relay3off\"\">Turn Off Light 3</a><br />");

Page 20: Proiecte Arduino

          

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          client.println("<br />");

          

          client.println("<a href=\"/?relay4on\"\">Turn On Light 4</a>");

          client.println("<a href=\"/?relay4off\"\">Turn Off Light 4</a><br />");

          client.println("<br />");

          client.println("<br />");

          

          

          

         

            // control arduino pin via ethernet Start //

          

Page 21: Proiecte Arduino

          

          if(readString.indexOf("?relay1on") >0)//checks for on

          {

            digitalWrite(6, HIGH); // set pin 4 high

            Serial.println("Led On");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");

            //client.println("Light 1 Is On");

            client.println("<br />");

        }

          else{

          if(readString.indexOf("?relay1off") >0)//checks for off

          {

            digitalWrite(6, LOW); // set pin 4 low

            Serial.println("Led Off");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

            //client.println("Light 1 Is Off");

            client.println("<br />");

Page 22: Proiecte Arduino

        }

          }

          

          if(readString.indexOf("?relay2on") >0)//checks for on

          {

            digitalWrite(7, HIGH); // set pin 4 high

            Serial.println("Led On");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");

            //client.println("Light 2 Is On");

            client.println("<br />");

          }

          else{

          if(readString.indexOf("?relay2off") >0)//checks for off

          {

            digitalWrite(7, LOW); // set pin 4 low

            Serial.println("Led Off");

Page 23: Proiecte Arduino

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

            //client.println("Light 2 Is Off");

            client.println("<br />");

          }

          }

          

           if(readString.indexOf("?relay3on") >0)//checks for on

          {

            digitalWrite(8, HIGH); // set pin 4 high

            Serial.println("Led On");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");

           // client.println("Light 3 Is On");

            client.println("<br />");

          }

          else{

          if(readString.indexOf("?relay3off") >0)//checks for off

          {

Page 24: Proiecte Arduino

            digitalWrite(8, LOW); // set pin 4 low

            Serial.println("Led Off");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

            //client.println("Light 3 Is Off");

            client.println("<br />");

          }

          }

          

          

           if(readString.indexOf("?relay4on") >0)//checks for on

          {

            digitalWrite(9, HIGH); // set pin 4 high

            Serial.println("Led On");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");

            //client.println("Light 4 Is On");

            client.println("<br />");

Page 25: Proiecte Arduino

          }

          else{

          if(readString.indexOf("?relay4off") >0)//checks for off

          {

            digitalWrite(9, LOW); // set pin 4 low

            Serial.println("Led Off");

            client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");

            //client.println("Light 4 Is Off");

            client.println("<br />");

          }

          }

          

         // control arduino pin via ethernet End //

         

         

         

          // Relay Status Display

Page 26: Proiecte Arduino

          client.println("<center>");

          client.println("<table border=\"5\">");

          client.println("<tr>");

          

          if (digitalRead(6))

          {

           client.print("<td>Light 1 is ON</td>");

           

          }

          else

          {

            client.print("<td>Light 1 is OFF</td>");

           

          }

          

          

          client.println("<br />");

Page 27: Proiecte Arduino

          

          if (digitalRead(7))

          {

           client.print("<td>Light 2 is ON</td>");

           

          }

          else

          {

            client.print("<td>Light 2 is OFF</td>");

            

          }

          

          client.println("</tr>");

          client.println("<tr>");

          

          if (digitalRead(8))

          {

Page 28: Proiecte Arduino

           client.print("<td>Light 3 is ON</td>");

           

          }

          else

          {

            client.print("<td>Light 3 is OFF</td>");

            

          }

          

         

        

         

          if (digitalRead(9))

          {

           client.print("<td>Light 4 is ON</td>");

          

          }

Page 29: Proiecte Arduino

          else

          {

            client.print("<td>Light 4 is OFF</td>");

           

          }

          

          client.println("</tr>");

          client.println("</table>");

          

          client.println("</center>");

          

          

          

          //clearing string for next read

          

          readString="";

Page 30: Proiecte Arduino

          client.println("</body>");

          client.println("</HTML>");

          delay(1);

          //stopping client

          client.stop();

        }

      }

    }

  }

}

Observatie: se alege orice adresa la shieldul ethernet pt arduino, adica alegem ce ip vrem noi unde ne cere’your ip;

Page 31: Proiecte Arduino

Cod modul relee

/* Used Arduino Mega pins and Relay ones- D46 > In1, D47 > In2, D48 > In3, D49 > In4, D50 > In5, D51 > In6, D52 > In7, D53 > In8- VCC > 5V- I GND on Relay board > Arduino's GND- II GND on Relay board > External Power source's GND (If you use external power source) */

void setup(){  for(int i=46; i<=53; i++){   pinMode(i, OUTPUT);  }}

void loop(){  for(int i=46; i<=53; i++){    int z = i-45;    digitalWrite(i, LOW);    delay(z*20);    digitalWrite(i, HIGH);    delay(z*20);  }}

Cod de test modul 8 relee pare sa fuctioneze

int relay1 = 35;int relay2 = 37;int relay3 = 39;int relay4 = 41;int relay5 = 43;int relay6 = 45;int relay7 = 47;int relay8 = 49;int i = 1000;

void setup() {

Page 32: Proiecte Arduino

pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); pinMode(relay5, OUTPUT); pinMode(relay6, OUTPUT); pinMode(relay7, OUTPUT); pinMode(relay8, OUTPUT);}void loop() { digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); digitalWrite(relay3, HIGH); digitalWrite(relay4, HIGH); digitalWrite(relay5, HIGH); digitalWrite(relay6, HIGH); digitalWrite(relay7, HIGH); digitalWrite(relay8, HIGH); if (i >= 5){ delay(i); digitalWrite(relay1, LOW); delay(i); digitalWrite(relay1, HIGH); delay(i); digitalWrite(relay2, LOW); delay(i); digitalWrite(relay2, HIGH); delay(i); digitalWrite(relay3, LOW); delay(i); digitalWrite(relay3, HIGH); delay(i); digitalWrite(relay4, LOW); delay(i); digitalWrite(relay4, HIGH); delay(i); digitalWrite(relay5, LOW); delay(i); digitalWrite(relay5, HIGH); delay(i); digitalWrite(relay6, LOW); delay(i); digitalWrite(relay6, HIGH); delay(i); digitalWrite(relay7, LOW); delay(i); digitalWrite(relay7, HIGH); delay(i); digitalWrite(relay8, LOW); delay(i); digitalWrite(relay8, HIGH);

Page 33: Proiecte Arduino

i = i/2; }else{ i = 1000;}}

Sketch pentru PIR

// http://blog.roman-mueller.ch/index.php/2013/01/26/hc-sr501-passive-

infrared-sensor-with-arduino/

// http://robotic-controls.com/learn/sensors/pir-sensor-hc-sr501

// http://nicuflorica.blogspot.ro/2014/02/senzorul-de-prezenta-hc-sr501-si-

arduino.html

#define pir A0

#define led 13

void setup() {

  Serial.begin(9600);

  pinMode(pir, INPUT);

  pinMode(led, OUTPUT);

}

void loop() {

  int val = analogRead(pir);

  float val1 = val*5.00/1023.00;

  digitalWrite(led, LOW);

  if (val <650) {

    Serial.print(val);

Page 34: Proiecte Arduino

    Serial.print("/1023 = ");

    Serial.print(val1);

    Serial.println("V - No motion"); //if the value read is low, there was

no motion

    

  }

  else {

    Serial.print(val);

    Serial.print("/1023 = ");

    Serial.print(val1);

    Serial.println("V - Motion!"); //if the value read was high, there was

motion

    digitalWrite(led, HIGH);

  }

  delay(500);

}

Pentru senzori de temperatura

#include <OneWire.h>

#include <DallasTemperature.h>

// Data wire is plugged into port 10 on the Arduino

#define ONE_WIRE_BUS 10

#define TEMPERATURE_PRECISION 12

// Setup a oneWire instance to communicate with any OneWire devices (not ju

st Maxim/Dallas temperature ICs)

OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 

DallasTemperature sensors(&oneWire);

// arrays to hold device addresses

DeviceAddress rightThermometer, midleThermometer, leftThermometer;

void setup(void)

{

  // start serial port

  Serial.begin(9600);

Page 35: Proiecte Arduino

  Serial.println("Dallas Temperature IC Control Library use by niq_ro");

  Serial.println("---------------------------------------------------");

  // Start up the library

  sensors.begin();

  DeviceAddress rightThermometer = { 0x28, 0xAC, 0x7A, 0xD4, 0x4, 0x0, 0x0,

0x7E };

  DeviceAddress midleThermometer = { 0x28, 0xF5, 0xFB, 0x58, 0x5, 0x0, 0x0,

0xA0 };

  DeviceAddress leftThermometer = { 0x28, 0xCB, 0xF0, 0x58, 0x5, 0x0, 0x0,

0x2E };

  // method 1: by index

  if (!sensors.getAddress(rightThermometer, 0)) Serial.println("Unable to

find address for Device 0");

  if (!sensors.getAddress(midleThermometer, 1)) Serial.println("Unable to

find address for Device 1");

  if (!sensors.getAddress(leftThermometer, 2)) Serial.println("Unable to

find address for Device 2");

  // show the addresses we found on the bus

  Serial.print("Device 0 Address: ");

  printAddress(rightThermometer);

  Serial.println();

  Serial.print("Device 1 Address: ");

  printAddress(midleThermometer);

  Serial.println();

  Serial.print("Device 2 Address: ");

  printAddress(leftThermometer);

  Serial.println();

  // set the resolution to 9..12 bit

  sensors.setResolution(rightThermometer, TEMPERATURE_PRECISION);

  sensors.setResolution(midleThermometer, TEMPERATURE_PRECISION);

  sensors.setResolution(leftThermometer, TEMPERATURE_PRECISION);

/*

  Serial.print("Device 0 Resolution: ");

  Serial.print(sensors.getResolution(rightThermometer), DEC); 

  Serial.println();

Page 36: Proiecte Arduino

  Serial.print("Device 1 Resolution: ");

  Serial.print(sensors.getResolution(midleThermometer), DEC); 

  Serial.println();

  

  Serial.print("Device 2 Resolution: ");

  Serial.print(sensors.getResolution(leftThermometer), DEC); 

  Serial.println();

*/

}

// function to print a device address

void printAddress(DeviceAddress device)

{

  for (uint8_t i = 0; i < 8; i++)

  {

    // zero pad the address if necessary

    if (device[i] < 16) Serial.print("0");

    Serial.print(device[i], HEX);

  }

    Serial.println(" - ");

}

// function to print the temperature for a device

void printTemperature(DeviceAddress device)

{

  float tempC = sensors.getTempC(device);

  Serial.print("Temp C: ");

  if (tempC == -127.00) {

  Serial.print("Error");

} else {

  Serial.print(tempC);

  Serial.print(" Temp F: ");

  Serial.print(DallasTemperature::toFahrenheit(tempC));

}

}

// function to print a device's resolution

void printResolution(DeviceAddress device)

{

  Serial.print("Resolution: ");

  Serial.print(sensors.getResolution(device));

  Serial.println();

}

// main function to print information about a device

void printData(DeviceAddress device)

Page 37: Proiecte Arduino

{

  Serial.print("Address: ");

  printAddress(device);

  Serial.print(" ");

  printTemperature(device);

  Serial.println();

}

void loop(void)

  DeviceAddress rightThermometer = { 0x28, 0xAC, 0x7A, 0xD4, 0x4, 0x0, 0x0,

0x7E };

  DeviceAddress midleThermometer = { 0x28, 0xF5, 0xFB, 0x58, 0x5, 0x0, 0x0,

0xA0 };

  DeviceAddress leftThermometer = { 0x28, 0xCB, 0xF0, 0x58, 0x5, 0x0, 0x0,

0x2E };

   // call sensors.requestTemperatures() to issue a global temperature

  // request to all devices on the bus

  Serial.println("---------------------------------------");

  Serial.println(" ");

  Serial.print("Requesting temperatures...");

  sensors.requestTemperatures();

  Serial.println("DONE");

  Serial.println("---------------------------------------");

  Serial.println(" ");

  // print the device information

  Serial.print("Device DS18B20 (right) ");

  printData(rightThermometer);

  Serial.println("---------------------------------------");

  Serial.print("Device MAX31820 (center) ");

  printData(midleThermometer);

  Serial.println("---------------------------------------");

  Serial.print("Device MAX31820 (left) ");

  printData(leftThermometer);

  Serial.println("---------------------------------------");

  delay(3000);

}

Variator de tensiune pentru bec cu Arduino (VII)

Page 38: Proiecte Arduino

/*

AC Light Control

 Updated by Robert Twomey <[email protected]>

 Thanks to http://www.andrewkilpatrick.org/blog/?page_id=445 

 and http://www.hoelscher-hi.de/hendrik/english/dimmer.htm

 adapted sketch by niq_ro from

 http://www.tehnic.go.ro 

 http://www.niqro.3x.ro 

 http://nicuflorica.blogspot.com 

IR Remote Kit Test

 Uses YourDuino.com IR Infrared Remote Control Kit 2

 http://arduino-direct.com/sunshop/index.php?l=product_detail&p=153

 based on code by Ken Shirriff - http://arcfn.com

 Get Library at: https://github.com/shirriff/Arduino-IRremote

Bluetooth:

// adapted sketch from http://english.cxem.net/arduino/arduino4.php

*/

#include <LiquidCrystal.h>

// use LiquidCrystal.h library for alphanumerical display 1602

LiquidCrystal lcd(13,12,11,10,9,8);

/*                                     -------------------

                                       |  LCD  | Arduino |

                                       -------------------

 LCD RS pin to digital pin 13          |  RS   |   D13   |

 LCD Enable pin to digital pin 12      |  E    |   D12   |

 LCD D4 pin to digital pin 11          |  D4   |   D11   |

 LCD D5 pin to digital pin 10          |  D5   |   D10   |

 LCD D6 pin to digital pin 9           |  D6   |    D9   |

 LCD D7 pin to digital pin 8           |  D7   |    D8   |

 LCD R/W pin to ground                 |  R/W  |   GND   |

                                       -------------------

*/

#include "IRremote.h"

//-----( Declare Constants )-----

int receiver = 7; // pin 1 of IR receiver to Arduino digital pin 7

//-----( Declare objects )-----

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results; // create instance of 'decode_results'

//-----( Declare Variables )-----

Page 39: Proiecte Arduino

#include <TimerOne.h>           // Avaiable from

http://www.arduino.cc/playground/Code/Timer1

volatile int i=0; // Variable to use as a counter

volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we

have crossed zero

int AC_pin = 3; // Output to Opto Triac

int buton1 = 4; // first button at pin 4

int buton2 = 5; // second button at pin 5

int dim2 = 0; // led control

int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff

int pas = 8; // step for count;

// version: 4m7 (15.04.2013 - Craiova, Romania) - 16 steps, 4 button & LED blue to 

red (off to MAX) 

// version: 7m6.1 (23.01.2014 - Craiova, Romania) - 16 steps, 2 button & LCD1602

int freqStep = 75; // This is the delay-per-brightness step in microseconds.

char incomingByte; // incoming data from serial 9bluetooth)

 

void setup() { // Begin setup

  Serial.begin(9600); // initialization

  

  irrecv.enableIRIn(); // Start the IR receiver (classic remote)

  pinMode(buton1, INPUT); // set buton1 pin as input

  pinMode(buton2, INPUT); // set buton1 pin as input

  pinMode(AC_pin, OUTPUT); // Set the Triac pin as output

  attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2

(interupt 0) for Zero Cross Detection

  Timer1.initialize(freqStep);                      // Initialize TimerOne library

for the freq we need

  Timer1.attachInterrupt(dim_check, freqStep);

  // Use the TimerOne Library to attach an interrupt

 lcd.begin(16, 2); // set up the LCD's number of columns and rows:

 lcd.clear(); // clear the screen

 lcd.setCursor(2, 0); // put cursor at colon 0 and row 0

 lcd.print("16 steps AC"); // print a text

 lcd.setCursor(0, 1); // put cursor at colon 0 and row 1

 lcd.print("dimmer for bulb"); // print a text

 delay (3000);

 lcd.clear(); // clear the screen

 lcd.setCursor(1, 0); // put cursor at colon 0 and row 0

 lcd.print("this sketch is"); // print a text

Page 40: Proiecte Arduino

 lcd.setCursor(1, 1); // put cursor at colon 0 and row 1

 lcd.print("made by niq_ro"); // print a text

 delay (3000);

 lcd.clear(); // clear the screen

}

void zero_cross_detect() {

  zero_cross = true; // set the boolean to true to tell our dimming

function that a zero cross has occured

  i=0;

  digitalWrite(AC_pin, LOW);

}                                 

// Turn on the TRIAC at the appropriate time

void dim_check() {

  if(zero_cross == true) {

    if(i>=dim) {

      digitalWrite(AC_pin, HIGH); // turn on light

      i=0;  // reset time step counter

      zero_cross=false; // reset zero cross detection

    } 

    else {

      i++;  // increment time step counter

    }                                

  }    

}                                      

//-----( Declare User-written Functions )-----

void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes 

{

  switch(results.value)

  {

  case 0xFFA25D:

    Serial.println(" CH- ");

    break;

  case 0xFF629D:

    Serial.println(" CH ");

    break;

  case 0xFFE21D:

    Serial.println(" CH+ ");

    break;

Page 41: Proiecte Arduino

  case 0xFF22DD:

    {

    Serial.println(" PREV ");

    dim=128;

    }

    break;

  case 0xFF02FD:

    {

    Serial.println(" NEXT ");

    dim=0;

    }

    break;

  case 0xFFC23D:

    Serial.println(" PLAY/PAUSE ");

    break;

  case 0xFFE01F:

    {

    Serial.println(" VOL- ");

    if (dim<127)

   {

    dim = dim + pas;

    if (dim>127)

    {

      dim=128; // in vechiul sketch era 127

    }

    }

    }

    break;

  case 0xFFA857:

    {

    Serial.println(" VOL+ ");

      {

  if (dim>5)

  {

     dim = dim - pas;

  if (dim<0)

    {

      dim=0;  // in vechiul sketch era 1

    }

   }

   }

Page 42: Proiecte Arduino

   }

    break;

  case 0xFF906F:

    Serial.println(" EQ ");

    break;

  case 0xFF6897:

    {

    Serial.println(" 0 ");

 // analogWrite(ledr, 0);

 // analogWrite(leda, 0);

 // analogWrite(ledv, 0);

     }

    break;

  case 0xFF9867:

    Serial.println(" 100+ ");

    break;

  case 0xFFB04F:

    Serial.println(" 200+ ");

    break;

  case 0xFF30CF:

    {

    Serial.println(" 1 ");

 // analogWrite(leda, 255);

    }

    break;

  case 0xFF18E7:

    {  

    Serial.println(" 2 ");

 // analogWrite(ledv, 255);

    }

    break;

  case 0xFF7A85:

    {

    Serial.println(" 3 ");

 // analogWrite(ledr, 255);

    }

    break;

  case 0xFF10EF:

Page 43: Proiecte Arduino

    {

    Serial.println(" 4 ");

 // analogWrite(leda, 122);

    }

    break;

  case 0xFF38C7:

    {

    Serial.println(" 5 ");

 // analogWrite(ledv, 122);

    }

    break;

  case 0xFF5AA5:

    {

    Serial.println(" 6 ");

 // analogWrite(ledr, 122);

    }

    break;

  case 0xFF42BD:

    {

    Serial.println(" 7 ");

 // analogWrite(leda, 0);

    }

    break;

  case 0xFF4AB5:

    {

    Serial.println(" 8 ");

 // analogWrite(ledv, 0);

    }

    break;

  case 0xFF52AD:

    {

    Serial.println(" 9 ");

 // analogWrite(ledr, 0);

    }

    break;

  default:

    Serial.println(" other button ");

  }

Page 44: Proiecte Arduino

}

void blustuf()

{

    incomingByte = Serial.read(); // read byte

    if(incomingByte == '0') {

       }

    if(incomingByte == '1') {

    }

    if(incomingByte == '2') {

    }

    if(incomingByte == '3') {

    }

    if(incomingByte == '4') {

    }

    if(incomingByte == '5') {

    }

    if(incomingByte == '6') {

    }

    if(incomingByte == '7') {

    }

  if(incomingByte == 'a') { //step up

  if (dim<127)

   {

    dim = dim + pas;

    if (dim>127)

    {

      dim=128; 

    }

    }  

  }

  

  if(incomingByte == 's') { //step down

  if (dim>5)

   {

    dim = dim - pas;

    if (dim<0)

    {

      dim=0; 

    }

    }  

  }

 

  if(incomingByte == 'w') { // power is 100%

    dim=0; 

  }

Page 45: Proiecte Arduino

  if(incomingByte == 'z') { // power is 0% (off)

    dim=128; 

  }

}

void stelute()

{

if (dim2<1) lcd.print("----------------");

else

if (dim2<9) lcd.print("*---------------");

else

if (dim2<17) lcd.print("-*--------------");

else

if (dim2<25) lcd.print("--*-------------");

else

if (dim2<33) lcd.print("---*------------");

else

if (dim2<41) lcd.print("----*-----------");

else

if (dim2<49) lcd.print("-----*----------");

else

if (dim2<57) lcd.print("------*---------");

else

if (dim2<65) lcd.print("-------*--------");

else

if (dim2<73) lcd.print("--------*-------");

else

if (dim2<81) lcd.print("---------*------");

else

if (dim2<89) lcd.print("----------*-----");

else

if (dim2<97) lcd.print("-----------*----");

else

if (dim2<105) lcd.print("------------*---");

else

if (dim2<113) lcd.print("-------------*--");

else

if (dim2<121) lcd.print("--------------*-");

else

if (dim2>127) lcd.print("---------------*");

}

void loop() {

  digitalWrite(buton1, HIGH);

  digitalWrite(buton2, HIGH);

Page 46: Proiecte Arduino

 if (Serial.available() > 0) blustuf(); // if bluetooth is present

 if (digitalRead(buton1) == LOW)

   {

  if (dim<127)

  {

    dim = dim + pas;

    if (dim>127)

    {

      dim=128; // in vechiul sketch era 127

    }

  }

   }

  if (digitalRead(buton2) == LOW)

   {

  if (dim>5)

  {

     dim = dim - pas;

  if (dim<0)

    {

      dim=0;  // in vechiul sketch era 1

    }

   }

   }

    while (digitalRead(buton1) == LOW) { }

    delay(10); // waiting little bit...

    while (digitalRead(buton2) == LOW) { }

    delay(10); // waiting little bit...

 

// remote

   if (irrecv.decode(&results)) // have we received an IR signal?

  {

    translateIR(); 

    irrecv.resume(); // receive the next value

  }  

 delay (100);

 lcd.setCursor(2, 0); // put cursor at colon 0 and row 0

 lcd.print("power is "); // print a text

 lcd.print(100*(128-dim)/128);

 lcd.print("% "); // print a text

lcd.setCursor(0, 1); // put cursor at colon 0 and row 1

Page 47: Proiecte Arduino

dim2=128-dim; // variable use for graphics

stelute();

}


Recommended