Hello. I just learned arduino. I want to write a program that works using up, down and select buttons. I can set the time with these buttons and keep a light on during this time when the time has not yet reached zero. Please help me if possible. Thank you very much
We’ll, now that you’ve completed learning, show us your best attempt at the code, and we can advise / comment on ways to improve it if needed.
One suggestion, try using millis() timing rather than delay(), which will help as the project becomes more comprehensive.
Post an annotated schematic showing how you wired it. Be sure to include all connections, power,ground and power sources. I do not read frizzys or pictures.
Hello alimodami
Welcome to the worldbest Arduino forum ever.
It seems to be a school assigment, isn´t it?
Keep in mind the sketch needs the following services:
- an adequate i/o handling
- debouncing of buttons
- state change detection of buttons
- functions to start/stop timers
- and parts that I´ve forgoten
Take a piece a paper and a pencil and design a program structure based on the IPO model before start coding.
The basics for coding the project are to be found in the IDE as examples.
Have a nice day and enjoy coding in C++.
How should somebody help you without seeing the existing code?
Without seeing the existing code your question transforms to a different question.
This different question would be:
Can somebody write from scratch the following code for me....."
Hopefully you have your new schematic ready so I can check it for you, the old one was not legible. I am assuming you were taught that when you learned arduino.
Thank you for all the help
Thank you so much for taking the time to answer me
I want to write a program that takes the time parameter as input and turns on and off some LEDs randomly at the specified time. After the timer reaches zero, all the LEDs will turn off.
The problem is that I want the screen to display the message "Time Remaining" when the countdown starts. But I don't know where to put this command in the program.
Because everything is recorded in memory and I can't write any other code in the program.
Thank you all again.
//Viral Science www.viralsciencecreativity.com www.youtube.com/c/viralscience
//Arduino Timer Relay - Triger relay for specific time
#include <LiquidCrystal_I2C.h> //https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD HEX address 0x3F -- change according to yours
#include "Countimer.h" //https://github.com/inflop/Countimer
Countimer tdown;
#include <EEPROM.h>
#define bt_set A3
#define bt_up A2
#define bt_down A1
#define bt_start A0
const byte NUM_RLYS = 6;
const byte RLY_Pins[] = {2, 3, 4, 5, 6, 7};
int time_s = 0;
int time_m = 0;
int time_h = 0;
int set = 0;
int flag1=0, flag2=0;
// int relay = 5;
// int buzzer = 6;
void setup() {
Serial.begin (9600);
pinMode(bt_set, INPUT_PULLUP);
pinMode(bt_up, INPUT_PULLUP);
pinMode(bt_down, INPUT_PULLUP);
pinMode(bt_start, INPUT_PULLUP);
randomSeed(analogRead(A0));
for (int n = 0; n < NUM_RLYS; n++)
{
pinMode(RLY_Pins[n], OUTPUT);
}
// pinMode(relay, OUTPUT);
// pinMode(buzzer, OUTPUT);
lcd.init();
lcd.backlight ();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Saynaco Burn-in");
lcd.setCursor(0,1);
lcd.print("Service Starting");
tdown.setInterval(print_time, 999);
eeprom_read();
delay(5000);
lcd.clear();
}
void print_time(){
time_s = time_s-1;
if(time_s<0){time_s=59; time_m = time_m-1;}
if(time_m<0){time_m=59; time_h = time_h-1;}
}
void tdownComplete(){Serial.print("ok");}
//tdown.stop();
void loop(){
tdown.run();
if(digitalRead (bt_set) == 0){
if(flag1==0 && flag2==0){
flag1=1;
set = set+1;
if(set>3){set=0;}
delay(100);
}
}else{flag1=0;}
if(digitalRead (bt_up) == 0){
if(set==0){tdown.start(); flag2=1;}
if(set==1){time_s++;}
if(set==2){time_m++;}
if(set==3){time_h++;}
if(time_s>59){time_s=0;}
if(time_m>59){time_m=0;}
if(time_h>99){time_h=0;}
if(set>0){
eeprom_write();
}
delay(200);
}
if(digitalRead (bt_down) == 0){
if(set==0){tdown.stop(); flag2=0;}
if(set==1){time_s--;}
if(set==2){time_m--;}
if(set==3){time_h--;}
if(time_s<0){time_s=59;}
if(time_m<0){time_m=59;}
if(time_h<0){time_h=99;}
if(set>0){
eeprom_write();
}
delay(200);
}
if(digitalRead (bt_start) == 0 && set==0){
flag2=1;
eeprom_read();
//digitalWrite(relay, HIGH);
Rand_Relay ();
tdown.restart();
tdown.start();
}
lcd.setCursor(0,0);
if(set==0){lcd.print("Set Time & Start");}
if(set==1){lcd.print(" Set Seconds ");}
if(set==2){lcd.print(" Set Minutes ");}
if(set==3){lcd.print(" Set Hours ");}
// lcd.setCursor(0,0); //for in progress
// lcd.print("Service End Time");
lcd.setCursor(4,1);
if(time_h<=9){lcd.print("0");}
lcd.print(time_h);
lcd.print(":");
if(time_m<=9){lcd.print("0");}
lcd.print(time_m);
lcd.print(":");
if(time_s<=9){lcd.print("0");}
lcd.print(time_s);
lcd.print(" ");
// lcd.print(" ");
if(time_s==0 && time_m==0 && time_h==0 && flag2==1){flag2=0;
tdown.stop();
// lcd.clear(); //for finished message
// lcd.setCursor(0,0);
// lcd.print("End of Burn-in");
// lcd.setCursor(0,1);
// lcd.print("Press Set to Start");
// digitalWrite(relay, LOW);
// digitalWrite(buzzer, HIGH);
// delay(300);
// digitalWrite(buzzer, LOW);
// delay(200);
// digitalWrite(buzzer, HIGH);
// delay(300);
// digitalWrite(buzzer, LOW);
// delay(200);
// digitalWrite(buzzer, HIGH);
// delay(300);
// digitalWrite(buzzer, LOW);
allOff();
}
// if(flag2==1){digitalWrite(relay, HIGH);}
// else{digitalWrite(relay, LOW);}
if(flag2==1){ // simulate for above code
Rand_Relay ();
}
else{
allOff();
}
delay(1);
}
void eeprom_write(){
EEPROM.write(1, time_s);
EEPROM.write(2, time_m);
EEPROM.write(3, time_h);
}
void eeprom_read(){
time_s = EEPROM.read(1);
time_m = EEPROM.read(2);
time_h = EEPROM.read(3);
}
void Rand_Relay()
{
static unsigned long timer = 0;
static unsigned long interval = 1000;
if (millis() - timer >= interval)
{
timer = millis();
allOff();
byte this_RLY = random(0, NUM_RLYS);
digitalWrite(RLY_Pins[this_RLY], HIGH);
interval = random(1000, 2000); // Set relays switch time (min, max)
delay(15);
}
}
void allOff()
{
for (int n = 0; n < NUM_RLYS; n++)
{
digitalWrite(RLY_Pins[n], LOW);
}
}
That statement doesn't make sense. In any case, the code you posted is poorly written. You might be better to start over.
I'm not sure what this means. Do you want to turn on and off LEDs randomly while the timer is running? Should the LEDs be random, the on/off time be random or both? What is the maximum on and off time?
All of the questions need to be answered before any code is written.
Start with writing a simple program that turns on and off an LED. Then modify it to turn on and off several LEDs. Then modify it to choose a random LED to turn on and off. Then modify it to choose a random time to turn on and off. Then add a button and make sure you can read the button. Then modify it to do...
You get what I am saying? If you try to code it all at once you will end up with confusing and hard to debug code.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.