Pages: 1 [2] 3   Go Down
Author Topic: light control unit code problems  (Read 2179 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

o yer i see

i just tried this and still have the same problem

Code:


// Example 02: Lighting Timer  26/10/2011

#define A_light 13     // Light A on pin 13
#define B_light 12     // Light B on pin 12
#define C_light 11     // Light C on pin 11
#define D_light 10     // Light D on pin 10
#define time_button 7  // Button on pin 7
#define A_light_button  1// Light A button
#define B_light_button  2// Light B button
#define C_light_button  3// Light C button
#define D_light_button  4// Light D button

int timebutton = 0;     
int buttonA = 0;     
int buttonB = 0;         // val will be used to store the
int buttonC = 0;         // state of the input pin
int buttonD = 0;

void setup() {
  pinMode(A_light, OUTPUT);
  pinMode(B_light, OUTPUT);       //Setting the digital
  pinMode(C_light, OUTPUT);       // pin's as output
  pinMode(D_light, OUTPUT);
  pinMode(time_button, INPUT);   
  pinMode(A_light_button, INPUT);
  pinMode(B_light_button, INPUT);
  pinMode(C_light_button, INPUT); // Setting as an input
  pinMode(D_light_button, INPUT);
}

void loop ()
{
timebutton = digitalRead(time_button); //read input value and store it
buttonA = digitalRead(A_light_button); //read input value and store it
buttonB = digitalRead(B_light_button); //read input value and store it
buttonC = digitalRead(C_light_button); //read input value and store it
buttonD = digitalRead(D_light_button); //read input value and store it

delay(1000); //turn all off for 1 second
if (buttonA == HIGH); { // if button A is on do the following
  digitalWrite(A_light, HIGH);  //turn A light on
  delay(9000UL); // wait for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }   }
  digitalWrite(A_light, LOW); // turn A light off
  delay(1000);  // turn all off for 1 second
if (buttonB == HIGH); { // if button B is on do the following
  digitalWrite(B_light, HIGH);  //turn B light on
  delay(9000UL); //waint for 15mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(B_light, LOW); //turn B ligt off
  delay(1000); // turn all off for 1 second
if (buttonC == HIGH); { // if button C is on do the following
  digitalWrite(C_light, HIGH);  // turn C light on
  delay(9000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(C_light, LOW); // turn C light off
  delay(1000); //turn all off for 1 second
if (buttonD == HIGH); { // if button D is on do the following
  digitalWrite(D_light, HIGH);  // turn D light on
  delay(9000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(D_light, LOW); // turn D light off
 
}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25762
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You still have the same semicolons
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

look again i have changed where the semicolons close on the end of each section
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25762
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, you look again.
And when you've sorted it, post again
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

there is a subtle difference i just got misses to check as well lol
prob not the difference  you hoping for tho lol
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25762
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, I don't understand your last post.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so where do you think i have gone wrong as i thought i had all the semicolons in the right place obviously i havent?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25762
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at your if statements (I can't because you haven't posted your code).
Is there a semicolon there?
Do you think it should be there?
If so, leave it there, otherwise, remove it.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i would say leve it there but i don't have much experience of this

Code:


// Example 02: Lighting Timer  26/10/2011

#define A_light 13     // Light A on pin 13
#define B_light 12     // Light B on pin 12
#define C_light 11     // Light C on pin 11
#define D_light 10     // Light D on pin 10
#define time_button 7  // Button on pin 7
#define A_light_button  1// Light A button
#define B_light_button  2// Light B button
#define C_light_button  3// Light C button
#define D_light_button  4// Light D button

int timebutton = 0;     
int buttonA = 0;     
int buttonB = 0;         // val will be used to store the
int buttonC = 0;         // state of the input pin
int buttonD = 0;

void setup() {
  pinMode(A_light, OUTPUT);
  pinMode(B_light, OUTPUT);       //Setting the digital
  pinMode(C_light, OUTPUT);       // pin's as output
  pinMode(D_light, OUTPUT);
  pinMode(time_button, INPUT);   
  pinMode(A_light_button, INPUT);
  pinMode(B_light_button, INPUT);
  pinMode(C_light_button, INPUT); // Setting as an input
  pinMode(D_light_button, INPUT);
}

void loop ()
{
timebutton = digitalRead(time_button); //read input value and store it
buttonA = digitalRead(A_light_button); //read input value and store it
buttonB = digitalRead(B_light_button); //read input value and store it
buttonC = digitalRead(C_light_button); //read input value and store it
buttonD = digitalRead(D_light_button); //read input value and store it

delay(1000); //turn all off for 1 second
if (buttonA == HIGH); { // if button A is on do the following
  digitalWrite(A_light, HIGH);  //turn A light on
  delay(9000UL); // wait for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }   }
  digitalWrite(A_light, LOW); // turn A light off
  delay(1000);  // turn all off for 1 second
if (buttonB == HIGH); { // if button B is on do the following
  digitalWrite(B_light, HIGH);  //turn B light on
  delay(9000UL); //waint for 15mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(B_light, LOW); //turn B ligt off
  delay(1000); // turn all off for 1 second
if (buttonC == HIGH); { // if button C is on do the following
  digitalWrite(C_light, HIGH);  // turn C light on
  delay(9000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(C_light, LOW); // turn C light off
  delay(1000); //turn all off for 1 second
if (buttonD == HIGH); { // if button D is on do the following
  digitalWrite(D_light, HIGH);  // turn D light on
  delay(9000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  } }
  digitalWrite(D_light, LOW); // turn D light off
 
}



Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at the Arduino reference for if: http://arduino.cc/en/Reference/If. Compare yours, with particular reference to semicolons, to the ones you will see there. There is a very important difference.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25762
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know what "leve" means.
Ok look at "if (buttonA == HIGH);"
What do you think I'm going on about?
(Hint: semicolon)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i ment leave

ok so i guess i dont need them then
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
delay(1000); //turn all off for 1 second
delay() doesn't turn anything on or off.

Do you suppose automobiles are assembled by piling all the parts around the edge of the room, and put together as they are encountered? No. The engine is put together. The transmission is put together.

You should be writing code the same way. Write a sketch that does nothing more than a Serial print when a switch is pressed. When that works, expand on that to print another message when the switch is released. When that works, expand on that to print only once when the switch transitions from released to pressed, and once when the switch transitions from pressed to released.

When that works, you know your switches are wired correctly, and that you understand how to read them and how if statements work.

You know none of that now. Assuming the switches work has you looking at the software for problems that may be hardware issues.

There are no statements to enable the internal pullup resistors, so you need external pullup or pulldown resistors. Do you have them? How ARE the switches wired?
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 291
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi there thanks for that information i will bare that in mind for other projects. the project is working how i wanted it to now but by all means say if you see something you dont like
Code:
// Example 02: Lighting Timer  26/10/2011

#define A_light 13     // Light A on pin 13
#define B_light 12     // Light B on pin 12
#define C_light 11     // Light C on pin 11
#define D_light 10     // Light D on pin 10
#define time_button 7  // Button on pin 7
#define A_light_button  1// Light A button
#define B_light_button  2// Light B button
#define C_light_button  3// Light C button
#define D_light_button  4// Light D button

int timebutton = 0;     
int buttonA = 0;     
int buttonB = 0;         // val will be used to store the
int buttonC = 0;         // state of the input pin
int buttonD = 0;

void setup() {
  pinMode(A_light, OUTPUT);
  pinMode(B_light, OUTPUT);       //Setting the digital
  pinMode(C_light, OUTPUT);       // pin's as output
  pinMode(D_light, OUTPUT);
  pinMode(time_button, INPUT);   
  pinMode(A_light_button, INPUT);
  pinMode(B_light_button, INPUT);
  pinMode(C_light_button, INPUT); // Setting as an input
  pinMode(D_light_button, INPUT);
}

void loop ()
{
timebutton = digitalRead(time_button); //read input value and store it
buttonA = digitalRead(A_light_button); //read input value and store it
buttonB = digitalRead(B_light_button); //read input value and store it
buttonC = digitalRead(C_light_button); //read input value and store it
buttonD = digitalRead(D_light_button); //read input value and store it


if (buttonA == HIGH) { // if button A is on do the following
  digitalWrite(A_light, HIGH);  //turn A light on
  delay(900000UL); // wait for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }
    digitalWrite(A_light, LOW); // turn A light off
    delay(1000); //turn all off for 1 second
  }
if (buttonB == HIGH) { // if button B is on do the following
  digitalWrite(B_light, HIGH);  //turn B light on
  delay(900000UL); //waint for 15mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }
    digitalWrite(B_light, LOW); //turn B ligt off
    delay(1000); //turn all off for 1 second
  }
if (buttonC == HIGH) { // if button C is on do the following
  digitalWrite(C_light, HIGH);  // turn C light on
  delay(900000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }
    digitalWrite(C_light, LOW); // turn C light off
    delay(1000); //turn all off for 1 second
  }
if (buttonD == HIGH) { // if button D is on do the following
  digitalWrite(D_light, HIGH);  // turn D light on
  delay(900000UL); // waint for 15 mins
  if (timebutton == HIGH) {
    delay(900000UL); // delay for an additional 15 mins if HIGH
  }
  digitalWrite(D_light, LOW); // turn D light off
  delay(1000); //turn all off for 1 second
  }
 
}


and this how i was wiring the switches
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
by all means say if you see something you dont like
OK. Since you invited...

Some of your variable names could stand improvement. In 3 months, will you remember what buttonC is?
Names like pinForLightC, stateOfC, pinForSwitchC, etc. are meaningful. Its easy to see which ones are meant to be constant and which ones are meant to contain changing values. Which category is buttonC in?

There is a convention that #define names be all upper case, to distinguish them from variables, whether const or not.

Making the delay time a variable means that the same value can be used in may places, but only needs to  be changed in one place, if the need to change it occurs. In your code, if the need to make the on time 20 minutes, instead of 15, arose, you would have to change the value in 8 places.

Finally, the use of arrays and loops and functions would cut your sketch size considerably. You are doing the same thing 4 times, except with different pins. Create a function that tests the switch state and performs, or not, the delay(s). Call that function 4 times, with different switch and LED pin numbers. Then, call it in a loop, with the pin numbers in arrays. Watch how much smaller, and easier to understand, your code gets.

Congratulations on getting a functioning program created that meets your needs.
Logged

Pages: 1 [2] 3   Go Up
Jump to: