Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // _ ___ _______ ___ ___ ___ ___ _ _ ___ _____ ___
- // / |_ )__ / \ / __|_ _| _ \/ __| | | |_ _|_ _/ __|
- // | |/ / |_ \ |) | | (__ | || / (__| |_| || | | | \__ \
- // |_/___|___/___/ \___|___|_|_\\___|\___/|___| |_| |___/
- //
- // 8 pract
- //
- // Made by EvilCoder
- // License: CC-BY-SA 3.0
- // Downloaded from: https://circuits.io/circuits/3438397-8-pract
- #include "Ultrasonic.h"
- #define rgbCount 3
- #define piezo 3
- int rgb[rgbCount] = { 9, 10, 11 };
- int distance = 0;
- int userMaxDistance = 200;
- int lastLedLight = 9;
- Ultrasonic sonic(12, 13);
- void setup() {
- pinMode(piezo, OUTPUT);
- for (int i = 0; i < rgbCount; i++) {
- pinMode(rgb[i], OUTPUT);
- }
- Serial.begin(9600);
- Serial.setTimeout(0);
- Serial.print("Enter new distance : ");
- }
- void loop() {
- if (Serial.available()) {
- int userDistance = Serial.parseInt();
- Serial.print(userDistance);
- Serial.println();
- if (userDistance > 0 && userDistance <= 5) {
- Serial.print("New max distance : ");
- userMaxDistance = userDistance * 100;
- Serial.print(userDistance);
- Serial.println();
- } else {
- Serial.println("Wrong distance!");
- }
- Serial.print("Enter new distance : ");
- }
- // Hack
- distance = sonic.Ranging(CM);
- Serial.println(distance);
- analogWrite(lastLedLight == -1 ? piezo : lastLedLight, 0);
- if (distance < 30) {
- lastLedLight = 10;
- } else if (distance >= 30 && distance < 50) {
- lastLedLight = 11;
- } else if (distance >= 50 && distance <= userMaxDistance) {
- lastLedLight = 9;
- } else {
- lastLedLight = -1;
- }
- //Serial.println(lastLedLight);
- analogWrite(lastLedLight == -1 ? piezo : lastLedLight, lastLedLight == -1 ? 150 : 255);
- delay(500);
- }
- int getData(int min, int max)
- {
- for (int i = 0; i < 11; i++) {
- random(min, max);
- }
- return random(min, max);
- }
- // _ ___ _______ ___ ___ ___ ___ _ _ ___ _____ ___
- // / |_ )__ / \ / __|_ _| _ \/ __| | | |_ _|_ _/ __|
- // | |/ / |_ \ |) | | (__ | || / (__| |_| || | | | \__ \
- // |_/___|___/___/ \___|___|_|_\\___|\___/|___| |_| |___/
- //
- // 9 pract
- //
- // Made by EvilCoder
- // License: CC-BY-SA 3.0
- // Downloaded from: https://circuits.io/circuits/3402818-9-pract
- #include <IRremote.h>
- #define rgbCount 3
- #define piezo 5
- #define second 1000
- #define led 12
- IRrecv sensor(2);
- decode_results results;
- int rgb[rgbCount] = { 9, 10, 11 };
- long rgbCommands[rgbCount] = { 0x18E7, 0x7A85, 0x10EF };
- bool isProgramStart = false;
- bool isFindButton = false;
- bool isPiezoPlay = false;
- bool isLedLight = true;
- long startTimePiezo = 0;
- long startTimeProgram = 0;
- long startTimeLed = 0;
- int lastPinColor = 9; // hack=
- void setup() {
- Serial.begin(9600);
- sensor.enableIRIn();
- for (int i = 0; i < rgbCount; i++) {
- pinMode(rgb[i], OUTPUT);
- }
- pinMode(piezo, OUTPUT);
- pinMode(led, OUTPUT);
- }
- void loop() {
- if ( sensor.decode( &results ) && !isProgramStart) {
- Serial.println("Start program");
- isProgramStart = true;
- startTimeProgram = millis();
- startTimeLed = millis() + (second / 2);
- sensor.resume();
- while (isProgramStart) {
- if ((millis() - startTimeLed) > (second / 2)) {
- digitalWrite(led, (isLedLight = !isLedLight) ? LOW : HIGH);
- startTimeLed = millis();
- }
- if ( sensor.decode( &results )) {
- if (results.value != 0 && results.value != 0xFFFF) {
- for (int i = 0; i < rgbCount; i++) {
- if (rgbCommands[i] == results.value) {
- analogWrite(lastPinColor, 0);
- lastPinColor = rgb[i];
- analogWrite(rgb[i], 255);
- isFindButton = true;
- }
- }
- if (!isFindButton) {
- //tone(piezo, 240, 1000);
- analogWrite(piezo, 200);
- Serial.println("piezo on");
- startTimePiezo = millis();
- isPiezoPlay = true;
- }
- }
- Serial.println(results.value, HEX);
- sensor.resume();
- isFindButton = false;
- }
- if (isPiezoPlay) {
- if ((millis() - startTimePiezo) > (1 * second)) {
- isPiezoPlay = false;
- analogWrite(piezo, LOW);
- Serial.println("piezo off");
- }
- }
- if ((millis() - startTimeProgram) > (30 * second)) {
- analogWrite(piezo, LOW);
- analogWrite(led, LOW);
- analogWrite(lastPinColor, LOW);
- isProgramStart = false;
- startTimeProgram = 0;
- }
- }
- Serial.println("Program is finished");
- }
- }
- // <-------- 10 PRACT ----------->
- #define gazSensor A0
- #define fireSensor A1
- #define green 10
- #define red 11
- #define piezo 3
- int middleGaz = 0;
- int middleLigth = 0;
- int gaz = 0;
- int fire = 0;
- void setup() {
- pinMode(green, OUTPUT);
- pinMode(red, OUTPUT);
- pinMode(piezo, OUTPUT);
- Serial.begin(9600);
- Serial.println("Calibrating :");
- int countToFindMiddle = 10000;
- int countDevices = 2;
- long middleParameters[countDevices] = {0, 0};
- int devices[countDevices] = {gazSensor, fireSensor};
- for (int i = 0; i < countToFindMiddle; i++) {
- for (int j = 0; j < countDevices; j++) {
- middleParameters[j] += j == 1 ? 1023 - analogRead(devices[j]) : analogRead(devices[j]);
- }
- }
- middleGaz = middleParameters[0] / countToFindMiddle;
- middleLigth = middleParameters[1] / countToFindMiddle;
- Serial.println("gaz = " + String(middleGaz));
- Serial.println("ligth = " + String(middleLigth));
- delay(500);
- //analogWrite(piezo, 240);
- }
- void loop() {
- gaz = analogRead(gazSensor);
- fire = 1023 - analogRead(fireSensor);
- bool isFire = digitalRead(A2);
- Serial.println(isFire);
- Serial.println("gaz = " + String(gaz));
- float coefficient = gaz / (float)middleGaz;
- int del = 500;
- int pin = 0;
- int ligthLevel = 100;
- bool isGreen = false;
- if (coefficient > 1.2) {
- pin = red;
- del = 500 / 10 * coefficient;
- } else if (fire / (float)middleLigth > 1.2) {
- pin = green;
- isGreen = true;
- ligthLevel = 255;
- }
- if (isFire) {
- if (isGreen) {
- pin = 0;
- }
- ligthLevel = 255;
- analogWrite(piezo, 230);
- }
- analogWrite(pin, ligthLevel);
- delay(del);
- analogWrite(piezo, 0);
- digitalWrite(red, LOW);
- digitalWrite(green, LOW);
- delay(del);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement