fbpx

www.huvitera.ee/arduino-relee

				
					// NR1 - Relee ja LED

 int Relay = 8;
  void setup()
{
  pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);     //Set Pin13 High
  pinMode(Relay, OUTPUT);     //Set Pin8 as output
}
void loop()
{
          digitalWrite(Relay, HIGH);   //Turn off relay
          delay(2000);
          digitalWrite(Relay, LOW);    //Turn on relay
          delay(2000);    
}
				
			
				
					// NR2 - Relee, LED, nupp

const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN  = 8; // Arduino pin connected to relay's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(13, OUTPUT);         //Set Pin13 as output
  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
  digitalWrite(13, HIGH);     //Set Pin13 High
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN); // read new state

  if (buttonState == LOW) {
    Serial.println("The button is being pressed");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }
  else
  if (buttonState == HIGH) {
    Serial.println("The button is unpressed");
    digitalWrite(RELAY_PIN, LOW);  // turn off
  }
}
				
			
				
					// NR3 - Relee, LED, potentsiomeeter

const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin
const int RELAY_PIN         = 8;  // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD  = 500;

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);     //Set Pin13 High
  Serial.begin(9600);                // initialize serial
}

void loop() {
  int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin
   Serial.println(analogValue);
   delay(100);

  if(analogValue > ANALOG_THRESHOLD)
    digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  else
    digitalWrite(RELAY_PIN, LOW);  // turn off Relay
}

				
			
				
					// NR4 - Relee, LED, valgusandur


const int LIGHT_SENSOR_PIN = A0; // Arduino pin connected to light sensor's  pin
const int RELAY_PIN        = 8;  // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 300;

// variables will change:
int analogValue;

void setup() {
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);     //Set Pin13 High
  Serial.begin(9600);                // initialize serial
}

void loop() {
  analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin
  Serial.println(analogValue);
   delay(100);

  if(analogValue > ANALOG_THRESHOLD)
    digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  else
    digitalWrite(RELAY_PIN, LOW);  // turn off Relay
}

				
			
				
					Convert HEX to DECIMAL
https://www.atatus.com/tools/hex-to-decimal
				
			
				
					// NR5 - Relee, LED, RFID

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 8 // the Arduino pin connects to relay

MFRC522 rfid(SS_PIN, RST_PIN);

byte authorizedUID[4] = {0xD2, 0xBC, 0x52, 0x43};

void setup() {
  Serial.begin(9600);
  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
  digitalWrite(RELAY_PIN, LOW); // deactivate the relay
  pinMode(2, OUTPUT);         //Set Pin13 as output
  digitalWrite(2, HIGH);     //Set Pin13 High
  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()) { // new tag is available
    if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

      if (rfid.uid.uidByte[0] == authorizedUID[0] &&
          rfid.uid.uidByte[1] == authorizedUID[1] &&
          rfid.uid.uidByte[2] == authorizedUID[2] &&
          rfid.uid.uidByte[3] == authorizedUID[3] ) {
        Serial.println("Authorized Tag");
        digitalWrite(RELAY_PIN, HIGH); // activate the relay for 2 seconds
        delay(2000);
        digitalWrite(RELAY_PIN, LOW);  // deactivate the relay
      }
      else
      {
        Serial.print("Unauthorized Tag with UID:");
        for (int i = 0; i < rfid.uid.size; i++) {
          Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
          Serial.print(rfid.uid.uidByte[i], HEX);
        }
        Serial.println();
      }

      rfid.PICC_HaltA(); // halt PICC
      rfid.PCD_StopCrypto1(); // stop encryption on PCD
    }
  }
}

				
			
				
					// NR6 - Relee, LED, RFID, EML

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
#define RELAY_PIN 8 // the Arduino pin connects to relay

MFRC522 rfid(SS_PIN, RST_PIN);

byte authorizedUID[4] = {0xD2, 0xBC, 0x52, 0x43};

void setup() {
  Serial.begin(9600);
  SPI.begin(); // init SPI bus
  rfid.PCD_Init(); // init MFRC522
  pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output.
  digitalWrite(RELAY_PIN, HIGH); // activate the relay
  Serial.println("Tap RFID/NFC Tag on reader");
}

void loop() {
  if (rfid.PICC_IsNewCardPresent()) { // new tag is available
    if (rfid.PICC_ReadCardSerial()) { // NUID has been readed
      MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);

      if (rfid.uid.uidByte[0] == authorizedUID[0] &&
          rfid.uid.uidByte[1] == authorizedUID[1] &&
          rfid.uid.uidByte[2] == authorizedUID[2] &&
          rfid.uid.uidByte[3] == authorizedUID[3] ) {
        Serial.println("Authorized Tag");
        digitalWrite(RELAY_PIN, LOW); // deactivate the relay for 2 seconds
      }
      else
      {
        Serial.print("Unauthorized Tag with UID:");
        for (int i = 0; i < rfid.uid.size; i++) {
          Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
          Serial.print(rfid.uid.uidByte[i], HEX);
        }
        Serial.println();
      }

      rfid.PICC_HaltA(); // halt PICC
      rfid.PCD_StopCrypto1(); // stop encryption on PCD
    }
  }
}