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

hi thanks for your reply
the reason being the name button c this will be going into a glass house with four sections hence a,b,c,d.
but yes i could see there could be more improvement on that

making sketch smaller:
ok would you quickly knock an example sketch up to show me this compared to what i have done.

thanks for your time and help.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok would you quickly knock an example sketch up to show me this compared to what i have done.
Code:
#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
becomes
Code:
const int lightPins[] = {13, 12, 11, 10};

Code:
#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
becomes
Code:
const int switchPins[] = {1, 2, 3, 4};

Code:
int buttonA = 0;     
int buttonB = 0;         // val will be used to store the
int buttonC = 0;         // state of the input pin
int buttonD = 0;
becomes
Code:
int states[4];

Code:
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);
}
becomes
Code:
void setup()
{
  for(byte b=0; b<4; b++)
  {
    pinMode(switchPins[i], INPUT);
    pinMode(lightPins[i], OUTPUT);
  }
  pinMode(time_button, INPUT);   
}

Now, you try making the changes to loop(). Read the states in a loop. Create a function to use the state of one pin to toggle the LED on another pin. The function will look like:
Code:
void toggleWithLongDelay(int switchState, int lightPin, bool longDelay)
{
   // Do stuff here...
}

The call will look like
Code:
   toggleWithLongDelay(state[i], lightPins[i], timeButton == HIGH);

That call will be made in a loop, obviously.
Logged

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

Don't use 'b' as a loop variable and 'i' as an index
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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Don't use 'b' as a loop variable and 'i' as an index
Excellent advice!
Logged

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

dont augh  smiley-razz   what do you think?

Code:
const int lightPins[] = {13, 12, 11, 10};
const int switchPins[] = {1, 2, 3, 4};

int states[4];

void setup()
{
for(byte b=0; b<4; b++)
{
pinMode(switchPins[a], INPUT);
pinMode(lightPins[a], OUTPUT);
}
pinMode (time_button, INPUT);
}

void toggleWithLongDelay(int switchState, int lightPin, bool longDelay)
{
 
    if (button == HIGH) {
  digitalWrite(lightPin, HIGH); 
  delay(900000UL);
  if (timeButton == HIGH) {
    delay(900000UL);
  }
    digitalWrite(lightPin, LOW);
    delay(1000);
    }
   
    toggleWithLongDelay(state[a], lightPins[a], timeButton == HIGH);
   
}
Logged

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

I think don't use b as as loop count, and a as an index
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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The loop function is missing. The code to read the switch states is missing. The toggle... function should not call the toggle... function; the loop() function should.
Logged

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

geting there?

Code:
const int lightPins[] = {13, 12, 11, 10};
const int switchPins[] = {1, 2, 3, 4};
#define time_button 7

int states[5];

void setup()
{
for(byte f=0; f<5; f++)
{
pinMode(switchPins[f], INPUT);
pinMode(lightPins[f], OUTPUT);
}
pinMode (time_button, INPUT);
}

void loop ()

void toggleWithLongDelay(int switchState, int lightPin, bool longDelay)

 
    if (switchPins == HIGH) {
  digitalWrite(lightPin, HIGH); 
  delay(900000UL);
  if (time_Button == HIGH) {
    delay(900000UL);
  }
    digitalWrite(lightPin, LOW);
    delay(1000);
    }
   
    toggleWithLongDelay(state[f], lightPins[f], time_Button == HIGH);
   
}


Logged

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

You can't nest function declarations
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.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
geting there?
Using the verify button on the IDE is orders of magnitude faster than copying the code and pasting it into a reply, and waiting for us to tell you that that won't compile.
Logged

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

i'm a bit confused of how to write it within the loop
Logged

Ft. Worth, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 591
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you using the compiler?

In Arduino IDE, it's under Sketch -> Verify/Compile

Or Apple+R on OSX. (Probably ctrl+R on windows)
Logged

KF5RVR

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50476
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i'm a bit confused of how to write it within the loop
If you mean that you are confused about how to write the toggleWithLongDelay() function in loop(), the answer is that you don't. The toggleWithLongDelay() function goes either before or after loop(), and you call it from within loop().
Logged

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

hi thought i would post some pic's of the finished product:



Logged

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