fbpx

www.huvitera.ee/arduino-3A

				
					//NR1 - SevSeg Test

#include "SevSeg.h"
SevSeg sevseg; 

void setup(){
  byte numDigits = 4;
  byte digitPins[] = {13, 12, 11, 10};
  byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};

  bool resistorsOnSegments = true; 
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE; 
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(90);
}

void loop(){
    sevseg.setNumber(1234, 3);
    sevseg.refreshDisplay(); 
}

				
			
				
					//NR2- TIMER

#include <math.h>

int digit_pin[] = {13, 12, 11, 10}; // PWM Display  digit pins from left to right

int speakerPin = A4;

#define DIGIT_ON  LOW
#define DIGIT_OFF  HIGH

int segA = 9; 
int segB = 2; 
int  segC = 3; 
int segD = 5; 
int segE = 6; //pin 6 is used bij display 1 for  its pwm function
int segF = 8; 
int segG = 7; 
int segPD = 4; 


int  button1=A0;
int button2=A1;
int button3=A2;
int button4=A3;

int  countdown_time = 10;

struct struct_digits {
    int digit[4];
  };


void  setup() {                
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  for (int i=0; i<4;  i++) {
    pinMode(digit_pin[i], OUTPUT);
  }

  pinMode(speakerPin,  OUTPUT);

  pinMode(button1,INPUT_PULLUP);
  pinMode(button2,INPUT_PULLUP);
  pinMode(button3,INPUT_PULLUP);
  pinMode(button4,INPUT_PULLUP);
}


void  playTone(int tone, int duration) {
  for (long k = 0; k < duration * 1000L; k  += tone * 2) {  
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}


void  lightNumber(int numberToDisplay) {

#define SEGMENT_ON  HIGH
#define SEGMENT_OFF  LOW

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA,  SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF,  SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case  1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA,  SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF,  SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case  3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE,  SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG,  SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD,  SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF,  SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case  5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE,  SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB,  SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG,  SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD,  SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF,  SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case  8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE,  SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB,  SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA,  SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC,  SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE,  SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG,  SEGMENT_OFF);
    break;  
  }
 
}



void SwitchDigit(int  digit) {
  for (int i=0; i<4; i++) {
    if (i == digit) {
      digitalWrite(digit_pin[i],  DIGIT_ON);
    } else {
      digitalWrite(digit_pin[i], DIGIT_OFF);
    }
  }
}


struct struct_digits IntToDigits(int n){
  struct struct_digits  dig;
  int zeros=0;
  int d;
  for (int i=0; i<4; i++) {
    d=n/pow(10,3-i);
    zeros += d;
    n = n - d*pow(10,3-i);
    if (zeros!=0 || i==3) {
      dig.digit[i]=d;
    } else {
      dig.digit[i]=10;
    }
  }
  return dig;
}

void PrintNumber(int n, int time) {
  struct struct_digits  dig;

  dig = IntToDigits(n);
  
  for (int i=0; i<= time/20; i++) {
    if (digitalRead(button2)==LOW) {
      return;
    }
    for (int j=0;  j<4; j++) {
      SwitchDigit(j);
      lightNumber(dig.digit[j]);
      delay(5);
    }
  }
}


bool Countdown(int n, int del){
  for (int q=n;  q>0; q--){
    PrintNumber(q,del);
    if (digitalRead(button2)==LOW) {
      return false;
    }
  }
  PrintNumber(0,0);
  playTone(1519,1000);
  return true;
}



void reset() {
  int m, zeros, d, pressed3  = 0, pressed4 = 0;
  m=countdown_time;
  struct struct_digits dig;

  dig = IntToDigits(countdown_time);
  
  while (digitalRead(button1)==HIGH)  {
    for (int j=0; j<4; j++) {
      SwitchDigit(j);
      lightNumber(dig.digit[j]);
      delay(5);
    }
    if (digitalRead(button3)==LOW) { 
      if (pressed3  == 0 || pressed3 > 30) {
        if (countdown_time > 0) {
          countdown_time  -= 1 ;
        }
        dig = IntToDigits(countdown_time);
      } 
      pressed3 += 1;
    }
    else if (digitalRead(button4)==LOW) { 
      if (pressed4 == 0 || pressed4 > 30) {
        if (countdown_time <9999)  {
          countdown_time += 1 ;
        }
        dig = IntToDigits(countdown_time);
      } 
      pressed4 += 1;
    }
    if (digitalRead(button3)==HIGH)  {
      pressed3=0;
    }
    if (digitalRead(button4)==HIGH) {
      pressed4=0;
    }
  }
}

void loop(){
  reset();
  while (!Countdown(countdown_time,962))  {
    reset();
  }
  while (digitalRead(button2)==1){};
}
				
			

Ülesanne 2 - RFID ja LCD I2C

I2C – SDA A4 ja SCL A5

				
					//NR 3 - RFID, LCD


#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Create instance for RFID reader
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Address 0x3F, 16 columns and 2 rows

// Define the custom character for LCD representing a chemistry set
byte chemistrySet[8] = {
  B01110,
  B11111,
  B01110,
  B01010,
  B11011,
  B11011,
  B11011,
  B01110
};

// Define the authorized RFID tags and their respective letters
struct Tag {
  String name;
  byte uid[4];
  char letter;
};

Tag tags[] = {
  {"TagRed", {0xD2, 0xbc, 0x52, 0x43}, 'O'},
  {"TagYellow", {0x6a, 0x95, 0x51, 0x43}, 'F'},
  {"TagGreen", {0x62, 0x35, 0x20, 0x1f}, 'H'},
  {"TagBlue", {0x62, 0x65, 0x1f, 0x1f}, 'C'}
};

void setup() {
  Serial.begin(9600);
  SPI.begin(); // Initiate SPI bus
  rfid.PCD_Init(); // Initiate RFID reader

  lcd.init(); // Initiate LCD
  lcd.backlight(); // Turn on backlight
  lcd.createChar(0, chemistrySet); // Load custom character
}

void loop() {
  // Look for new RFID cards
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
    String scannedUID = "";

    for (byte i = 0; i < rfid.uid.size; i++) {
      scannedUID += String(rfid.uid.uidByte[i], HEX);
    }
    
    Serial.print("Scanned UID: ");
    Serial.println(scannedUID);
    
    char letter = '\0';

    // Compare scanned UID with each tag's UID
    for (int i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
      bool match = true;
      for (int j = 0; j < 4; j++) {
        if (tags[i].uid[j] != rfid.uid.uidByte[j]) {
          match = false;
          break;
        }
      }
      if (match) {
        letter = tags[i].letter;
        break;
      }
    }
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.write(byte(0)); // Display custom character
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.setCursor(0, 1);
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.write(byte(0));
    lcd.setCursor(6, 0);
    lcd.print(letter); // Display letter on LCD
    Serial.print("Letter displayed on LCD: ");
    Serial.println(letter);
    
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Place RFID Tag:");
    rfid.PICC_HaltA();
    rfid.PCD_StopCrypto1();
  }
}