My project isnt working, its simple.. Help plz

Good evening, my idea is, first verify if there is enough luminosity in the room (or, if the luminosity sensor returns a low number), then i just turn a led ON and a BUZZER to advice that light is missing! then, i show a menu, and depending on the time i hold the button, i turn another LED on or OFF. See the code! Help me, cause there is something wrong :frowning:

#include<LiquidCrystal.h>
const int BUTTON=1;
const int LEDL=8;
const int LEDH=13;
const int SENSOR=0;
const int BUZZER=7;
int statelight,statebutton=0,option,cont=0,untilhere,time;
LiquidCrystal lcd(12,11,5,4,3,2);
void turnBuzzer()
{
  digitalWrite(BUZZER,HIGH);
  delay(800);
  digitalWrite(BUZZER,LOW);
}
void showMenu()
{
  lcd.setCursor(0,0);
  lcd.print("1s para acender");
  lcd.setCursor(0,1);
  lcd.print("5s para apagar");
}
void modifyLights()
{
  statelight=analogRead(SENSOR);
  if(statelight<=250)
  {
    turnBuzzer();
    digitalWrite(LEDH,HIGH);
    lcd.setCursor(0,0);
    lcd.print("Luz de emergencia ativado");
    lcd.setCursor(0,1);
    lcd.print("Problema de luz resolvido");
    delay(2000);
    showMenu();
  }
  else{
    digitalWrite(LEDH,LOW);}
}
int readButton()
{
  while(statebutton!=HIGH)
  {
  modifyLights();
  statebutton=digitalRead(BUTTON);
  if(statebutton==HIGH)
  {
    untilhere=millis();
    while(statebutton==HIGH)
    {
       statebutton=digitalRead(BUTTON);
    }
    time=millis()-untilhere;
  }
  }
  if(time>=800 && time <=1200)
  {
    return 1;}
  else if(time>=4000 && time<=6000){
    return 2;}
  else if(time>8000){
    return 3;}
}
void setup ()
{
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("INICIO PROJETO VENTO DE CORES ");
  delay(5000);
  pinMode(BUTTON,INPUT);
  pinMode(LEDL,OUTPUT);
  pinMode(LEDH,OUTPUT);
  pinMode(BUZZER,OUTPUT);
}  
void loop()
{
  showMenu();
  option=readButton();
  if(option==1)
  {
     turnBuzzer();
     lcd.setCursor(0,0);
     lcd.print("Led aceso");
     digitalWrite(LEDL,HIGH);
  }
  else if(option==2)
  {
     turnBuzzer();
     lcd.setCursor(0,0);
     lcd.print("Led apagado");
     digitalWrite(LEDL,LOW);
  }
  else if(option==3)
  {
     turnBuzzer();
     lcd.setCursor(0,0);
     lcd.print("xxx");  // ignore the option 3 plz, it is just a joke i made haha
  }
}

my first guess would be that the functions defined before the setup() should probably be in at the end of the loop().

stretched59:
my first guess would be that the functions defined before the setup() should probably be in at the end of the loop().

Nah, doesn't matter.

alessandro_0606:
Good evening, my idea is, first verify if there is enough luminosity in the room (or, if the luminosity sensor returns a low number), then i just turn a led ON and a BUZZER to advice that light is missing! then, i show a menu, and depending on the time i hold the button, i turn another LED on or OFF. See the code! Help me, cause there is something wrong :frowning:

That gives us an idea of what it's supposed to do, but please also tell us what it actually does.

Make all variables that use values from millis() or are calculated based on those values unsigned long, not int (like time and untilhere). Not sure that's the problem, but it's a problem. :smiley:

while(statebutton!=HIGH)
{
  modifyLights();
  statebutton=digitalRead(BUTTON);
  if(statebutton==HIGH)
  {
    untilhere=millis();
    while(statebutton==HIGH)
    {
       statebutton=digitalRead(BUTTON);
    }
    time=millis()-untilhere;
  }
}

There looks to be a bit of a problem here. If your outer while loop is checking that statebutton!=HIGH, then you are pretty likely to leave this loop as soon as it does go HIGH so the if statement: if(statebutton==HIGH) will probably never be reached (unless you are extremely lucky and it happens to change in the loop). Even if you do happen to get the change between entering the while loop and the if statement, your inner while loop will continue until statebutton is low again, and so you won't exit the outer while loop.
Effectively you either go through outer loop indefinitely or you leave it without ever entering the inner loop.
For what (I think) you are trying to do you would need to do:

while(statebutton!=HIGH)
{
	modifyLights();
	statebutton=digitalRead(BUTTON);
}

untilhere=millis();
while(statebutton==HIGH)
{
	statebutton=digitalRead(BUTTON);
}
time=millis()-untilhere;

Though I'm not sure if this is actually what you want to do, one of the main problems you have with this code is the way that the flow is structured, i.e. the way you step through your code. The way you have written it is:

while waiting for user input:
    check light level
    if the lights are low: turn on LEDs, use buzzer, display message
    else turn off LEDs
when button has been pressed:
    check how long button is pressed down for
    if it's less than 1200ms turn on an LED, display message
    if it's greater than 1200ms turn off an LED, display message

But this way the user input is what causes the program to continue.
I think what you are trying to do is more along the lines of:

while light level is high: do nothing
when light level goes low:  turn on LEDs, use buzzer, display message
wait for user input
check how long button is pressed down for
if it's less than 1200ms turn on an LED, display message
if it's greater than 1200ms turn off an LED, display message

This way the routines will be triggered by the light level, and then it will wait for the user input. You can add more advanced logic, for example making that it self-clear when the light goes high again, but this just requires a bit of thought. I would also be a bit careful with the way you check for user input, at the moment if they press the button for less then 800 ms your program doesn't define a state of what should happen, you should add some extra code for this case and loop until your user input is valid.

Hope this helps,

Tobyb121

alessandro_0606:
See the code! Help me, cause there is something wrong :frowning:

You need to be more specific: what's it doing that you don't expect, or not doing that you do expect?

So... Tobyb121 is absolutly right, i think this solve the problem, and sorry guys, i didnt specified my problem....

The code i made didnt turn the light on when it needs to, and when lcd shows the menu and i try to press the button, doesnt happen anything ... (I think Tobyb121 solved this problems)...

Anyways, i will make the changes Tobyb121 adviced and see what happens, soon i post here again!

EDIT::: So, i wrote the code and.. my problem is solved xD

thank you Tobyb121 u helped me alot