Go Down

### Topic: light control unit code problems (Read 7911 times)previous topic - next topic

#### Joes

#30
##### Oct 30, 2011, 04:48 pm
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.

#### PaulS

#31
##### Oct 30, 2011, 06:13 pm
Quote
ok would you quickly knock an example sketch up to show me this compared to what i have done.

Code: [Select]
`#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: [Select]
`const int lightPins[] = {13, 12, 11, 10};`

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

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

Code: [Select]
`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: [Select]
`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: [Select]
`void toggleWithLongDelay(int switchState, int lightPin, bool longDelay){   // Do stuff here...}`

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

That call will be made in a loop, obviously.

#### AWOL

#32
##### Oct 30, 2011, 06:28 pm
Don't use 'b' as a loop variable and 'i' as an index
"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.
I speak for myself, not Arduino.

#### PaulS

#33
##### Oct 30, 2011, 10:35 pm
Quote
Don't use 'b' as a loop variable and 'i' as an index

#### Joes

#34
##### Oct 31, 2011, 05:03 pm
dont augh     what do you think?

Code: [Select]
`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);   }`

#### AWOL

#35
##### Oct 31, 2011, 05:57 pm
I think don't use b as as loop count, and a as an index
"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.
I speak for myself, not Arduino.

#### PaulS

#36
##### Oct 31, 2011, 07:12 pm
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.

#### Joes

#37
##### Oct 31, 2011, 08:02 pm
geting there?

Code: [Select]
`const int lightPins[] = {13, 12, 11, 10};const int switchPins[] = {1, 2, 3, 4};#define time_button 7int 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);   }`

#### AWOL

#38
##### Oct 31, 2011, 09:30 pm
You can't nest function declarations
"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.
I speak for myself, not Arduino.

#### PaulS

#39
##### Nov 01, 2011, 12:51 pm
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.

#### Joes

#40
##### Nov 07, 2011, 09:15 pm
i'm a bit confused of how to write it within the loop

#41
##### Nov 07, 2011, 09:59 pm
Are you using the compiler?

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

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

#### PaulS

#42
##### Nov 08, 2011, 01:36 am
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().