Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  _ ___ _______     ___ ___ ___  ___ _   _ ___ _____ ___
  2. // / |_  )__ /   \   / __|_ _| _ \/ __| | | |_ _|_   _/ __|
  3. // | |/ / |_ \ |) | | (__ | ||   / (__| |_| || |  | | \__ \
  4. // |_/___|___/___/   \___|___|_|_\\___|\___/|___| |_| |___/
  5. //
  6. // 8 pract
  7. //
  8. // Made by EvilCoder
  9. // License: CC-BY-SA 3.0
  10. // Downloaded from: https://circuits.io/circuits/3438397-8-pract
  11.  
  12. #include "Ultrasonic.h"
  13.  
  14. #define rgbCount 3
  15. #define piezo 3
  16.  
  17. int rgb[rgbCount] = { 9, 10, 11 };
  18.  
  19. int distance = 0;
  20.  
  21. int userMaxDistance = 200;
  22.  
  23. int lastLedLight = 9;
  24.  
  25. Ultrasonic sonic(12, 13);
  26.  
  27. void setup() {
  28.  
  29.   pinMode(piezo, OUTPUT);
  30.  
  31.   for (int i = 0; i < rgbCount; i++) {
  32.    pinMode(rgb[i], OUTPUT);
  33.  }
  34.  Serial.begin(9600);
  35.  Serial.setTimeout(0);
  36.  Serial.print("Enter new distance : ");
  37. }
  38.  
  39. void loop() {
  40.  
  41.  if (Serial.available()) {
  42.    int userDistance = Serial.parseInt();
  43.    Serial.print(userDistance);
  44.    Serial.println();
  45.    if (userDistance > 0 && userDistance <= 5) {
  46.      Serial.print("New max distance : ");
  47.       userMaxDistance = userDistance * 100;
  48.       Serial.print(userDistance);
  49.       Serial.println();
  50.     } else {
  51.       Serial.println("Wrong distance!");
  52.     }
  53.     Serial.print("Enter new distance : ");
  54.   }
  55.  
  56.   // Hack
  57.  
  58.   distance = sonic.Ranging(CM);
  59.  
  60.   Serial.println(distance);
  61.  
  62.   analogWrite(lastLedLight == -1 ? piezo : lastLedLight, 0);
  63.  
  64.   if (distance < 30) {
  65.    lastLedLight = 10;
  66.  } else if (distance >= 30 && distance < 50) {
  67.    lastLedLight = 11;
  68.   } else if (distance >= 50 && distance <= userMaxDistance) {
  69.    lastLedLight = 9;
  70.   } else {
  71.     lastLedLight = -1;
  72.   }
  73.  
  74.   //Serial.println(lastLedLight);
  75.  
  76.   analogWrite(lastLedLight == -1 ? piezo : lastLedLight, lastLedLight == -1 ? 150 : 255);
  77.  
  78.   delay(500);
  79. }
  80.  
  81. int getData(int min, int max)
  82. {
  83.   for (int i = 0; i < 11; i++) {
  84.    random(min, max);
  85.  }
  86.  return random(min, max);
  87. }
  88.  
  89.  
  90. //  _ ___ _______     ___ ___ ___  ___ _   _ ___ _____ ___
  91. // / |_  )__ /   \   / __|_ _| _ \/ __| | | |_ _|_   _/ __|
  92. // | |/ / |_ \ |) | | (__ | ||   / (__| |_| || |  | | \__ \
  93. // |_/___|___/___/   \___|___|_|_\\___|\___/|___| |_| |___/
  94. //
  95. // 9 pract
  96. //
  97. // Made by EvilCoder
  98. // License: CC-BY-SA 3.0
  99. // Downloaded from: https://circuits.io/circuits/3402818-9-pract
  100.  
  101. #include <IRremote.h>
  102.  
  103. #define rgbCount 3
  104. #define piezo 5
  105. #define second 1000
  106. #define led 12
  107.  
  108. IRrecv sensor(2);
  109.  
  110. decode_results results;
  111.  
  112. int rgb[rgbCount] = { 9, 10, 11 };
  113. long rgbCommands[rgbCount] = { 0x18E7, 0x7A85, 0x10EF };
  114.  
  115. bool isProgramStart = false;
  116. bool isFindButton = false;
  117. bool isPiezoPlay = false;
  118. bool isLedLight = true;
  119.  
  120. long startTimePiezo = 0;
  121. long startTimeProgram = 0;
  122. long startTimeLed = 0;
  123.  
  124. int lastPinColor = 9; // hack=
  125.  
  126. void setup() {
  127.   Serial.begin(9600);
  128.   sensor.enableIRIn();
  129.  
  130.   for (int i = 0; i < rgbCount; i++) {
  131.    pinMode(rgb[i], OUTPUT);
  132.  }
  133.  
  134.  pinMode(piezo, OUTPUT);
  135.  pinMode(led, OUTPUT);
  136. }
  137.  
  138. void loop() {
  139.  if ( sensor.decode( &results ) && !isProgramStart) {
  140.    Serial.println("Start program");
  141.    isProgramStart = true;
  142.    startTimeProgram = millis();
  143.    startTimeLed = millis() + (second / 2);
  144.    sensor.resume();
  145.    while (isProgramStart) {
  146.      if ((millis() - startTimeLed) > (second / 2)) {
  147.         digitalWrite(led, (isLedLight = !isLedLight) ? LOW : HIGH);
  148.         startTimeLed = millis();
  149.       }
  150.       if ( sensor.decode( &results )) {
  151.        if (results.value != 0 && results.value != 0xFFFF) {
  152.          for (int i = 0; i < rgbCount; i++) {
  153.            if (rgbCommands[i] == results.value) {
  154.              analogWrite(lastPinColor, 0);
  155.              lastPinColor = rgb[i];
  156.              analogWrite(rgb[i], 255);
  157.              isFindButton = true;
  158.            }
  159.          }
  160.          if (!isFindButton) {
  161.            //tone(piezo, 240, 1000);
  162.            analogWrite(piezo, 200);
  163.            Serial.println("piezo on");
  164.            startTimePiezo = millis();
  165.            isPiezoPlay = true;
  166.          }
  167.        }
  168.        Serial.println(results.value, HEX);
  169.        sensor.resume();
  170.        isFindButton = false;
  171.      }
  172.  
  173.      if (isPiezoPlay) {
  174.        if ((millis() - startTimePiezo)  > (1 * second)) {
  175.           isPiezoPlay = false;
  176.           analogWrite(piezo, LOW);
  177.           Serial.println("piezo off");
  178.         }
  179.       }
  180.       if ((millis() - startTimeProgram) > (30 * second)) {
  181.         analogWrite(piezo, LOW);
  182.         analogWrite(led, LOW);
  183.         analogWrite(lastPinColor, LOW);
  184.         isProgramStart = false;
  185.         startTimeProgram = 0;
  186.       }
  187.     }
  188.     Serial.println("Program is finished");
  189.   }
  190. }
  191.  
  192.  
  193.  
  194.  
  195. // <-------- 10 PRACT ----------->
  196.  
  197.  
  198. #define gazSensor A0
  199. #define fireSensor A1
  200. #define green 10
  201. #define red 11
  202. #define piezo 3
  203.  
  204. int middleGaz = 0;
  205. int middleLigth = 0;
  206.  
  207. int gaz = 0;
  208. int fire = 0;
  209.  
  210. void setup() {
  211.   pinMode(green, OUTPUT);
  212.   pinMode(red, OUTPUT);
  213.   pinMode(piezo, OUTPUT);
  214.   Serial.begin(9600);
  215.   Serial.println("Calibrating :");
  216.  
  217.   int countToFindMiddle = 10000;
  218.   int countDevices = 2;
  219.   long middleParameters[countDevices] = {0, 0};
  220.   int devices[countDevices] = {gazSensor, fireSensor};
  221.   for (int i = 0; i < countToFindMiddle; i++) {
  222.    for (int j = 0; j < countDevices; j++) {
  223.      middleParameters[j] += j == 1 ? 1023 - analogRead(devices[j]) : analogRead(devices[j]);
  224.    }
  225.  }
  226.  middleGaz = middleParameters[0] / countToFindMiddle;
  227.  middleLigth = middleParameters[1] / countToFindMiddle;
  228.  Serial.println("gaz = " + String(middleGaz));
  229.  Serial.println("ligth = " + String(middleLigth));
  230.  
  231.  delay(500);
  232.  //analogWrite(piezo, 240);
  233. }
  234.  
  235. void loop() {
  236.  gaz = analogRead(gazSensor);
  237.  fire = 1023 - analogRead(fireSensor);
  238.  bool isFire = digitalRead(A2);
  239.  Serial.println(isFire);
  240.  Serial.println("gaz = " + String(gaz));
  241.  
  242.  float coefficient = gaz / (float)middleGaz;
  243.  int del = 500;
  244.  int pin = 0;
  245.  int ligthLevel = 100;
  246.  bool isGreen = false;
  247.  if (coefficient > 1.2) {
  248.     pin = red;
  249.     del = 500 / 10 * coefficient;
  250.   } else if (fire / (float)middleLigth > 1.2) {
  251.     pin = green;
  252.     isGreen = true;
  253.     ligthLevel = 255;
  254.   }
  255.   if (isFire) {
  256.     if (isGreen) {
  257.       pin = 0;
  258.     }
  259.     ligthLevel = 255;
  260.     analogWrite(piezo, 230);
  261.   }
  262.  
  263.   analogWrite(pin, ligthLevel);
  264.   delay(del);
  265.   analogWrite(piezo, 0);
  266.   digitalWrite(red, LOW);
  267.   digitalWrite(green, LOW);
  268.   delay(del);
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement