PLEASE HELP. URGENT!!!

I Need Help with a project I'm doing for school.
I am making a toothbrush reminder that has a red light on when you need to brush your teeth and a green light on when you already have. I can't figure out how to do it I'm using a button and some LEDs.

Basically what I need the project to do is
When Button is released a green light turns on.
When Button is pressed for more than 6 hours Red Light turns on and Green Light is turned off.

If someone could help me out and tell me how to code it that'd be much appreciated.

Please post the full sketch of your best attempt. If possible you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's OK to add it as an attachment. Don't put your code in some external file service like dropbox, etc. We shouldn't need to go to an external website just to help you. I do feel it's reasonable to post a link to code hosted on GitHub or similar code hosting sites since that's an platform specifically designed for this sort of thing

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor then you will not have access to this useful tool. I recommend using the standard Arduino IDE instead.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

What could possibly be URGENT about this? People were successfully cleaning (and neglecting) their teeth long before the transistor was invented, never mind the microchip.

And if the notion of URGENT means that you want your question answered before someone who was in the queue ahead of you, then that is not nice.

And what @pert said.

...R

READ HOW TO USE THIS FORUM

PAY PARTICULAR ATTENTION TO #6 ABOUT HOW TO WRITE A SUBJECT LINE THAT IS MEANINGFUL

so far, you have been too loud and have not really asked for help with your project, you have asked for someone to do it for you.

what type of sketches have you tried ?
what are the problems with the sketches you have written ?

one question, who is going to hold the button for more than 6 hours ?

the arduino IDE has example programs. try the first few.

read about 'blink without delay' to get your timing

int ButtonValue = 0;

int Button = 3;
int LEDGREEN = 2;
int LEDRED = 1;

void setup() {
  pinMode(Button, INPUT);
  pinMode(LEDGREEN, OUTPUT);
  pinMode(LEDRED, OUTPUT);





}
void loop() {
  ButtonValue = digitalRead(Button);

  if (ButtonValue != 0) {
    digitalWrite(LEDGREEN, LOW);
    delay(1000);
    digitalWrite(LEDGREEN, HIGH);

  }

  if (ButtonValue != 0) {

    digitalWrite(LEDRED, HIGH);

  }

  else {
    digitalWrite(LEDGREEN, HIGH);
    digitalWrite(LEDRED, LOW);




  }
}

I have the delay at 1000 milliseconds instead of 6 hours so I can see how it works. The problem I'm having is that when I release the button the Green LED won't turn on until the delay has finished and I'm not sure how to get around that.

Also sorry for saying urgent but for me, it is as I'm trying to finish my project as quick as possible because I need to pass this to pass my year level.

JakeNelson:
I have the delay at 1000 milliseconds instead of 6 hours so I can see how it works. The problem I'm having is that when I release the button the Green LED won't turn on until the delay has finished and I'm not sure how to get around that.

If you want a responsive program then you should not use delay() because it blocks the Arduino from doing other things.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

If you want to keep time accurately over long periods (i.e. to stay in sync with a proper clock) you may need to use a Real Time Clock (RTC) module as the normal Arduino oscillator does not run at precisely 16 MHz.

...R

Robin2:
What could possibly be URGENT about this? People were successfully cleaning (and neglecting) their teeth long before the transistor was invented, never mind the microchip.

And if the notion of URGENT means that you want your question answered before someone who was in the queue ahead of you, then that is not nice.

And what @pert said.

...R

maybe he has been holding the button for more than 6 hours and URGENT has a different meaning to him that everybody else ?

I have already looked into millis() and doing several things at once but I don't really understand it could anyone show me an example of how I could use that in the context of my toothbrush reminder.
Oh and the toothbrush is what holds down the button. It's not me holding it down for 6 hours hahaha.

I don't know why everyone cares so much that I said "urgent" I only said that because I need help as fast as possible since my project is due tomorrow and I have no idea how to do it.

I have already looked into millis() and doing several things at once but I don't really understand it

Try understanding the blink without delay example - it boils down to about six lines of code.

Honestly, the problem is so simple that any code we provide would end up being a complete answer, and would be doing no-one any favours.

(By my reckoning, relative to me, it already is tomorrow where you are, so you've left it too late)

I mean I would really appreciate a complete answer but I understand that I'm not going to learn from that but it's not that important for me to learn this. I live in New Zealand and we use a credit-based school system and I need to get 4 more credits to pass the year. I have done all the written work for this project but I just really don't understand millis() so if you could help me out I would really, really appreciate it.

millis() returns the number of milliseconds since the processor last reset.
If I write down the value it returns NOW and call it "start time" and then write down the value it returns NOW and call it "endTime", then if I subtract endTime from startTime, then the DIFFERENCE is the time in milliseconds between the two events.

That's all there is to it.

There's nothing mystical about milliseconds, it's just an inconveniently large number to handle long pauses, but fortunately, microcontrollers don't really care about the size of numbers.

I understand that I'm not going to learn from that but it's not that important for me to learn this.

So it isn't "URGENT!!!"

Alright, thanks heaps sorry to bother everyone.

So do I need any components other than
Arduino Uno
LEDs
Jumper cables
button

or do I need a microcontroller?

You do need a microcontroller...fortunately that's what an Arduino Uno is.

Other than that you just need to change your code to use millis() and you've had enough useful hints to do that yourself.

Steve

Also is there any way you can just show me the code because it's like 1 am where I live and I have to hand this in tomorrow I know it's my fault for leaving it last minute but if you know how to do it that'd really help because I need some sleep.

dave-in-nj:
maybe he has been holding the button for more than 6 hours and URGENT has a different meaning to him that everybody else ?

LOL

...R

JakeNelson:
Also is there any way you can just show me the code because it's like 1 am where I live and I have to hand this in tomorrow

I can't see the difference between saying these alternatives to your teacher tomorrow

{A} I did not do my homework

{B} Here is some homework that somebody else did

I presume it was never your intention to cheat and pretend that it was all your own work?

...R

Or

{C} I wasn't organised enough to study ahead of time

{D} I can't seem to google simple questions and I'd rather ask folks on Forums.

{E} I played too much Fortnite Battle Royal.

Sorry dude, but if anything comes from this experience it's that you need to get Organised and Study properly. If you were doing a marathon tomorrow, would you start running today?

Hope things work out.

So basically you needed to have done some work by tomorrow in order to pass your course. You've known this for ages but you haven't done the work. You've only even started thinking about how to do it a few hours before the deadline.

You've failed your course. That's the way it is when you can't be bothered to do the course work.

Steve

JakeNelson:
So do I need any components other than
...

Stating the obvious here, but you also need resistors if you don't want to burn out your LEDs immediately. 5V will most likely kill them.

Instead of saying you need help, ask for help in a meaningful way. Study the blink without delay example, think about how you could incorporate that into YOUR code. DO IT! And if it isn't working, come back.

with Millis() you can basically check the time. It doesn't really matter if you checked the time and it is as 1 pm and then you can say, hey when it's 7 pm 6 hours have passed or if you saw it is x amount of milliseconds now since last reset. You can still find out when 6 hours will have passed.

Good luck!