#include <Wire.h> #include <SoftwareSerial.h> #define trigPin 3 #define echoPin 2 #define RX 1 #define TX 0 SoftwareSerial window(RX, TX); void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); window.begin(9600); Wire.begin(); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if(distance <= 10) { Wire.beginTransmission(8); Wire.write(0); Wire.endTransmission(); window.println("Human within 10cm"); } else if(distance > 10 && distance <=20) { Wire.beginTransmission(8); Wire.write(1); Wire.endTransmission(); window.println("Human within 20cm"); } else if(distance > 20 && distance <=30) { Wire.beginTransmission(8); Wire.write(2); Wire.endTransmission(); window.println("Human within 30cm"); } delay(1000); }
#include <Wire.h> #define led1 2 #define led2 3 #define led3 1 void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); Wire.begin(8); Wire.onReceive(receiveEvent); } void loop() { delay(1000); } void receiveEvent() { int x = Wire.read(); if (x == 0) { digitalWrite(led1, HIGH); digitalWrite(led2, LOW); digitalWrite(led3, HIGH); } else if (x == 1) { digitalWrite(led1, HIGH); digitalWrite(led2, LOW); digitalWrite(led3, LOW); } else if (x == 2) { digitalWrite(led1, HIGH); digitalWrite(led2, HIGH); digitalWrite(led3, LOW); } }
#include <Wire.h> #include <SoftwareSerial.h> #define trigPin 3 #define echoPin 2 #define RX 1 #define TX 0 SoftwareSerial window(RX, TX); void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); window.begin(9600); Wire.begin(); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if(distance <= 2) { Wire.beginTransmission(8); Wire.write(0); Wire.endTransmission(); window.println("Human within 2cm"); } else if(distance > 2 && distance <=4) { Wire.beginTransmission(8); Wire.write(1); Wire.endTransmission(); window.println("Human within 4cm"); } else if(distance > 4 && distance <=6) { Wire.beginTransmission(8); Wire.write(2); Wire.endTransmission(); window.println("Human within 6cm"); } else if(distance > 6 && distance <=8) { Wire.beginTransmission(8); Wire.write(3); Wire.endTransmission(); window.println("Human within 8cm"); } else if(distance > 8 && distance <=10) { Wire.beginTransmission(8); Wire.write(4); Wire.endTransmission(); window.println("Human within 10cm"); } else if(distance > 10 && distance <=12) { Wire.beginTransmission(8); Wire.write(5); Wire.endTransmission(); window.println("Human within 12cm"); } else if(distance > 12 && distance <=14) { Wire.beginTransmission(8); Wire.write(6); Wire.endTransmission(); window.println("Human within 14cm"); } else if(distance > 14 && distance <=16) { Wire.beginTransmission(8); Wire.write(7); Wire.endTransmission(); window.println("Human within 16cm"); } else if(distance > 16 && distance <=18) { Wire.beginTransmission(8); Wire.write(8); Wire.endTransmission(); window.println("Human within 18cm"); } else if(distance > 18 && distance <=20) { Wire.beginTransmission(8); Wire.write(9); Wire.endTransmission(); window.println("Human within 20cm"); } else if(distance > 20 && distance <=22) { Wire.beginTransmission(8); Wire.write(10); Wire.endTransmission(); window.println("Human within 22cm"); } else if(distance > 22 && distance <=24) { Wire.beginTransmission(8); Wire.write(11); Wire.endTransmission(); window.println("Human within 24cm"); } else if(distance > 24 && distance <=26) { Wire.beginTransmission(8); Wire.write(12); Wire.endTransmission(); window.println("Human within 26cm"); } else if(distance > 26 && distance <=28) { Wire.beginTransmission(8); Wire.write(13); Wire.endTransmission(); window.println("Human within 28cm"); } delay(100); }
#include <Wire.h> #define led1 2 #define led2 3 #define led3 1 void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); Wire.begin(8); Wire.onReceive(receiveEvent); } void loop() { delay(1000); } void receiveEvent() { int x = Wire.read(); if (x == 0) { RGB_color(255,0, 0); // Red } else if (x == 1) { RGB_color(0,255, 0); // Green } else if (x == 2) { RGB_color(0, 0,255); // Blue } else if (x == 3) { RGB_color(255, 147, 41); // Candle } else if (x == 4) { RGB_color(201, 226, 255); // overcast sky } else if (x == 5) { RGB_color(64, 156, 255); // clear blue sky } else if (x == 6) { RGB_color(244, 255, 250); // standard fluorecent } else if (x == 7) { RGB_color(255, 239, 247); // grow light fluorescent } else if (x == 8) { RGB_color(167,0,255); // black light fluorecent } else if (x == 9) { RGB_color(0, 255, 255); // Cyan } else if (x == 10) { RGB_color(255, 0, 255); // Magenta } else if (x == 11) { RGB_color(255, 255, 0); // Yellow } else if (x == 12) { RGB_color(255, 255, 255); // White } else if (x == 13) { RGB_color(0, 0, 0); // White } } void RGB_color(int red_light_value, int green_light_value, int blue_light_value) { analogWrite(led2, 255-red_light_value); analogWrite(led3, 255-green_light_value); analogWrite(led1,255-blue_light_value); }
Powered by w3.css