Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 KB | None | 0 0
  1. #include <math.h>
  2. #include <Wire.h>
  3. #include <dht11.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_ADXL345_U.h>
  6. #include "DS3231.h"
  7. #include <OneWire.h>
  8. #include <IRremote.h>
  9. IRsend irsend;
  10. RTClib RTC;
  11. dht11 DHT;
  12.  
  13.  
  14. const int photoCell = A0;
  15. const int microphone = A1;
  16. const int temt = A2;
  17. const int joyX = A3;
  18. const int joyY = A4;
  19. const int thermistor = A5; //analog temp
  20. const int rotary = A6;
  21. const int moist = A7;
  22. const int lm = A8;
  23.  
  24. const int knock = 3;
  25. const int joyBut = 4;
  26. const int piranha = 5;
  27. const int photoInt = 6;
  28. const int actBuzz = 7;
  29. const int greenLed = 8;
  30. const int blueLed = 2; //can't use 9 because of IRsend
  31.  
  32. const int redLed = 10;
  33. const int yellowButton = 11;
  34. const int hallSensor = 12;
  35. const int whiteLed = 13;
  36.  
  37.  
  38. /* Arduino Rotary Encoder Tutorial
  39. *
  40. * by Dejan Nedelkovski, www.HowToMechatronics.com
  41. *
  42. */
  43. const int outputA = 49;
  44. const int outputB = 48;
  45. const int DHT11_PIN = 47;
  46. const int passBuzz = 46;
  47. const int pir = 45;
  48. const int capTouch = 44;
  49. const int echoPin = 43;
  50. const int trigPin = 42;
  51. const int DS18S20_Pin = 41;
  52. const int hugeLed = 40;
  53. const int relay = 39;
  54. const int vibe = 38;
  55. const int line = 37;
  56. const int tilt = 36;
  57. const int RECV_PIN = 35;
  58. const int flame = 34;
  59. const int obst = 33;
  60.  
  61.  
  62. OneWire ds(DS18S20_Pin);
  63. int relayState = 0;
  64. int counter = 0;
  65. int aState;
  66. int aLastState;
  67. unsigned char vibeState = 0;
  68.  
  69. int maximumRange = 200; // Maximum range needed
  70. int minimumRange = 0; // Minimum range needed
  71. long duration, distance; // Duration used to calculate distance
  72.  
  73. int val = 0; //store random int values
  74.  
  75.  
  76. //stuff for Analog Temp sensor
  77. double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation
  78. double Temp;
  79. Temp = log(((10240000/RawADC) - 10000));
  80. Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  81. Temp = Temp - 273.15; // Convert Kelvin to Celsius
  82. Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius
  83.  
  84. }
  85.  
  86.  
  87. //345 accelerometer_data
  88. Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);
  89.  
  90.  
  91. float AccelMinX = 0;
  92. float AccelMaxX = 0;
  93. float AccelMinY = 0;
  94. float AccelMaxY = 0;
  95. float AccelMinZ = 0;
  96. float AccelMaxZ = 0;
  97.  
  98. void setup(){
  99. Serial.begin(9600);
  100.  
  101. /* Initialise the sensor */
  102. if(!accel.begin())
  103. {
  104. /* There was a problem detecting the ADXL345 ... check your connections */
  105. Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
  106. delay(1000);
  107. }
  108. pinMode(joyBut,INPUT);
  109. pinMode(piranha, OUTPUT);
  110. pinMode(photoInt, INPUT);
  111. pinMode(actBuzz, OUTPUT);
  112. pinMode(passBuzz, OUTPUT);
  113. pinMode(redLed, OUTPUT);
  114. pinMode(blueLed, OUTPUT);
  115. pinMode(greenLed, OUTPUT);
  116. pinMode(yellowButton, INPUT);
  117. pinMode(hallSensor, INPUT);
  118. pinMode(whiteLed, OUTPUT);
  119. pinMode(pir, INPUT);
  120. pinMode(capTouch, INPUT);
  121. pinMode(trigPin, OUTPUT);
  122. pinMode(echoPin, INPUT);
  123. pinMode(hugeLed, OUTPUT);
  124. pinMode(relay, OUTPUT);
  125. pinMode(vibe, INPUT);
  126. pinMode(line, INPUT);
  127. pinMode(tilt, INPUT);
  128. pinMode(RECV_PIN, INPUT);
  129. pinMode(flame, INPUT);
  130. pinMode(obst, INPUT);
  131. Wire.begin();
  132. aLastState = digitalRead(outputA);
  133. }
  134.  
  135. void loop(){
  136. digitalWrite(whiteLed, LOW);
  137. digitalWrite(piranha, LOW);
  138. digitalWrite(hugeLed, LOW);
  139. digitalWrite(actBuzz, LOW);
  140. digitalWrite(passBuzz, LOW);
  141.  
  142. lightLevel();
  143. hallDetect();
  144. checkButton();
  145. rainbow();
  146. photoInter();
  147. soundLevel();
  148. checkVibe();
  149. temtLight();
  150. checkJoy();
  151. analogTemp();
  152. checkRoto();
  153. rotaryEncoder();
  154. axdl();
  155. moisture();
  156. lmTemp();
  157. checkKnock();
  158. readDht();
  159. rtcNow();
  160. readPir();
  161. touchSensor();
  162. echoRange();
  163. getTemp();
  164. checkGas();
  165. relayFlip();
  166. isLine();
  167. isTilt();
  168. irsend.sendRC5(0x0, 8);
  169. isIR();
  170. isFlame();
  171. obstCheck();
  172.  
  173. Serial.println("*********************");
  174. digitalWrite(whiteLed, HIGH);
  175. digitalWrite(piranha, HIGH);
  176. delay(5000);
  177. digitalWrite(hugeLed, HIGH);
  178. digitalWrite(actBuzz, HIGH);
  179. digitalWrite(passBuzz, HIGH);
  180. }
  181. //So, uh yeah. Let's get started...
  182. // void whiteLED(){
  183. // digitalWrite(white,HIGH);
  184. // delay(1000);
  185. // digitalWrite(white,LOW);
  186. // delay(1000);
  187. // }
  188.  
  189. void lightLevel(){
  190. val = analogRead(photoCell);
  191. Serial.print("light: ");
  192. Serial.println(val, DEC);
  193. }
  194.  
  195. void hallDetect(){
  196. val = digitalRead(hallSensor); // read input value
  197. if (val == HIGH) { // check if the input is HIGH
  198. Serial.println("No Magnet");
  199. } else {
  200. Serial.println("Magnet");
  201. }
  202. }
  203.  
  204. void checkButton(){
  205. val = digitalRead(yellowButton); // read input value
  206. if (val == HIGH) { // check if the input is HIGH
  207. Serial.println("Not Pushed");
  208. } else {
  209. Serial.println("Pushed");
  210. }
  211. }
  212.  
  213. void rainbow(){
  214. for(val=255; val>0; val--)
  215. {analogWrite(redLed, val);
  216. analogWrite(blueLed, 255-val);
  217. analogWrite(greenLed, 128-val);
  218. }
  219. for(val=0; val<255; val++)
  220. {analogWrite(redLed, val);
  221. analogWrite(blueLed, 255-val);
  222. analogWrite(greenLed, 128-val);
  223. }
  224. digitalWrite(redLed, HIGH);
  225. digitalWrite(blueLed, HIGH);
  226. digitalWrite(greenLed, HIGH);
  227. }
  228.  
  229. void photoInter(){
  230. val = digitalRead(photoInt);
  231. if (val == HIGH) {
  232. Serial.println("Beam Broken");
  233. }else{
  234. Serial.println("Beam Solid");
  235. }
  236. }
  237.  
  238. void soundLevel(){
  239. val=analogRead(microphone);
  240. Serial.print("SoundLvl: ");
  241. Serial.println(val,DEC);
  242. }
  243.  
  244. void temtLight(){
  245. val = analogRead(temt);
  246. Serial.print("TEMT Light: ");
  247. Serial.println(val);
  248. }
  249.  
  250. void checkJoy(){
  251. int x,y,z;
  252. x=analogRead(joyX);
  253. y=analogRead(joyY);
  254. z=digitalRead(joyBut);
  255. Serial.print("X: ");
  256. Serial.print(x ,DEC);
  257. Serial.print(",");
  258. Serial.print("Y: ");
  259. Serial.print(y ,DEC);
  260. Serial.print(",");
  261. if(z == HIGH){
  262. Serial.println(" Button Pushed");
  263. }else{
  264. Serial.println(" Button NOT Pushed");
  265. }
  266. }
  267.  
  268. void analogTemp(){
  269. Serial.print(Thermister(analogRead(thermistor)));
  270. Serial.println("c");
  271. }
  272.  
  273. void checkRoto(){
  274. int rot=analogRead(rotary);
  275. Serial.print("Rotary Value: ");
  276. Serial.println(val,DEC);
  277. }
  278.  
  279. void rotaryEncoder(){
  280. aState = digitalRead(outputA); // Reads the "current" state of the outputA
  281. // If the previous and the current state of the outputA are different, that means a Pulse has occured
  282. if (aState != aLastState){
  283. // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
  284. if (digitalRead(outputB) != aState) {
  285. counter ++;
  286. } else {
  287. counter --;
  288. }
  289. Serial.print("Position: ");
  290. Serial.println(counter);
  291. }
  292. aLastState = aState; // Updates the previous state of the outputA with the current state
  293. }
  294.  
  295. void axdl(){
  296. AccelMinX = 0;
  297. AccelMaxX = 0;
  298. AccelMinY = 0;
  299. AccelMaxY = 0;
  300. AccelMinZ = 0;
  301. AccelMaxZ = 0;
  302. sensors_event_t accelEvent;
  303. accel.getEvent(&accelEvent);
  304.  
  305. if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
  306. if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
  307.  
  308. if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
  309. if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;
  310.  
  311. if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
  312. if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;
  313.  
  314. Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print(" ");Serial.print(AccelMinY); Serial.print(" "); Serial.print(AccelMinZ); Serial.println();
  315. Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print(" ");Serial.print(AccelMaxY); Serial.print(" "); Serial.print(AccelMaxZ); Serial.println();
  316.  
  317. }
  318.  
  319. void moisture(){
  320. val=analogRead(moist);
  321. Serial.print("Moisture: ");
  322. Serial.println(val,DEC);
  323. }
  324.  
  325. void lmTemp(){
  326. val = analogRead(lm);
  327. float mv = ( val/1024.0)*5000;
  328. float cel = mv/10;
  329. float farh = (cel*9)/5 + 32;
  330.  
  331. Serial.print("LM35: ");
  332. Serial.print(cel);
  333. Serial.print("*C");
  334. Serial.println();
  335. }
  336.  
  337. void checkKnock(){
  338. val=digitalRead(knock);
  339. if(val==HIGH){
  340. Serial.println("Shock Detected");
  341. }else{
  342. Serial.println("No Shock Detected");
  343. }
  344. }
  345.  
  346. void readDht(){
  347. val = DHT.read(DHT11_PIN);
  348. Serial.print("DHT: ");
  349. Serial.print(DHT.humidity,1);
  350. Serial.print("%,\t");
  351. Serial.print(DHT.temperature,1);
  352. Serial.println(" deg C");
  353. }
  354.  
  355. void rtcNow(){
  356. DateTime now = RTC.now();
  357.  
  358. Serial.print(now.year(), DEC);
  359. Serial.print('/');
  360. Serial.print(now.month(), DEC);
  361. Serial.print('/');
  362. Serial.print(now.day(), DEC);
  363. Serial.print(' ');
  364. Serial.print(now.hour(), DEC);
  365. Serial.print(':');
  366. Serial.print(now.minute(), DEC);
  367. Serial.print(':');
  368. Serial.print(now.second(), DEC);
  369. Serial.println();
  370. }
  371.  
  372. void readPir(){
  373. val = digitalRead(pir);
  374. if(val == 1){
  375. Serial.println("Somebody is in this area!");
  376. }else{
  377. Serial.println("No motion!");
  378. }
  379. }
  380.  
  381. void touchSensor(){
  382. if(digitalRead(capTouch)==HIGH) {
  383. Serial.println("Capacitive Touched");
  384. }else{
  385. Serial.println("Not touched");
  386. }
  387. }
  388.  
  389. void echoRange(){
  390. digitalWrite(trigPin, LOW);
  391. delayMicroseconds(2);
  392.  
  393. digitalWrite(trigPin, HIGH);
  394. delayMicroseconds(10);
  395. digitalWrite(trigPin, LOW);
  396.  
  397. duration = pulseIn(echoPin, HIGH);
  398.  
  399. //Calculate the distance (in cm) based on the speed of sound.
  400. distance = duration/58.2;
  401. if (distance >= maximumRange || distance <= minimumRange){
  402. Serial.println("Out of Range");
  403. }else {
  404. Serial.print(distance);
  405. Serial.println(" cm");
  406. }
  407. }
  408.  
  409. void getTemp(){
  410. //returns the temperature from one DS18S20 in DEG Celsius
  411.  
  412. byte data[12];
  413. byte addr[8];
  414.  
  415. if ( !ds.search(addr)) {
  416. //no more sensors on chain, reset search
  417. ds.reset_search();
  418. return -1000;
  419. }
  420.  
  421. if ( OneWire::crc8( addr, 7) != addr[7]) {
  422. Serial.println("CRC is not valid!");
  423. return -1000;
  424. }
  425.  
  426. if ( addr[0] != 0x10 && addr[0] != 0x28) {
  427. Serial.print("Device is not recognized");
  428. return -1000;
  429. }
  430.  
  431. ds.reset();
  432. ds.select(addr);
  433. ds.write(0x44,1); // start conversion, with parasite power on at the end
  434.  
  435. byte present = ds.reset();
  436. ds.select(addr);
  437.  
  438. ds.write(0xBE); // Read Scratchpad
  439.  
  440.  
  441. for (int i = 0; i < 9; i++) { // we need 9 bytes
  442. data[i] = ds.read();
  443. }
  444. ds.reset_search();
  445.  
  446. byte MSB = data[1];
  447. byte LSB = data[0];
  448.  
  449. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  450. float TemperatureSum = tempRead / 16;
  451. Serial.print("DS18b20: ");
  452. Serial.print(TemperatureSum);
  453. Serial.println(" deg C");
  454. }
  455.  
  456. void checkGas(){
  457. val = analogRead(10);//gas sensor
  458. Serial.print("Gas: ");
  459. Serial.println(val,DEC);
  460.  
  461. val = analogRead(9);//alcohol sensor
  462. Serial.print("Alc: ");
  463. Serial.println(val,DEC);
  464. }
  465.  
  466. void relayFlip(){
  467. if(relayState == 0){
  468. digitalWrite(relay,HIGH);
  469. relayState = 1;
  470. Serial.println("Setting Relay High");
  471. }else{
  472. digitalWrite(relay,LOW);
  473. relayState = 0;
  474. Serial.println("Setting Relay Low");
  475. }
  476. }
  477.  
  478. void checkVibe(){
  479. val = digitalRead(vibe);
  480. // Serial.println(val);
  481. if(val != 0){
  482. Serial.println("Vibration detected");
  483. }else{
  484. Serial.println("Vibration NOT detected");
  485. }
  486. }
  487.  
  488. void isLine(){
  489. val = digitalRead(line);
  490. if(val != 0){
  491. Serial.println("No Line!");
  492. }else{
  493. Serial.println("Line!!!!!!!!!!!!!!!!!");
  494. }
  495. }
  496.  
  497. void isTilt(){
  498. val = digitalRead(tilt);
  499. if(val != 0){
  500. Serial.println("Steady");
  501. }else{
  502. Serial.println("TILT!");
  503. }
  504. }
  505.  
  506. void isIR(){
  507. val = digitalRead(RECV_PIN);
  508. if(val == 0){
  509. Serial.println("IR Detected");
  510. }else{
  511. Serial.println("No IR Detected");
  512. }
  513. }
  514.  
  515. void isFlame(){
  516. val = digitalRead(flame);
  517. if (val == HIGH) {
  518. Serial.println("Flame NOT Detected");
  519. }
  520. else {
  521. Serial.println("Flame Detected");
  522. }
  523. }
  524.  
  525. void obstCheck(){
  526. val = digitalRead(obst);
  527. if (val == HIGH) {
  528. Serial.println("Object NOT Detected");
  529. }
  530. else {
  531. Serial.println("Object Detected");
  532. }
  533. }
  534.  
  535. //Sketch uses 13512 bytes (5%) of program storage space. Maximum is 253952 bytes.
  536. //Global variables use 1405 bytes (17%) of dynamic memory, leaving 6787 bytes for local variables. Maximum is 8192 bytes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement