Is this efficient?

Hello guys,

I’ve been a passive user for some time and I learned a lot by reading your tutorials, advices, etc and I thank you so much for giving me a basic understanding about either analog and digital circuits.
But right now I’m stuck at a very simple approach and need some input.

Current project is some kind of a brightness dependant nightlight.
I’ve been using a LDR and a NE555 so far (analog), but I want to include some more doors and I decided to switch to using an Arduino Pro Mini.
If its dark enough the Arduino should power a Transistor as soon as a door is opened (suggestions for Transistors are very welcome, my collection is listed at the end of the post) for like 1 or 2 Minutes. Also the code shouldnt retrigger if the door was already open. The circuit is battery powered and should be low-power consuming.

My attempt so far is

//second try

int nbts = 2;
boolean btgs[2];
int bts[2];

int startpin = 2;

int ldr;

unsigned long abstand = 6000 * 10 * 10;







void setup() {
  Serial.begin(9600);
  for (int i = 0; i < nbts; i++) bts[i] = i + startpin;
  for (int i = 0; i < nbts; i++) btgs[i] = false;
  for (int i = 0; i < nbts; i++) pinMode(bts[i], INPUT_PULLUP);

}



void buttons(int i) {

  if (!btgs[i]) {
    if (digitalRead(bts[i]) == LOW) {
      Serial.println("im low");  //i got buttons with 3 pins, so this doesnt really matter
      btgs[i] = true;
    }
  }
  else {
    if (digitalRead(bts[i]) == HIGH) {
      Serial.println("im high");     
      btgs[i] = false;
    }
  }
  delay(500);
}


void loop() {
  int ldr = analogRead(A0);

    delay(abstand);

  }

  else {
    for (int i = 0; i < nbts; i++) {
      buttons(i);
    }
  }
}

Can’t test right now and wanna go on with it. Does it work? Am I stuck? How can it be more efficient?

Thanks in advance and sorry for bothering you guys, but I’m just an average enthusiast and need you professionals to proof it.

Transistors ready to be used: 2N2222, S9012, S9013, S9014, S9015, S9018, A1015, C1815, A42, A92, 2N5401, 2N5551, A733, 2N2222, C945, S8050, S8550, 2N3906, 2N390

Test it, then post about issues that arise.

Thanks for using code tags!

Format your code, remove those big pieces of white space, add comments to your code so we don’t have to guess what you are trying to do.

Use words, helps document your code: nbts, btgs, abstand.


unsigned long abstand = 6000 * 10 * 10; >>>——-> unsigned long abstand = 6000ul * 10 * 10;


for (int i = 0; i < nbts; i++) bts[i ] = i + startpin; <——<<< place on separate lines


delay(abstand);
don’t use delay as it blocks code execution for that amount of time, reasearch BWD (BlinkWithoutDelay)

What’s not working?

It would be more readable if you replaced

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < nbts; i++) bts[i] = i + startpin;
  for (int i = 0; i < nbts; i++) btgs[i] = false;
  for (int i = 0; i < nbts; i++) pinMode(bts[i], INPUT_PULLUP);

}

with

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < nbts; i++) 
  {
     bts[i] = i + startpin;
     btgs[i] = false;
     pinMode(bts[i], INPUT_PULLUP);
  }
}

Also this doesn’t compile:

void loop() {
  int ldr = analogRead(A0);

    delay(abstand);

  }

  else {
    for (int i = 0; i < nbts; i++) {
      buttons(i);
    }
  }
}

the else portion is just kind of hanging out

This will make your code more readable and memory more efficient. Replace:

int nbts = 2;
boolean btgs[2];
int bts[2];

with:

const int nbts = 2;
boolean btgs[nbts];
byte bts[nbts];

This will result in a 600 second (10 minute) delay each time through loop()

unsigned long abstand = 6000 * 10 * 10;

ok guys, first of all thanks for the fast and constructive feedback. You already helped me. Gonna get through it in detail tomorrow and will see if I understand what you told me :slight_smile: Good night and, as I already said, thank you.