Jump to content
ELFORUM - Forumul electronistilor

Arduino cu trei LED-uri si RTC


norad

Recommended Posts

Va salut.

Intampin o problema la scrierea codului pentru un montaj cu trei LED-uri (rosu, galben, albastru) si modulul RTC DS3231.

Asadar, scopul montajului este sa aprinda LED-urile albastru si rosu, pentru o secunda, atunci cand se schimba minutul. Ledul galben sta aprins intre secundele 58 si 2.

Montajul functioneaza.

La montaj se adauga un buton, la a carui apasare, LED-ul galben sa lumineze continuu si LED-urile rosu si albastru sa lunimeze alternativ la o secunda. Fuctioneaza si asta.

 

Cand butonul nu mai este apasat, LED-ul galben sa se stinga. La codul pe care l-am scris eu LED-ul galben ramane aprins pana cand secunda este 2. Nu stiu cum/ce sa modific.

 

As vrea ca structura codului sa ramana cam la fel, cu functia toggleLEDs (), caci am inteles-o  si eventualele alte modificari le voi face mai usor.

 

 

 

 

/*Connect the module SDA to A4 and SCL to A5*/

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

const int redLEDPin = 2;
const int blueLEDPin = 3;
const int yellowLEDPin = 9;
const int buttonPin = 8;

int currentSecond;
int buttonState = 0;
int currentLEDState = 0;

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  rtc.begin();
  
}

void loop() {

  DateTime now = rtc.now();
  
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(yellowLEDPin, HIGH);
    toggleLEDs();
  }
  else {
    currentSecond=now.second();

    if (currentSecond == 58) {
      digitalWrite(yellowLEDPin, HIGH);
    }
    else if (currentSecond == 2) {
      digitalWrite(yellowLEDPin, LOW);
    }

    if (currentSecond == 0) {
      toggleLEDs();
    }
  }
}

void toggleLEDs() {
  if (currentLEDState == 0) {
    digitalWrite(redLEDPin, HIGH);
    delay(1050);
    digitalWrite(redLEDPin, LOW);
    currentLEDState = 1;
  }
  else {
    digitalWrite(blueLEDPin, HIGH);
    delay(1050);
    digitalWrite(blueLEDPin, LOW);
    currentLEDState = 0;
  }
}

 

 

 

 

Link to comment
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

const int redLEDPin = 2;
const int blueLEDPin = 3;
const int yellowLEDPin = 9;
const int buttonPin = 8;

int currentSecond;
int buttonState = 0;
int currentLEDState = 0;

void setup() {
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  pinMode(yellowLEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);

  rtc.begin();
  
}

void loop() {

  DateTime now = rtc.now();
  
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(yellowLEDPin, HIGH);
    toggleLEDs();
  }
  else {
    currentSecond=now.second();
    if (currentSecond >2 && currentSecond <58) digitalWrite(yellowLEDPin, LOW);  //linia adugata  
    if (currentSecond == 58) {
      digitalWrite(yellowLEDPin, HIGH);
    }
    else if (currentSecond == 2) {
      digitalWrite(yellowLEDPin, LOW);
    }

    if (currentSecond == 0) {
      toggleLEDs();
    }
  }
}

void toggleLEDs() {
  if (currentLEDState == 0) {
    digitalWrite(redLEDPin, HIGH);
    delay(1050);
    digitalWrite(redLEDPin, LOW);
    currentLEDState = 1;
  }
  else {
    digitalWrite(blueLEDPin, HIGH);
    delay(1050);
    digitalWrite(blueLEDPin, LOW);
    currentLEDState = 0;
  }
}

Si eventual poti renunta la linia cu actiunea din secunda 2 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines