jidshan
December 10, 2022, 10:01pm
1
Hello guys whats the best thing to do im trying to turn on and off the LEDS but when i set the "LED1" first to a certain time and set the "LED2" while the "LED1" is not finished, the LED 1 is not working but the LED2 is working. Everytime i set the led the latest LED is the only one working. Im controlling it in android app via bluetooth
if (ledColor == 1) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED1, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED1, LOW);
Serial.print("OFF ");
}
} else if (ledColor == 2) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED2, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED2, LOW);
Serial.print("OFF ");
}
} else if (ledColor == 3) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED3, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED3, LOW);
Serial.print("OFF ");
}
}
All of the LEDs appear to share the same variables for the on and off dates and times. How would the sketch know which LED the variable relate to ?
1 Like
jidshan
December 10, 2022, 10:20pm
3
this is where the data coming from, i hardcoded in the android app the variable that identify the led by "ledColor"
for (int n = 0; n < nToks; n++)
//Serial.println (toks [n]);
onYear = atoi(toks[0]);
onMonth = atoi(toks[1]);
onDate = atoi(toks[2]);
onHour = atoi(toks[3]);
onMin = atoi(toks[4]);
offYear = atoi(toks[5]);
offMonth = atoi(toks[6]);
offDate = atoi(toks[7]);
offHour = atoi(toks[8]);
offMin = atoi(toks[9]);
ledColor = atoi(toks[10]);
If you are trying to turn more than one LED on and off then you need different variables. Suppose that you set onHour to 5 to be used with ledColor 1 then set onHour to 7 for use with ledColor 2 then it will change it for ledColor 1 too
Can you see the problem ?
Please post your complete sketch
1 Like
jidshan
December 10, 2022, 10:30pm
5
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define TxD 3
#define RxD 2
SoftwareSerial bluetoothSerial(TxD, RxD);
Time t;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
String dofweek1;
String myDate;
String myTime;
String displayLCD;
void setup()
{
// Setup Serial connection
Serial.begin(9600);
bluetoothSerial.begin(9600);
lcd.begin();
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}
// Initialize the rtc object
rtc.begin();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
// The following lines can be uncommented to set the date and time
//rtc.setDOW(2); // Set Day-of-Week to SUNDAY
//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
}
void loop()
{
// Send Day-of-Week
t = rtc.getTime();
//date and time to ON
char buf [80];
char *toks [20];
int nToks;
int onYear;
int onMonth;
int onDate;
int onHour;
int onMin;
//date and time to OFF
int offDate;
int offMonth;
int offYear;
int offHour;
int offMin;
int ledColor;
if (bluetoothSerial.available ()) {
int n = bluetoothSerial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
Serial.println (buf);
nToks = 0;
toks [nToks++] = strtok (buf, ",");
for ( ; NULL != (toks [nToks] = strtok (NULL, ",")); nToks++);
for (int n = 0; n < nToks; n++)
//Serial.println (toks [n]);
onYear = atoi(toks[0]);
onMonth = atoi(toks[1]);
onDate = atoi(toks[2]);
onHour = atoi(toks[3]);
onMin = atoi(toks[4]);
offYear = atoi(toks[5]);
offMonth = atoi(toks[6]);
offDate = atoi(toks[7]);
offHour = atoi(toks[8]);
offMin = atoi(toks[9]);
ledColor = atoi(toks[10]);
}
if (ledColor == 1) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED1, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED1, LOW);
Serial.print("OFF ");
}
} else if (ledColor == 2) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED2, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED2, LOW);
Serial.print("OFF ");
}
} else if (ledColor == 3) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED3, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED3, LOW);
Serial.print("OFF ");
}
}
dofweek1 = rtc.getDOWStr(FORMAT_SHORT);
myTime = rtc.getTimeStr();
myDate = rtc.getDateStr();
displayLCD = dofweek1 + " " + myDate ;
lcd.setCursor(1,0);
lcd.print(displayLCD);
lcd.setCursor(4,1);
lcd.print(myTime);
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
// Wait one second before repeating :)
delay(1000);
lcd.clear();
}
jidshan
December 10, 2022, 11:46pm
6
hello still cant figured it out how to do it, do you know how?
jidshan:
Serial.println (buf);
Can you please show what the buffer looks like?
Are you using a standard app like Kai Morich's Serial Bluetooth Terminal to send the message or is it a custom app you made?
If you forget about the bluetooth for a moment and hard code some on and off times, and then just enter the ledColor over Serial from the monitor, can you see the first led stay on when a new one is called for?
jidshan
December 11, 2022, 1:00am
8
its a custom made
Yes it always stays on when new one is called
when i set the "LED1" first to a certain time and set the "LED2" while the "LED1" is not finished, the LED 1 is not working but the LED2 is working. Everytime i set the led the latest LED is the only one working.
Yes it always stays on when new one is called
I'm confused. Are you saying that when you input the ledColor over Serial, the code behaves as expected but when ledColor is entered from you app, the behavior is not as desired?
jidshan
December 11, 2022, 1:13am
10
when i entered a range time 9:12 to 9:15 the LED1 will turned on in 9:12 and turned off in 9:15, but when i set the LED2 with the new range of time while the LED1 is not finished turning on and off the LED1 doesnt work anymore
Do you want LED1 to turn off when LED2 is called for when LED1 is still on?
Perhaps something like this
else if (ledColor == 2)
{
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
if (t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin)
{
digitalWrite(LED2, HIGH);
Serial.print("ON ");
}
else if (t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin)
{
digitalWrite(LED2, LOW);
Serial.print("OFF ");
}
}
1 Like
jidshan
December 11, 2022, 1:29am
12
the problem is when i set the range of LED1 to a certain of datetime and send it to arduino it will work, but when it is working and i send another set of range to another LED2, the LED1 dont work anymore
Please explain in detail what "don't work anymore" means.
jidshan
December 11, 2022, 1:42am
14
the datetime range in LED1 that i set in first place is not running. and when i place another set of datetime to other LED when LED1 is running, the LED1 doesnt run anymore
This was the issue raised in the first reply's from @UKHeliBob .
I think your program is going to need some reconfiguration.
You will need on/off times for each led.
Your control logic will not work for multiple leds with different on/off times.
if (ledColor == 1)
{ }
else if (ledColor == 2)
{ }
else if (ledColor == 3)
{ }
There are several ways to enable multiple leds to run independently with specific times. Boolean control variables and conditional statements like
if (runLED1 == true)
{
//check and run according to timing
//set runLED1 = false when timed out
}
Give this some thought, and come back with revised code.
jidshan
December 11, 2022, 5:42am
16
i think its the same
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define TxD 3
#define RxD 2
SoftwareSerial bluetoothSerial(TxD, RxD);
Time t;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
String dofweek1;
String myDate;
String myTime;
String displayLCD;
void setup()
{
// Setup Serial connection
Serial.begin(9600);
bluetoothSerial.begin(9600);
lcd.begin();
// Uncomment the next line if you are using an Arduino Leonardo
//while (!Serial) {}
// Initialize the rtc object
rtc.begin();
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
// The following lines can be uncommented to set the date and time
//rtc.setDOW(2); // Set Day-of-Week to SUNDAY
//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014
}
void loop()
{
// Send Day-of-Week
t = rtc.getTime();
//date and time to ON
char buf [80];
char *toks [20];
int nToks;
int onYear;
int onMonth;
int onDate;
int onHour;
int onMin;
//date and time to OFF
int offDate;
int offMonth;
int offYear;
int offHour;
int offMin;
int ledColor;
if (bluetoothSerial.available ()) {
int n = bluetoothSerial.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0';
Serial.println (buf);
nToks = 0;
toks [nToks++] = strtok (buf, ",");
for ( ; NULL != (toks [nToks] = strtok (NULL, ",")); nToks++);
for (int n = 0; n < nToks; n++)
//Serial.println (toks [n]);
onYear = atoi(toks[0]);
onMonth = atoi(toks[1]);
onDate = atoi(toks[2]);
onHour = atoi(toks[3]);
onMin = atoi(toks[4]);
offYear = atoi(toks[5]);
offMonth = atoi(toks[6]);
offDate = atoi(toks[7]);
offHour = atoi(toks[8]);
offMin = atoi(toks[9]);
ledColor = atoi(toks[10]);
}
int onYear2;
int onMonth2;
int onDate2;
int onHour2;
int onMin2;
int offDate2;
int offMonth2;
int offYear2;
int offHour2;
int offMin2;
bool ledColor1 = false;
bool ledColor2 = false;
bool ledColor3 = false;
if(ledColor == 1) {
ledColor1 = true;
}
else if (ledColor == 2) {
ledColor2 = true;
}
else if (ledColor == 3) {
ledColor3 = true;
}
Serial.print(ledColor1);
Serial.print(ledColor2);
Serial.print(ledColor3);
if (ledColor1 == true) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED1, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED1, LOW);
Serial.print("OFF ");
}
}
if (ledColor2 == true) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED2, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED2, LOW);
Serial.print("OFF ");
}
}
if (ledColor3 == true) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED3, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED3, LOW);
Serial.print("OFF ");
}
}
dofweek1 = rtc.getDOWStr(FORMAT_SHORT);
myTime = rtc.getTimeStr();
myDate = rtc.getDateStr();
displayLCD = dofweek1 + " " + myDate ;
lcd.setCursor(1,0);
lcd.print(displayLCD);
lcd.setCursor(4,1);
lcd.print(myTime);
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Send date
Serial.print(rtc.getDateStr());
Serial.print(" -- ");
// Send time
Serial.println(rtc.getTimeStr());
// Wait one second before repeating :)
delay(1000);
lcd.clear();
}
Now you have 2 sets of date/time variables such as onHouur and onHour2 but you never use the second ones so the onHour for ledColor1, ledColor2 and ledColor3
If I understand what you are trying to do then you need a different variable for the on Hour value for each LED and the same for the other timing variables
1 Like
Do not reset the control varialbles false every pass through loop, let them be persistent by declaring them static, or else make them globals. You will set them false when the color completes and times out.
// bool ledColor1 = false;
//bool ledColor2 = false;
// bool ledColor3 = false;
static bool ledColor1 = false;
static bool ledColor2 = false;
static bool ledColor3 = false;
if(ledColor == 1) {
ledColor1 = true;
}
else if (ledColor == 2) {
ledColor2 = true;
}
else if (ledColor == 3) {
ledColor3 = true;
}
if (ledColor1 == true) {
if(t.date == onDate && t.mon == onMonth && t.hour == onHour && t.min == onMin){
digitalWrite(LED1, HIGH);
Serial.print("ON ");
}
else if(t.date == offDate && t.mon == offMonth && t.hour == offHour && t.min == offMin){
digitalWrite(LED1, LOW);
Serial.print("OFF ");
ledColor1 = false; //set false when complete
}
}
As said, you are going to use a second set of date time variables. With three leds, and three schedules, you would be best to convert the program to use arrays.
system
Closed
June 9, 2023, 5:26pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.