Hello,
I was wondering if anyone knows why the relay that I have connected to pin 4 on the Az-Delivery UNO turns on every time the Rx LED flashes? Are they connected or is it another issue?
Thank you for any help.
Hello,
I was wondering if anyone knows why the relay that I have connected to pin 4 on the Az-Delivery UNO turns on every time the Rx LED flashes? Are they connected or is it another issue?
Thank you for any help.
The problem will be either in the code that you have not posted or the schematic that you have not posted
Your topic was MOVED to its current forum category as it is more suitable than the original
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);
int BUTTONstate = 0;
int BUTTONstate2 = 0;
int relay = 0;
byte degree[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
};
//Timed events
const unsigned long lcd_time = 2000; //interval in ms
const unsigned long serial_time = 500;
const unsigned long sd_time = 10000;
const unsigned long volts_time = 100;
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_4 = 0;
unsigned long page = 1;
int page_counter=1 ; //To move beetwen pages
//-------Pins-----//
int up = 2; //Up button
int down = 3; //Down button
//---------Storage debounce function-----//
boolean current_up = LOW;
boolean last_up=LOW;
boolean last_down = LOW;
boolean current_down = LOW;
int value = 0;
int value2 = 0;
float voltageraw;
float voltage = 0;
float voltage2 = 0;
float R1 = 98100.0;
float R2 = 5330.0;
const int chipSelect = 10;
void setup () { //----------------------------------------------SETUP
pinMode(8, OUTPUT); //Error LED
pinMode(7, OUTPUT); //Relay 1
pinMode(6, OUTPUT); //Relay 2
pinMode(5, OUTPUT); //Relay 3
pinMode(4, OUTPUT); //Relay 4
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Solar controller");
lcd.setCursor(0,1);
lcd.print(" Nesko 2022");
delay(3000);
lcd.clear();
//Serial.print("Initializing SD card...");
lcd.setCursor(0,0);
lcd.print("Initializing");
lcd.setCursor(0,1);
lcd.print("SD card...");
delay(2000);
lcd.clear();
if (!SD.begin(4)) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SD card failed");
digitalWrite(8, HIGH);
//while (1);
} else {
digitalWrite(8, LOW);
}
Serial.println("initialization done.");
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
//---- De-bouncing function for all buttons----//
boolean debounce(boolean last, int pin)
{
boolean current = digitalRead(pin);
if (last != current)
{
delay(5);
current = digitalRead(pin);
}
return current;
}
void loop () {
digitalWrite(4, HIGH);
//Conversion formula
value = analogRead(A0);
value2 = analogRead(A1);
voltage = value * (5.0/1023)*((R1 + R2)/R2);
voltage2 = value2 * (5.0/1023)*((R1 + R2)/R2);
current_up = debounce(last_up, up); //Debounce for Up button
current_down = debounce(last_down, down); //Debounce for Down button
//----Page counter function to move pages----//
//Page Up
if (last_up== LOW && current_up == HIGH){ //When up button is pressed
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter <3){ //Page counter never higher than 3(total of pages)
page_counter= page_counter +1; //Page up
}
else{
page_counter= 3;
}
}
last_up = current_up;
//Page Down
if (last_down== LOW && current_down == HIGH){ //When down button is pressed
lcd.clear(); //When page is changed, lcd clear to print new page
if(page_counter >1){ //Page counter never lower than 1 (total of pages)
page_counter= page_counter -1; //Page down
}
else{
page_counter= 1;
}
}
last_down = current_down;
unsigned long currentTime = millis();
DateTime now = rtc.now();
switch (page_counter) {
case 1:{
//-------------------------------------------------------------------- LCD line 1 Page 1
lcd.setCursor(0,0);
char lcdline1[16];
sprintf(lcdline1, "%02u/%02u/%04u",now.day(),now.month(),now.year());
lcd.print(lcdline1);
//-------------------------------------------------------------------- LCD line 2 Page 1
lcd.createChar(2, degree);
lcd.setCursor(0,1);
char lcdline2[16];
lcd.print("T:");
lcd.print(rtc.getTemperature());
lcd.write(2);
lcd.print("C ");
sprintf(lcdline2, " %02u:%02u ",now.hour(),now.minute());
lcd.print(lcdline2);
break;
}
break;
case 2: { //---------------------------------------------------Design of page 2---------- VOLTAGE CALCULATION
if( currentTime - previousTime_2 >= serial_time ){
/* //Conversion formula
value = analogRead(A0);
value2 = analogRead(A1);
voltage = value * (5.0/1023)*((R1 + R2)/R2);
voltage2 = value2 * (5.0/1023)*((R1 + R2)/R2); */
lcd.setCursor(0,0);
lcd.print("V_Solar = ");
lcd.print(voltage);
lcd.setCursor(0,1);
lcd.print("V_Batt = ");
lcd.print(voltage2);
previousTime_2 = currentTime;
}
}
break;
case 3: { //-----------------------------------------------------------Design of page 3
lcd.setCursor(1,0);
lcd.print("Light timer");
lcd.setCursor(4,1);
lcd.print("Hour - Hour");
}
break;
}//switch end
//------------------------------------------------------------------- Buttons
// BUTTONstate = digitalRead(2);
//BUTTONstate2 = digitalRead(3);
//------------------------------------------------------------------- Serial printing
if( currentTime - previousTime_1 >= lcd_time ){
previousTime_1 = currentTime;
}
//----------------------------------------------------------------------- File writing
if( currentTime - previousTime_3 >= sd_time ){
myFile = SD.open("data.txt", FILE_WRITE);
if (myFile){
Serial.println("WRITING TO FILE");
char textline[24];
double temp = rtc.getTemperature();
String string_temp = " ";
// string_temp = dtostrf(temp);
sprintf(textline, "%02u/%02u/%04u,%02u:%02u,%d,%d,%d",now.day(),now.month(),now.year(),now.hour(),now.minute(),temp,voltage,voltage2);
myFile.print(textline);
myFile.println(" ");
Serial.println(textline);
Serial.println(temp);
Serial.println(voltage);
Serial.println(voltage2);
//sprintf(lcdline1, "%02u/%02u/%04u",now.day(),now.month(),now.year());
/* myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(" ");
//myFile.print(" T: ");
myFile.print(rtc.getTemperature());
myFile.print("C ");
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.println(" ");*/
myFile.close();
}
else {
Serial.println("error opening file");
}
previousTime_3 = currentTime;
}
}
Here is the schematic and code. I have even tried to put the relay in the loop in hopes of overpowering the problem. It's set to high because The relay module uses negative logic.
Thank you
The RX Led on the Uno, or the LED in your schematic?
If the former - when does that happen? You don't seem to be doing any Serial.read() calls in your sketch, so I'm not sure why the RX LED would ever flash.
May not make a difference but try changing the first part of setup() to:
void setup () { //----------------------------------------------SETUP
digitalWrite(7, HIGH); // set port pin HIGH (relay OFF) first
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
pinMode(8, OUTPUT); //Error LED
pinMode(7, OUTPUT); //Relay 1 // then set pins as OUTPUT
pinMode(6, OUTPUT); //Relay 2
pinMode(5, OUTPUT); //Relay 3
pinMode(4, OUTPUT); //Relay 4
digitalWrite(8, LOW);
Serial.begin(9600);
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.