Running a loop once (Beginner talk!)

Hi, I'm new and I am completely losing my mind with my uno! I've been interested in open source hardware since building a monome a year or so ago.

I've basically gone from knowing next to no programming to finally wrapping my head around it (albeit slowly!) and it's making me pretty happy on the inside! yay!

I am wanting to run a loop once. Basically all I need is for my lightPin to sense brightness and once it senses, the motor will power on (HIGH) and delay 2000ms till (LOW) but no matter what combination of statements I use, I can only manage to have it turn on and stay on constantly until my lightPin senses darkness. I've pasted my code below.

I hope this makes sense. The project I am attempting is to simply open up a set of household blinds once ambient light has hit a predetermined level using "threshold". Obviously once ambient light falls below a certain level, I will add to the code so it closes. I'll figure that out later.

int lightPin = 0;
int motorPin = 9;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(lightPin, INPUT);
}

void loop() {
if (analogRead(lightPin) > 300) {
digitalWrite(motorPin, HIGH);
delay(2000);
digitalWrite(motorPin, LOW);
}
else if (analogRead(lightPin) < 300) {
digitalWrite(motorPin, LOW);
}
}

Thanks heaps guys and maybe it's a good idea to have an "Absolute Beginner Talk" forum so people like me don't flood the "smart people" forum with stupid questions! :slight_smile:

could do something like:

int lightPin = 0;
int motorPin = 9;
boolean trigger=false;

void setup()
{
 pinMode(motorPin, OUTPUT);
 pinMode(lightPin, INPUT);
}

void loop() {
   if (analogRead(lightPin) > 300 && trigger==false) {     
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);
      trigger=true;
        }
   if (analogRead(lightPin) < 300) {
      digitalWrite(motorPin, LOW);
      trigger=false;
        }
  }

and delay 2000ms till (LOW)

This requirement does not make sense. If the motor is supposed to run for 2 seconds, that's one requirement. If the motor is supposed to run until the sensor goes LOW, that is a different requirement.

Perhaps your issue with the code is really a matter of not being able to clearly define what the code is supposed to do.

Would you care to try again?

Z:

int lightPin = 0;

int motorPin = 9;
boolean trigger=false;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(lightPin, INPUT);
}

void loop() {
  if (analogRead(lightPin) > 300 && !trigger) {    
     digitalWrite(motorPin, HIGH);
     delay(2000);
     digitalWrite(motorPin, LOW);
     trigger=true;
       }
  if (analogRead(lightPin) < 300) {
     digitalWrite(motorPin, LOW);
     trigger=false;
       }
 }

XD :wink:
I would do !trigger, much cleaner.
Edit: hm no bold tags in quote...

Intosia:

Z:

int lightPin = 0;

int motorPin = 9;
boolean trigger=false;

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(lightPin, INPUT);
}

void loop() {
  if (analogRead(lightPin) > 300 && !trigger) {    
     digitalWrite(motorPin, HIGH);
     delay(2000);
     digitalWrite(motorPin, LOW);
     trigger=true;
       }
  if (analogRead(lightPin) < 300) {
     digitalWrite(motorPin, LOW);
     trigger=false;
       }
 }

XD :wink:
I would do !trigger, much cleaner.
Edit: hm no bold tags in quote...

Worked a charm and thanks for going to the effort! After reading through the code carefully, it makes perfect sense! This community rules. :smiley: