Go Down

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

Joes

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.

PaulS

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 the
int buttonC = 0;         // state of the input pin
int 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

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.

PaulS

Quote
Don't use 'b' as a loop variable and 'i' as an index

Excellent advice!

Joes

dont augh  :P   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

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.

PaulS

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

geting there?

Code: [Select]

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);
   
}



AWOL

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.

PaulS

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

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

magnethead794

Are you using the compiler?

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

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

PaulS

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().

Joes

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




Go Up