Pages: [1]   Go Down
Author Topic: My project isnt working, its simple.. Help plz  (Read 552 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

Code:
#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
  }
}
« Last Edit: January 04, 2013, 11:44:29 pm by Nick Gammon » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3941
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
« Last Edit: January 04, 2013, 11:40:49 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
Code:
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:
Code:
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:
Code:
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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12520
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See the code! Help me, cause there is something wrong smiley-sad

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 05, 2013, 09:47:00 am by alessandro_0606 » Logged

Pages: [1]   Go Up
Jump to: