Go Down

Topic: Running a loop once (Beginner talk!) (Read 942 times) previous topic - next topic

jampola

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.

Quote

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! :)

Z

could do something like:

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

PaulS

Quote
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?

Intosia

#3
Jan 26, 2011, 01:13 pm Last Edit: Jan 26, 2011, 01:14 pm by Intosia Reason: 1

Code: [Select]
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  ;)
I would do !trigger, much cleaner.
Edit: hm no bold tags in quote...

jampola



Code: [Select]
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  ;)
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. :D

Go Up